Go Down

Topic: Using the if statement with the Serial monitor (Read 171 times) previous topic - next topic

AnonymooseYT

Hello everyone i am very new to Arduino so this code might be wrong. But i need help. I am trying to create a program where when i ask the serial monitor a question it gives me a answer but for some reason it doesn't work. I hope you can help me here is my code.
String myname = ""; 
  
void setup() {  
    Serial.begin(9600);  
   
}  
  
  
void loop() {  
    
Serial.println("Enter your name.");  
   
  while (Serial.available()==0)  
  {                                       //Wait for user input  
    
  }  
  myname = Serial.readString(); 
   
                     
   






    
  
 if (myname="Max")
 {
  Serial.println("hello");
  
  }
  else{
    
    Serial.println("wut");
    
    }
    while(Serial.available()==0)  
  {  
   }    
}   
  


J-M-L

#1
Aug 01, 2019, 01:02 pm Last Edit: Aug 01, 2019, 01:05 pm by J-M-L
Issue with = instead of == in your if test
Code: [Select]
if (myname="Max") // oops

You don't need the last while since the loop will loop so you'll go back to the top where you are actively waiting for input

You should read this Serial Input Basics and Planning and Implementing a Program


And of course you should read how to use the forum...

Please correct your post above and add code tags around your code:
[code] // your code is here [/code].

It should look like this:
Code: [Select]
// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

AnonymooseYT

#2
Aug 01, 2019, 01:11 pm Last Edit: Aug 01, 2019, 01:17 pm by AnonymooseYT
I didn't quite understand what you were trying to say. My apologies i am new so it might be difficult for me to understand.

Code: [Select]
String myname = "";
 
void setup() { 
    Serial.begin(9600); 
   

 
 
void loop() { 
   
Serial.println("Enter your name."); 
   
  while (Serial.available()==0) 
  {                                       //Wait for user input 
   
  } 
  myname = Serial.readString();
   
                     
   






   
 
 if (myname="Max")
 {
  Serial.println("hello");
 
  }
  else{
   
    Serial.println("wut");
   
    }
}
 

AnonymooseYT

Also i removed the last while loop it did not do any changes. I want the serial monitor only to respond back to me when I type max in it. But it responds even if i don't type max. If you don't understand run the code for your self.

groundFungus

Code: [Select]
if (myname="Max")
= for assignment, == for comparison.  The if is a comparison.

AnonymooseYT


meltDown

Then what should i use??

== like you did here:

Code: [Select]
while (Serial.available()==0) 


AnonymooseYT

Bro just make it easy for me and give the code edited and explain it to me. The way you guys are explain is really confusing

HellasT

#8
Aug 01, 2019, 01:46 pm Last Edit: Aug 01, 2019, 01:51 pm by HellasT
Also when it comes to Strings it is good practice to use the reserve operation.
For example in setup
myname.reserve(10);

And give it enough space to accept the longest pissible input from the serial port by changung the number (10)

HellasT

#9
Aug 01, 2019, 01:48 pm Last Edit: Aug 01, 2019, 01:50 pm by HellasT
I didn't quite understand what you were trying to say. My apologies i am new so it might be difficult for me to understand.

Code: [Select]
String myname = "";
  
void setup() {  
    Serial.begin(9600);  
  
}  
  
  
void loop() {  
    
Serial.println("Enter your name.");  
  
  while (Serial.available()==0)  
  {                                       //Wait for user input  
    
  }  
  myname = Serial.readString();
  
                    
  






    
  
 if (myname="Max")
 {
  Serial.println("hello");
  
  }
  else{
    
    Serial.println("wut");
    
    }
}
  


Replace this

if (myname="Max")


With this


if (myname=="Max")

P.S.

Being a little more polite wont hurt. Remember that noone is obliged to help. We all do it to help each other from hart.

AnonymooseYT

This only enables the else statement. Bro i want that when i type max in the serial monitor i get the reply "Hello" and if i type anything besides Max i get the reply "Wut". But when i type Max in the serial monitor i get the reply "Wut".

meltDown

This works for me: ie, I got hello when I pretended  to be Max. It's your code from #2 with the = replaced with == as explained.

(Format tidied up a bit)

Code: [Select]

String myname = "";

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

void loop() {

  Serial.println("Enter your name.");

  while (Serial.available() == 0)
  { //Wait for user input

  }
  myname = Serial.readString();

  if (myname == "Max")
  {
    Serial.println("hello");
  }
  else
  {
    Serial.println("wut");
  }
}

sterretje

#12
Aug 01, 2019, 02:04 pm Last Edit: Aug 01, 2019, 02:05 pm by sterretje
What is the line-ending in serial monitor set to?

It's somewhere in the right bottom. I guess it's set to carriage return and line feed

"Max" is not the same as "Max\r\n".

Change the line ending to none.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

meltDown

What is the line-ending in serial monitor set to?
Good call: my test which worked was with it set to none. When I set it to either or both cr or lf, I get wut as expected.

Nothing like a "tight bottom"...






AnonymooseYT

Doesn't work for me i keep getting "wut"

Go Up