Commands with serial monitor

hi all, I encountered a problem in this program, when I run the if the program does not recognize the string that I enter and then the program does not turn on the LED.
Someone knows how to help me.
Thanks

String command;
#define led 13
void setup() {
pinMode(led,OUTPUT);
Serial.begin(9600);
}
void loop() {
while(Serial.available()==0);
while(Serial.available()){
delay(10);
char c=Serial.read();
if(c=='#'){break;}
command+=c;
}
if(command=="turn on led"){Serial.println("LED on");digitalWrite(led,1);}
Serial.print(command);
command="";
}

Try a single-character command first. Like "o" for on and "f" for off.

It is best to avoid the use of big-S Strings and delay() if your program is going to get much bigger than this.

read this. pay particular attention to #7:

7. If you are posting code or error messages, use "code" tags

http://forum.arduino.cc/index.php/topic,148850.0.html

this forum is like flight school: everybody taking flying lessons believes they know all the ground school stuff before they walk in the door. if you show up not knowing basic things you get no respect.

Works OK for me. Here is the output I see on serial monitor:

LED on
turn on led

What do you see?

in future, please post your code correctly. Read the forum guide in the sticky post to find out how, and lots of other useful tips.

@OP

The program also works fine for me!

sm72.png

sm72.png

what version do you have of the IDE?

Why? Do you have any compilation error or uploading error?

Did you select "9600" speed in serial monitor?

  • no upload error

  • yes i have selected 9600

I reloaded the program several times but it still does not go

ok guys I solved, I had not selected "no line ending" in the serial monitor.

thank you all for your help