SERIAL COMMUNICATION

Hi! NEW TO ARDUINO!!

Please consider the following code:
int led1 = 13;
void setup()
{
Serial.begin(9600);
pinMode(led1,OUTPUT);
}
void loop()
{
while (Serial.available() == 0);
int val = Serial.read()-'0';
if(val==1)
{
Serial.println("LED 13 ON");
digitalWrite(led1,HIGH);
}
else if(val==0)
{
Serial.println("LED 13 OFF");
digitalWrite(led1,LOW);
}
else
{
Serial.println("INVALID!");
}

//Serial.println(val); // echo back
}

when i enter any value in the given (default) serial monitor of Arduino, i get the following result:

INVALID! INVALID!

Please advise. I thank you for your input in advance!

Probably a Carriage Return (CR, '\r') and Line Feed (LF, '\n') are transmitted if you press enter.
You can write the code to the serial monitor in hexadecimal notation, but you have to do that before the '0' is subtracted.

when you say "You can write the code to the serial monitor in hexadecimal notation, but you have to do that before the '0' is subtracted." you mean in the serial monitor i should enter the value in hexadecimal, without the subtraction of zero, right?
if so, i tried tat too. but then all the outputs in the serial monitor are "INVALID!".

Lets start with the simple stuff, first. In the Serial Monitor, what line ending do you have selected? I'm guessing that it is not none, so you are sending something like "0" and the cr and lf are what is invalid.

THANKS PaulS. I got it!! my stupid mistake. should have looked for it... thanks again!!

KRODAL YOU WERE RIGHT TOO! thanks for your reply!!