I have a new NodeMCU ESP8266. I made sure that the builtin LED on it was working properly by uploading the "Blink" example sketch from the Arduino IDE examples and it worked. When I try my own code to make the LED turn on or off using commands from the serial monitor or audio input the LED simply stays lit the whole time.
I have also made sure that the serial monitor is working by logging (Serial.println(command)
) the command that I send to the COM3 port to which my NodeMCU is connected.
How should I go about solving this?
My code (I have copied this from the internet after my debugging efforts on my own code failed but this also did not work):
int led = LED_BUILTIN;
void setup()
{
Serial.begin(9600); //Baud Rate
pinMode(led, OUTPUT);
}
void loop()
{
char data = Serial.read();
switch (data) //Selection Control Statement
{
case 'ON':
digitalWrite(led, HIGH); // Sets the led ON
break;
case 'OFF':
digitalWrite(led, LOW); //Sets the led OFF
break;
}
}
EDIT: My original code:
String cmnd = "";
void setup()
{
Serial.begin(9600);
Serial.setTimeout(10);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
while(Serial.available() == 0) {}
cmnd = Serial.readString();
if(cmnd == "on") {
Serial.println(cmnd);
digitalWrite(LED_BUILTIN, HIGH);
} else if (cmnd == "off") {
Serial.println(cmnd);
digitalWrite(LED_BUILTIN, LOW);
}
}
```
2 Answers 2
The data = Serial.read()
just returns one character. You'll get an O
the first time and F
for the next two reads and then, perhaps, a CR
or LF
character depending on what you're transmitting.
Try changing to case 'N'
and case 'F'
instead. This is a simple fix and doesn't require any fancy buffering.
void loop()
{
char data = Serial.read();
switch (data) //Selection Control Statement
{
case 'N': // ON
digitalWrite(led, HIGH); // Sets the led ON
break;
case 'F': // OFF
digitalWrite(led, LOW); //Sets the led OFF
break;
}
}
If you want to expand the range of controls then have a read of Using Serial.read() with Arduino which seems to be a useful tutorial.
-
\$\begingroup\$ Thank you very much, this worked, In my original code which I will edit and add to the question I was actually using Serial.Readline() which was printing ON and OFF properly to the serial monitor but still not working as expected. Thanks a lot again. \$\endgroup\$Odasaku– Odasaku2021年10月25日 10:44:31 +00:00Commented Oct 25, 2021 at 10:44
-
1\$\begingroup\$ Good work. Make sure you leave the original question intact so that the answers make sense. Thank you for accepting my answer. \$\endgroup\$Transistor– Transistor2021年10月25日 10:46:04 +00:00Commented Oct 25, 2021 at 10:46
Please note that "not working" is not a good description as the code simply does what it is told to do. Also this is not an electronics problem, but a generic C programming problem, suggesting that it would be beneficial to read a C tutorial rather than copying random code snippets from the Internet.
The code just is not written to do what you expect and the compiler warnings about the code would be a strong warning sign that there are problems with it. Always read the compiler warnings and have a look at the code that gives warnings.
A char is a data type that can only contain a single character.
The serial read method also returns a single character, if there is one that is received.
So you can't compare a single character with 'ON' or 'OFF' as they are not single characters.
Also you are using the 'ON' and 'OFF' as single characters which will both get truncated to a single character 'O' so every time there is the letter 'O' received it will hit the first case match and turns the LED on.
Try for example characters 'X' and 'Y' to turn the LED on and off.
data = Serial.read()
just return one character? You'll get anO
the first time andF
for the next two reads. Try changing tocase 'N'
andcase 'F'
instead. \$\endgroup\$