I just dont understand this "IF" thing

Ok everybody newbie in the house. LOL I am trying to understand this code but I am missing something. Im just trying to turn on 2 LEDS but I dont understand the parameters associated with the variable "R", "r" or "A", "a".. meaning if I put "R1" the 2nd LED doesnt come on (but the first one does) If I put "A1" the 2nd LED does come on.

How do I define more characters(variables) as placeholders for turning the light on and off. I learned that I can turn them on/off with Numbers, Capitol and Lowercase letters. What am I missing? because you can run out of them real quick. Should I only use numbers? and not letters?

[u]Here it is[/u]

int message = 0; // This will hold one byte of the serial message int redLEDPin = 11; // What pin is the red LED connected to? int redLED = 0; // The value/brightness of the LED, can be 0-255 int redLEDPin2 = 10; int redLED2 = 0;

void setup() { Serial.begin(9600); //set serial to 9600 baud rate }

void loop(){ if (Serial.available() > 0) { // Check if there is a new message message = Serial.read(); // Put the serial input into the message

if (message == 'R'){ // If a capitol R is received... redLED = 255; // Set redLED to 255 (on)

} if (message == 'r'){ // If a lowercase r is received... redLED = 0; // Set redLED to 0 (off)

} if (message == 'A'){ //what if I wanted to use A1 here? redLED2 = 255; } if (message == 'a'){ redLED2 = 0; } } analogWrite(redLEDPin, redLED); // Write an analog value between 0-255 analogWrite(redLEDPin2, redLED2); }

Serial.read gets 1 byte from the serial buffer, if there is a byte to read. Storing the value read in a variable that is larger than a byte is a waste of space.

But, you could look at the types that are the same size as byte. Hmm, look at that. The char type is the same size. So, you could store the value returned by Serial.read in a char type variable, instead.

You could store the values in successive positions in an array of chars. Hmm, a string is an array of chars when the array is NULL terminated.

There are string comparison functions that you could use (strcmp comes to mind).

char message[40];
int index = 0;

void loop()
{
   while(Serial.available() > 0)
   {
      char aChar = Serial.read();
      message[index] = aChar; // Store the character
      index++; // Increment the index
      message[index] = '\0'; //NULL terminate the string
   }

   if(strcmp(message, "Please turn LED 5 on") == 0)
   {
       // Turn LED 5 on
   }
}