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.
So you only look to read something when something is sent, but you send the prompt only after something is sent. Seem the wrong way round to me. This would be better.
first of all thanks a lot for the response, I did try this and yet it doesn't seem to work. The monitor directly displays the else part in the program.
I am trying to get to compare two strings in Arduino, one string which I know of, and the other string which I am getting from user from the Serial Monitor, in arduino. If both compare and are equal to each other it should display something otherwise it should show something else.
I am using module of esp32.
Here's the code, please let me know why when I enter through serial monitor "Dhaval" the output is still "Nah buddy" part of the code when it should be "That's right"
Thanks for replying, I did check, and your doubt is correct, the length of S2 containing Dhaval is 7 whereas the already defined s1 string Dhaval is 6. What it could mean? Does the serial monitor assign an extra random character to the string s2?
@naidardevil
You were asked to look this up in your first thread but you didn't. You started another thread. This is called cross posting and against the rules here. Repeated ignoring of this rule will lead to a ban.
Thank you very much for doing that. I actually didn't know that such was the rule, as I had just registered like yesterday.
I will be checking other rules. Very sorry if this has caused any problems, I just was curious to see insights from like minded people and upon trying various methods on my own and trying the suggested methods of these fellow goodfellas I decided maybe a separate string related post might help me close this faster.
i appreciate you understanding. Thank you very much.
Apologies for that. I didn't know. In my defense I did look up strings both as topics in arduino and c++ and tried them differently with newer method of coding and whatnot as well, I am still looking and would appreciate all the support. I just made this account yesterday and didn't know such were the rules. I humbly request you to cut me some slack as I am fairly new to this Mr. Mike.
I appreciate you letting me know, now I shall look forward to read the rules of this forum and what I can do to avoid breaking them. Thanks.
Same, I looked up strings in arduino and c++, how to compare them, how to maybe utilise the equalTo method, or as I had done look to use '==' operator as well. So far, as I test this, the string length shows to be 7 when the user inputs Dhaval as the string in the Serial monitor.
So, the problem was me. As I said, I am fairly new to Arduino, and upon trying this, changing the no new line it actually resolved it. I was so happy to know that the legacy code I wrote wasn't wrong at all, it was my lack of knowledge about Arduino.
Thanks a lot. The code works now and I have it fully updated with the Serial Monitor turned "No new line" and the code actually does what it was supposed to. I really appreciate everyone's help and I cannot thank you all enough for helping me understand and find solution to this.
For anyone's future reference, here is the full code:
#include<string.h>
String s1;
String s2("yourword");
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 'yourword' to blink thy LED in hue of blue!");
Serial.println(s1);
if (s1.equals(s2)){
Serial.println("You have entered 'yourword', 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 'yourword', please just enter 'yourword'.");
}
}
}