Reading serials

I am trying to modify the code below such that I can turn the LED on or off with the commands "on" and "off". When I type multiple characters into the serial box, they are read one by one and set to "message" variable one after the other. How do I set them up such that I can type "on" and the variable "message" stores that word, such that I can compare it in the if statement to turn the LED on or off. Thanks for the help, just starting to familiarise myself with serial so forgive me if I'm asking something very simple.

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

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)
   }

 }   
analogWrite(redLEDPin, redLED);  //  Write an analog value between 0-255
}

How do I set them up such that I can type “on” and the variable “message” stores that word

The way that code is written? You can’t. char arrays hold character strings. ints do not.

This topic only comes up about 7 times a week. A little research would have turned up a bazillion threads. One way to collect characters in an array is like so:

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Where it says “Process the packet”, you can call strcmp() to see if the string is “on”. Call it again to see if the string is “off”.

This code requires that you send or , and strips off the < and >, but it needs them to know where the packet starts and ends. Any alternate code you come up with will need some sort of packet delimiters.