Part2: http://dredx.com/electronics/?p=222

Part1:

I have made a video tutorial on how to have the arduino receive commands over the serial connection from the computer and recognize that data as a specific command. In the tutorial we just use it to light an LED but it would be easily modifiable to change a relay or even change a servo position.

Tomorrow I will be posting a tutorial on how to create a python program to send the data to the arduino.

For this tutorial I simply wired a LED between pin 8 and ground(with a resistor). You could also use the built in LED on the board but I needed something more visible…
Arduino Tutorial

Here is the final code with comments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
This Program will recieve data from the computer over serial
and process it as different commands depending on the leading byte
 */

 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int state;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  if (Serial.available() > 0) {
    if (Serial.peek() == 'c') { //check for the character that signifies that this will be a command
      Serial.read(); //remove the character that signifies this is a command from the serial buffer
      state = Serial.parseInt(); //store our expected integer into state
      digitalWrite(led,state); // set the state of the LED
    }
    while (Serial.available() > 0){ //Discard everything that we didn't expect
      Serial.read();
    }
  }
}

2 LED States with one command

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
This Program will recieve data from the computer over serial
and process it as different commands depending on the leading byte
 */

 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led[] = {13,8};
int current;
int state;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led[0], OUTPUT);
  pinMode(led[1], OUTPUT);
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  if (Serial.available() > 0) {
    while (Serial.peek() == 'L') { //check for the character that signifies that this will be a command
      Serial.read(); //remove the L off the serial buffer
      current = Serial.parseInt();
      Serial.read(); //remove the 'S' that we used as a integer seperator off the serial buffer
      state = Serial.parseInt(); //store our expected integer into state
      digitalWrite(led[current],state); // set the state of the LED
    }
    while (Serial.available() > 0){ //Discard everything that we didn't expect
      Serial.read();
    }
  }
}

To make the above code work we would need to send a string such as L0S1L1S1 which would turn on LED 0 and LED 1. To turn off LED0 we could send L0S0 or to change both to off we could send L1S0L0S0 (notice the order doesnt matter) hope this makes sense, it was tough thinking of a way to do this and have it explainable to someone else.

For a more advanced example look at my previous post here: http://dredx.com/electronics/?p=179