can't read string from serial to preform certian task

i searched all the internet just to figure out how read text from serial and save it in a string
i just don’t know why this code doesn’t work

p.s. it works if i replaced while with if

#include<Servo.h>

Servo servo;

void setup() {
  Serial.begin(9600);
  servo.attach(9,330,2200);
}

void loop()
{
  String content;
  char character;
  servo.write(90);

  while (Serial.available() > 0) {
    character = Serial.read();
    content.concat(character);
    delay(10);
    Serial.println(content);
  }

  if (content == "start") {
    Serial.println(content);
    servo.write(0);
    delay(1000);

  }
}

I think you need to test for the newline character - you're just stuffing it in the string so
it won't be == "start" but "start\n" or "start\r" or whatever line termination chars are
being sent.

Basic test code similar to yours that uses a comma , as the string delimiter, and operates the LED on the arduino board.

//zoomkat 3-5-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
//send on, or off, from the serial monitor to operate LED

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      Serial.println(readString); //prints string to serial port out
      //do stuff with the captured readString
      if(readString.indexOf("on") >=0)
      {
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON");
      }
      if(readString.indexOf("off") >=0)
      {
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF");
      }       
      readString=""; //clears variable for new input
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

MarkT:
I think you need to test for the newline character - you're just stuffing it in the string so
it won't be == "start" but "start\n" or "start\r" or whatever line termination chars are
being sent.

you were absolutely right and it works fine but i still wonder how this "\n" get stuffed in the string and why it worked without "\n" in case of if() instead of while() ???!!

The newline is inserted by the IDE.

zoomkat:
Basic test code similar to yours that uses a comma , as the string delimiter, and operates the LED on the arduino board.

//zoomkat 3-5-12 simple delimited ',' string parse 

//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter
//send on, or off, from the serial monitor to operate LED

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}

void loop() {

if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      Serial.println(readString); //prints string to serial port out
      //do stuff with the captured readString
      if(readString.indexOf("on") >=0)
      {
        digitalWrite(ledPin, HIGH);
        Serial.println("LED ON");
      }
      if(readString.indexOf("off") >=0)
      {
        digitalWrite(ledPin, LOW);
        Serial.println("LED OFF");
      }       
      readString=""; //clears variable for new input
    } 
    else {     
      readString += c; //makes the string readString
    }
  }
}

Thank you, I tried your code but unfortunately in didn't work; I don't know if it's a missing library or what !

This is where the newline ( \n ) comes from:

But before you go changing that, you need some way of telling the end of the string.

You can look for a comma, but why not work with the newline you are getting anyway? Look for that.

Please note that in versions of the IDE up to and including 1.0.3, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.), as described here for example.

Alternatively, install the fix described here: Fixing String Crashes

Preferably upgrade your IDE to version 1.0.4 or above at: http://arduino.cc/en/Main/Software

Thank you, I tried your code but unfortunately in didn't work; I don't know if it's a missing library or what !

Another version that does not use a character delimiter.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if(readString.indexOf("on") >=0)
    {
      digitalWrite(ledPin, HIGH);
    }

    if(readString.indexOf("off") >=0)
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}