Try this
#define LED 13
int val =0;
void setup(){
Serial.begin(9600);
pinMode (LED, OUTPUT);
}
void loop (){
while (Serial.available() ==0 ); // Arduino waits to recieve data
int val = Serial.read()-('0');
if (val ==1 ) {
digitalWrite (LED, HIGH);
Serial.println ("ON");
}
else if (val == 0){
digitalWrite (LED, LOW);
Serial.println ("OFF");
}else{
Serial.println ("Invalid!");
}
Serial.println(val);
Serial.flush();
}
The important bit to note is
int val = Serial.read()-('0')What you need to remember is that when you type 1 into the serial monitor it isnt sending an int, its sending a char. If you look at an ascii table you'll see that the char "1" is equal to 49 in decimal. So in your sketch when your sending 1 your actually sending it 49, which is why your code doesnt turn the led on. The way to get around this is to -('0'-) from your serial.read. This subtracts the value of the character 0 (48) to give you your desired number.
so when you input 1 for example this will happen.
49 - 48 = 1.
Hope this helps.
happy to answer any questions you have about it.