Alk3r
March 9, 2019, 2:33pm
1
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.
PaulRB
March 9, 2019, 3:39pm
4
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!
Alk3r
March 9, 2019, 5:23pm
6
what version do you have of the IDE?
Why? Do you have any compilation error or uploading error?
PaulRB
March 9, 2019, 7:14pm
8
Did you select "9600" speed in serial monitor?
Alk3r
March 9, 2019, 7:26pm
9
no upload error
yes i have selected 9600
I reloaded the program several times but it still does not go
Alk3r
March 9, 2019, 7:33pm
10
ok guys I solved, I had not selected "no line ending" in the serial monitor.
thank you all for your help