Reputation: 21
So,I have a task at hand and that is to use a word, like my name "DASH" as input to Serial Monitor in Arduino so that the LED in the ESP32-WROOM-32 blinks and otherwise, it won't. I am fairly new to the Arduino side and would really appreciate any guidance at all, even though it may be very little info so feel free please.
I am using ESP32-WROOM-32 module and no outside LED, the one that blinks blue in the module itself.
Here's what I have tried:
String incomingString;
int LED_BUILTIN=2;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if (Serial.available()>0)
{
String incomingString=Serial.readString();
Serial.println("Enter 'Dhaval' to blink thy LED in hue of blue!");
if (incomingString=="Dhaval"){
Serial.println("You have entered 'Dhaval', needless to say enjoy the beautiful baby blue LED.");
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
Serial.println("Seems that you've entered anything else but Dhaval, please just enter 'Dhaval'.");
}
}
}
I am aware that in this code it would be best to use for loop and if conditions to make it take each character once at a time, but not sure how to achieve that, plus I have a feeling that it may just work with this string defined as when I get to read the entry as to what I have entered and get to print it with Serial.print command it shows exactly the same string.
What I have also thought about is using the above loops as mentioned giving them input parameters as ASCII values by each turn and confirm it and then let the final output say the whole word DASH and that only when verified lets the LED blink.
Thank you in advance.
Upvotes: 0
Views: 613
Reputation: 21
So, upon carefully reading and understanding more about Arduino and serial monitor, I feel ready to say that the problem wasn't the code, in fact it was fine.
The problem was me and the serial monitor showing the output options. It should have been "No new line" and in my case it had always been "Newline" which is what made the string input given by the user add new line to the string entered increasing the length of the string.
Here is the code, and it works just fine given that the setting of the Serial Monitor output has been changed to "No line ending":
#include<string.h>
String s1;
String s2("Dhaval");
int LED_BUILTIN=2;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if (Serial.available()>0)
{
String s1=Serial.readString();
Serial.println("Enter 'Dhaval' to blink thy LED in hue of blue!");
Serial.println(s1);
if (s1.equals(s2)){
Serial.println("You have entered 'Dhaval', needless to say enjoy the beautiful baby blue LED.");
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}
else
{
Serial.println("Seems that you've entered anything else but Dhaval, please just enter 'Dhaval'.");
}
}
}
Hope it helps some day, to someone. And if you want to use "Newline" the string input will have an extra length than original intention in which case, you can just trim the string by the .trim function of the string. No issues.
Upvotes: 1