If-else statement by user input in serial monitor

Hello everyone, i try to do if-else statement by using a user input in serial monitor. but everytime i enter a number on serial monitor, the print is not what i code. anyone please help, thank you

 

char value0;

void setup (){

Serial.begin(9600);
delay(200);

Serial.println("Enter Number: ");

}

void loop()
{
if( Serial.available())
{
value0 = Serial.read();

if((value0 > '39') && (value0 < '151'))
{
Serial.println("Play song 1");
}
else
{
Serial.println("Next song");
}

}
}

  • I think you should write any character in a single quote like below

    if((value0 > '0') && (value0 < 'z'))
  • In reply to Okamiya Yuuki:

    z means any number or what? already single kot my number. still not what it should be.
  • In reply to Thwbh:

    '0' is 48.
    'z' is 122.
    The compiler convert from ascii code to number. See below for ascii code.
    https://theasciicode.com.ar/

    So '39' and '151' are wrong. When you specify a number, I think you should write a number without single quote.
  • In reply to Okamiya Yuuki:

    still same, when i enter number such as 152 which is supposed to be "next song" it display "Play song 1
    Play song 1
    Play song 1
    " already remove the single quote still same
  • In reply to Thwbh:

    If you specify "152", that means 49, 53 and 50 in order.
    It may be displayed "Next song", when you enter space, which is 32.
  • In reply to Okamiya Yuuki:

    What should I do, if I enter more that 151 then will display "Next song" instead 49, 53 and 50 in order. With single quote or not, both are not correct.
  • In reply to Thwbh:

    I give you a sample code. When you hit number and enter, serial monitor will display either.

    String command;

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

    void loop() {
    if(Serial.available()){
    command = Serial.readStringUntil('\n');
    int value = command.toInt();
    if((value > 39) && (value < 151)){
    Serial.println("Play song 1");
    }
    else{
    Serial.println("Next song");
    }
    }
    }
  • In reply to Okamiya Yuuki:

    Thank you so much Mr Okamiya, i didnt include
    command = Serial.readStringUntil('\n');
    int value = command.toInt();

    maybe this is makes it work. have a nice day. thank you for helping