Arduino Forum

Using Arduino => Programming Questions => Topic started by: AnonymooseYT on Aug 01, 2019, 01:00 pm

Title: Using the if statement with the Serial monitor
Post by: AnonymooseYT on Aug 01, 2019, 01:00 pm
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)  
  {  
   }    
}   
  

Title: Re: Using the if statement with the Serial monitor
Post by: J-M-L on Aug 01, 2019, 01:02 pm
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 (http://forum.arduino.cc/index.php?topic=396450.0) and Planning and Implementing a Program (http://forum.arduino.cc/index.php?topic=261445.0)


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)
Title: Re: Using the if statement with the Serial monitor
Post by: AnonymooseYT on Aug 01, 2019, 01:11 pm
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");
   
    }
}
 
Title: Re: Using the if statement with the Serial monitor
Post by: AnonymooseYT on Aug 01, 2019, 01:16 pm
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.
Title: Re: Using the if statement with the Serial monitor
Post by: groundFungus on Aug 01, 2019, 01:19 pm
Code: [Select]
if (myname="Max")
= for assignment, == for comparison.  The if is a comparison.
Title: Re: Using the if statement with the Serial monitor
Post by: AnonymooseYT on Aug 01, 2019, 01:22 pm
Then what should i use??
Title: Re: Using the if statement with the Serial monitor
Post by: meltDown on Aug 01, 2019, 01:24 pm
Then what should i use??

== like you did here:

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

Title: Re: Using the if statement with the Serial monitor
Post by: AnonymooseYT on Aug 01, 2019, 01:42 pm
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
Title: Re: Using the if statement with the Serial monitor
Post by: HellasT on Aug 01, 2019, 01:46 pm
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)
Title: Re: Using the if statement with the Serial monitor
Post by: HellasT on Aug 01, 2019, 01:48 pm
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.
Title: Re: Using the if statement with the Serial monitor
Post by: AnonymooseYT on Aug 01, 2019, 01:54 pm
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".
Title: Re: Using the if statement with the Serial monitor
Post by: meltDown on Aug 01, 2019, 02:02 pm
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");
  }
}
Title: Re: Using the if statement with the Serial monitor
Post by: sterretje on Aug 01, 2019, 02:04 pm
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.
Title: Re: Using the if statement with the Serial monitor
Post by: meltDown on Aug 01, 2019, 02:06 pm
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"...





Title: Re: Using the if statement with the Serial monitor
Post by: AnonymooseYT on Aug 01, 2019, 02:07 pm
Doesn't work for me i keep getting "wut"
Title: Re: Using the if statement with the Serial monitor
Post by: AnonymooseYT on Aug 01, 2019, 02:12 pm
Can you please show your example in code
Title: Re: Using the if statement with the Serial monitor
Post by: meltDown on Aug 01, 2019, 02:19 pm
Can you please show your example in code

Take your code from #2 and make the == change from #9.

Check the Line Ending in the bottom right of Serial Monitor is set to "No line ending"



Title: Re: Using the if statement with the Serial monitor
Post by: meltDown on Aug 01, 2019, 02:20 pm
Line ending....

(https://forum.arduino.cc/index.php?action=dlattach;topic=629357.0;attach=318717)

Title: Re: Using the if statement with the Serial monitor
Post by: AnonymooseYT on Aug 01, 2019, 02:20 pm
Thank you very much everyone i got it to work!!!!!