How can I control an LED via the serial monitor?

Hello all,

I would like to turn on a LED as soon as a sentence is entered in the Serial Monitor. I already have the first approach, but for now only with one letter. So currently it is so that the LED is turned on as soon as I enter the letter "Y". However, I would like to have it so that the LED is switched on when "Turn on yellow LED" is entered. So a sentence should be entered. How can I do this? Let me show you my first approach. Thanks a lot!

const int yellowPin = 8;

void setup() {

  pinMode(yellowPin, OUTPUT);
  Serial.begin(115200);
  Serial.println("Enter Value");
  Serial.print("Input:");
  
}

void loop() {

  if (Serial.available() > 0) {
      char comdata = char(Serial.read());
    if (comdata == 'Y') {
      Serial.println("Yellow LED is ON");
      digitalWrite(yellowPin, HIGH);
    }
    else if (comdata == 'X') {
      Serial.println("all LED turn OFF");
      digitalWrite (yellowPin, LOW);
    }
  }
}

This is the circuit. But instead of the red LED, a yellow LED is connected.

What are the results of running the above sketch ?

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

I have just uploaded a picture. When the code is executed and I enter the letter "Y" in the serial monitor the LED is turned on.

And what do you want it to do differently ?

I want to use a phrase to turn on instead of just one letter. So the LED should be turned on when the phrase "Turn on yellow LED" is entered and not just the letter "Y". I just don't know how to implement this with the programming.

Have a look at the strcmp() and strncmp() functions.

Do you want that exact phrase, or would "TURN ON YELLOW LED", or "turn on yellow led" or any other combination of letter case be accepted?

Do you know what a character array is ?

As mentioned, strcmp

https://cplusplus.com/reference/cstring/strcmp/

  String sentence = Serial.readString();
  if (sentence.startsWith("Turn on yellow LED"))
  {
    // Do stuff
  }

Hi,
try this code.

The text entered can be in lowercase or uppercase, but it must have exactly the same characters and spaces as the "if"

Remember to configure your serial monitor with the newLine option.
If not, it won't work properly.

SerialMonitor2

const int yellowPin = 8;

void setup() {

  pinMode(yellowPin, OUTPUT);
  Serial.begin(115200);
  Serial.println("Enter Value");
  Serial.print("Input:");
  delay(100);

}
String comdata = "";
void loop() {

  if (Serial.available() > 0) {
    comdata  = Serial.readStringUntil(0x0A);
    delay(10);
    comdata.toUpperCase();
    Serial.println(comdata);
  }
  if (comdata == "TURN ON YELLOW LED") {
    Serial.println("Yellow LED is ON");
    digitalWrite(yellowPin, HIGH);
    comdata = "";
  }
  if (comdata == "TURN OFF YELLOW LED") {
    Serial.println("Yellow LED is OFF");
    digitalWrite (yellowPin, LOW);
    comdata = "";
  }
}

As you progress, consider your parser… it needs a complete comparison for every phrase/command.

Ideally, you’ll break the command line into multiple ordered parameters (use strtok), then you can have tests (strcmp) for specific words in a specific order…

Then you only need to test for TURN once, YELLOW once, LED once and ON or OFF once, along with any other keywords or commands as needed.

A useful tip is to process the string in UPPERir case only -then your string comparison is much simpler.

BTW, this is usually much more compact and flexible if you use c-strings rather than Strings.

If I can find time, I’ll post a complete serial parser example that supports single key ‘hot keys’, as well as layered command strings. I need to trim it down for instructional use, but it works perfectly, isn’t that long, and can prompt for missing, misspelled or invalid entries.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.