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.
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.
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.