Serial.read

How can I save whole command like "Lights on" sent from rs232 to variable by function Serial.read? I was trying to do that, but i wasn`t successul. It has wrote just first letter.

You have to build the string from the individual characters. The below works with the current 0021 IDE which contains some string handling functions. I’ve commented out the servo stuff so the string sent should be captured in readString and sent back to the serial monitor.

http://www.arduino.cc/en/Tutorial/TextString

// zoomkat 10-4-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// for 0019 and later

String readString = String(100);
//#include <Servo.h> 
//Servo myservo;  // create servo object to control a servo 

void setup() {
      Serial.begin(9600);
        //myservo.attach(9);
        }

void loop() {

        while (Serial.available()) {
        delay(10);  
          if (Serial.available() >0) {
        char c = Serial.read();  //gets one byte from serial buffer
        readString += c;} //makes the string readString
        }
        
      if (readString.length() >0) {
      Serial.println(readString);
      /*
      int n;
      char carray[6];
      readString.toCharArray(carray, sizeof(carray));
      n = atoi(carray); 
      myservo.writeMicroseconds(n);
      //myservo.write(n);
      */
      readString="";
      } 
   }

OK thanks a lot. It works! But now, I have another problem - the function if...else probably does not work as it should, it runs through the else part even though an if condition is true. Here is the code:

String readString = String(0);
String mistake;
int ledPin1=13;
int ledPin2=12;
int error=0;
void setup() {
      Serial.begin(9600);
        Serial.println("Ready!");
        readString="";
        pinMode(13, OUTPUT); 
        pinMode(12, OUTPUT); 
        }

void loop() {
        while (Serial.available()) {
        delay(10);  
          if (Serial.available() >0) {
        char c = Serial.read();  //gets one byte from serial buffer
        readString += c;} //makes the string readString
        }
        
      if (readString.length() >0) {
        if (readString == "LED 1 ON") {
          digitalWrite (ledPin1,HIGH);
            Serial.println(readString + " - done");
        }
        if (readString == "LED 1 OFF"){
          digitalWrite(ledPin1, LOW);
            Serial.println(readString + " - done");
      }
      if (readString == "LED 2 ON") {
        digitalWrite (ledPin2, HIGH);
          Serial.println(readString + " - done");
      }
      if (readString == "LED 2 OFF") {
        digitalWrite (ledPin2, LOW);
          Serial.println(readString + " - done");
      }
      if (readString == "ALL LED OFF") {
        digitalWrite (ledPin2, LOW);
        digitalWrite (ledPin1, LOW);
          Serial.println(readString + " - done");
      }
    if (readString == "ALL LED ON") {
        digitalWrite (ledPin2, HIGH);
        digitalWrite (ledPin1, HIGH);
          Serial.println(readString + " - done");
    }
   
     else 
       error++;
       if(error == 1){mistake = "Message 1!";}
       if(error == 2){mistake = "Message 2!";}
       if(error == 3){mistake = "Message 3!";}
       if(error == 4){mistake = "Message 4!";}
       if(error == 5){mistake = "Message 5!";}
       if(error >= 6){mistake = "Message 6!";}

       Serial.println(mistake);
      readString="";
      
      }
   }

Notice that each if statement is followed by a pair of curly braces ({ and }) that enclose the block of statements that should be executed.

Notice also that the else statement does not have a similar set of braces. This means that the error++; statement is the only one affected by the if/else block.

Also notice that the else goes with a particular if statement. You are not putting each open brace on a separate line, so lining up the opening ans closing braces isn’t easy. This makes it difficult to see which if statement the else goes with.

Code like this:

if(i == 0)
{
   if(j == 14)
   {
   }
}
else
{
}

makes it much easier to see the blocks and the relationship between the if and the else than this code:

if(i == 0) {
   if(j == 14) {
   }
   else
}