A problem whit if, I think so

I just started using Arduino and got stuck in a problem.
I'm trying to select a message to display.
I wrote everything in viod setup Just for my convenience, When everything works I want to move it to void loop.
I tried to use int and byte instead of char, but when I check the number it display me the number in Hexadecimal
(49,50,51 etc.) instead of 1,2,3.
I searched here on the site and online and did not find anything that would help me with the problem.
I'm using a mega board.
This is the code I use.

char number=1;
String message1 = "Hey";
String message2 = "Hello";
String message3 = "Whats up";



void setup()
{
  Serial.begin(9600);
  Serial.println("Enter a number Between 1-3");
  while (!Serial.available())
  {
  }
  number = Serial.read();
  Serial.print (number); // To check the variable
  if (number == 1) Serial.println (message1);
  if (number == 2) Serial.println (message2);
  if (number == 3) Serial.println (message3);

}
void loop()
{

}

Sorry if I have grammar issues, English is not my first language.
Thanks for the help,

if i see it right Serial.read doesn't return a "number" but a byte. does it work if you compare it to the characters instead of the literal numbers? does if(number=='1') ... work?

Just subtract ‘0’ from the variable called number.

@frozenrat Yes, it works. Thanks

A '1' from the keyboard is 49, the ASCII value of '1'

One way to fix it would be to subtract the ASCII for '0':

if (number-48 == 1) Serial.println(message1);
if (number-48 == 2) Serial.println(message2);
if (number-48 == 3) Serial.println(message3);

Alternately, subtract ASCII zero when you read the keyboard:

number = Serial.read()-48;

SteveMann:
A '1' from the keyboard is 49, the ASCII value of '1'

One way to fix it would be to subtract the ASCII for '0':

if (number-48 == 1) Serial.println(message1);

if (number-48 == 2) Serial.println(message2);
if (number-48 == 3) Serial.println(message3);




Alternately, subtract ASCII zero when you read the keyboard:


number = Serial.read()-48;

if we go that path it's always handy to have the ascii chart at hand

It is possible to read the number form serial (not in ascii)?

do you mean like Serial.parseInt?

shvada:
It is possible to read the number form serial (not in ascii)?

If you don’t send it as ASCII, it won’t be received as ASCII.

That’s hard to do if you’re using the serial monitor.

TheMemberFormerlyKnownAsAWOL:
If you don't send it as ASCII, it won't be received as ASCII.

That's hard to do if you're using the serial monitor.

does the serial monitor support escape characters?

frozenrat:
do you mean like Serial.parseInt?

I do not know this command, I'll see if it works

TheMemberFormerlyKnownAsAWOL:
If you don't send it as ASCII, it won't be received as ASCII.

That's hard to do if you're using the serial monitor.

so how can I make a changing message?

shvada:
I do not know this command, I'll see if it worksso how can I make a changing message?

either you program your own serial monitor / application that talks to your arduino or you use third party software. i chose the first one, no idea what's on the market as third party software.
this thread taught me i want to add binary/hex mode to my serial communication tool :smiley:

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