How do I use Serial Monitor in Arduino to blink an LED using only the word "DASH" and nothing else?

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.

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.

Hi,

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.

It fails because you can't comparer strings like the above.
So look up how to comparer strings.

Sorry got the character number wrong, use

while(Serial.available()< 6) { } ;

Hi, thanks for reading first of all.

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"

String s1("Dhaval");

void setup(){
  Serial.begin(9600);

  
  String s1("Dhaval");
  String s2;
  Serial.println(".....Welcome I guess?......");
}

void loop(){
  while(Serial.available()){
    String s2=Serial.readString();

    if (s2!=s1){
      Serial.println("Nah buddy");
      Serial.println(s2);
    }
    else{
      Serial.println("That's right!");
    }
  }
}

I am very new to this, so any explanation would really help. How do I let both the strings to be equal? What steps should I take to make that happen?

As a test try printing the length of the String that you enter. Is it what you expect ?

What is the line ending set to in the Serial monitor ?

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?

No, it adds the line ending characters that you tell it to, hence my question about how you have that set

1 Like

Hi,

How is your serial monitor configured here?

SerialMonitor

1 Like

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

I have reported this to the moderators.

Topics merged.

@naidardevil please avoid the creation of parallel discussions in multiple topics. This can waste the time of the people trying to help you.

Thanks in advance for your cooperation.
Per

1 Like

Evidently you can. Because

String incomingString;

means that's not comparing strings.

I may need sleep, but many variations of

    if (incomingString=="Dhaval")...

worked in tests. String to String, String to "string", String to char *foo = "foo!"; &c.

I looked at the source code for Strings but my C++ can't see why it works. I think I see overloading of other operators.

a7

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.

1 Like

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.

Line ending is newline in the serial monitor. Although I have looked up and that doesn't look to create much of a difference in the output.

'Newline'

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'.");
      
    }
    
  }

}

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