Issues with serial.print and if statements

I am trying to get the serial monitor to display certain messages depending on user input within the serial monitor using if statements. However, the serial monitor just keeps repeating the same statement for the first if condition completing ignoring my second one.

LiquidCrystal lcd(RS, EN, D4, D5, D6, D7); //Identify pins used on LCD
int myNumber;
String msg = "Please Enter Your Number";
String msg2 = "Your Number is: ";

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly: 
  Serial.println(msg);
  while (Serial.available()==0){
    lcd.setCursor (0,0);
    lcd.print ("Wait");
  }
  myNumber = Serial.parseInt();
  if (myNumber = '2'){
      Serial.println(msg2);
      Serial.println("Even");
      lcd.setCursor (0,0);
      lcd.print("Even");
    }
    else if (myNumber = '1'){
      Serial.println(msg2);
      Serial.println("Odd");
      lcd.setCursor (0,0);
      lcd.print("Odd");
    }
}

No matter what number I put into the serial monitor (0, 1 ,2 ,3 , 4, 5, 6, 7, 8, 9) I always get the same message back in the serial monitor. It always says "Your Number is: Even" and then goes back to saying "Please Enter Your Number". Can anyone tell me where I am going wrong?

Serial.parseInt() returns an integer (hence the name), so myNumber will never be equal to '2' (a char).

Also: "=" is for assignment, "==" for comparison! What's happening in if (myNumber = '2') is that the value '2' (a char !) is assigned to the variable myNumber (an integer). Funny enough this is always true. Welcome to C++ ... :wink:

Try this: if (myNumber == 2) etc.

Erik_Baas:
Serial.parseInt() returns an integer (hence the name), so myNumber will never be equal to '2' (a char).

Also: "=" is for assignment, "==" for comparison! What's happening in if (myNumber = '2') is that the value '2' (a char !) is assigned to the variable myNumber (an integer). Funny enough this is always true. Welcome to C++ ... :wink:

Try this: if (myNumber == 2) etc.

Ah I see, this definitely clears some things up. Thank you. As for the integer/char problem is there a way to assign one to the other or convert to make them work with each other?

hhijjk:
As for the integer/char problem is there a way to assign one to the other or convert to make them work with each other?

Can you rephrase that as it's not quite clear what you want to achieve or are asking?

'2' in ASCII is 50 in ASCII, so '2' - 48 equals 2 and this works for any single digit

For a non-blocking replacement for Serial.parseInt(); which rejects invalid inputs

See my Arduino Software Solutions for a simple but much more robust replacement that ignores invalid numbers.
readStringUntil_nonBlocking.ino reads in data from your IDE serial connection until newline found (set newline in the IDE monitor)
ArduinoStringToInt.ino takes that String and converts it to an integer, it will complain if you don't give it an integer

A complete example sketch for reading ints from the IDE monitor, with line ending set to newline is here
ReadToInt_String.ino

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