1
\$\begingroup\$

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);
  }
}
```
\$\endgroup\$
3
  • \$\begingroup\$ It could be that there's a conflict between the serial pins and the led. arduino.stackexchange.com/questions/43682/… confirm specifically which board you have and what pins it uses for the built in led and serial pins. Alternatively try adding an led to a different gpio. \$\endgroup\$
    – Passerby
    Commented Oct 25, 2021 at 8:35
  • 2
    \$\begingroup\$ Won't the data = Serial.read() just return one character? You'll get an O the first time and F for the next two reads. Try changing to case 'N' and case 'F' instead. \$\endgroup\$
    – Transistor
    Commented Oct 25, 2021 at 8:36
  • \$\begingroup\$ In your original code, you would have to be a very fast typist to manage "on" of "off" in 10 milliseconds, milliseconds being the unit for setTimeout(). \$\endgroup\$ Commented Nov 3, 2023 at 15:28

2 Answers 2

2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\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
    Commented 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
    Commented Oct 25, 2021 at 10:46
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.