Simple question on if else statement

Hi all,

I am sorry for this simple question. I am still learning Arduino code.

Currently I am doing this following code; to read input from user for the char of m (which refer to minute) or s (second).

Serial.println("Is it in min or sec? (m/s)");
    while (Serial.available() == 0);
    {
      char i = Serial.parseFloat();
      
      if (i = 'm') 
      {
        Serial.print("ts = ");
        Serial.print(h, DEC);  //h refers to the input int written by user previously
        Serial.println(" min");
      }
      else if (i = 's') 
      {
         Serial.print("ts = ");
         Serial.print(h, DEC);
         Serial.println(" sec");
       }
      else
      {
        Serial.println("Error!!");
      }
    }

When I run the code, and click serial monitor, here is the result: - if user enters 'm', it will display min. - if user enters 's', it will still display min. - if user enters other than m/s, it still displaying min.

but what I want the code to do is - whenever user enters 'm' it will show min - whenever user enters 's' it will show sec - whenever user enters other characters beside 'm' or 's', the error warning will display and the code will ask again the user to input m/s.

Thank you in advance for your help and kindness.

 while (Serial.available() == 0);
    {

Why the braces?

if (i = 's')

==

if (i = 'm') will give the value m to i, it will not compare i with m.

Comparison Operator

x == y (x is equal to y)

else if (i == 'm')

luxxtek: if (i = 'm') will give the value m to i, it will not compare i with m.

Comparison Operator

x == y (x is equal to y)

else if (i == 'm')

I've done as you suggested, but the result displayed when I insert 'm', 's' or other characters than both alphabets mentioned is still 'Error!!'. Is that the correct way to write the if else statement?

pretty sure this is wrong; char i = Serial.parseFloat(); you want a character, not a float

try Serial.read();

427v8: pretty sure this is wrong; char i = Serial.parseFloat(); you want a character, not a float

try Serial.read();

Thank youuuuuu :) you help me lot. May God bless you :)