Hai guys, i am trying to write a simple program which reads from the serial monitor and executes a function accoring to the given input. here's my program..
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()>0){
// int y = ;
switch(Serial.read()){
case 10 :
Serial.print("10");
break;
case 20:
Serial.print("20");
break;
case 220:
Serial.print("220");
break;
case 30:
Serial.print("30");
break;
default:
Serial.println("Nothing selected..!");
break;
}//end of switch
}
}
What you posted is probably a test.
But you can do that test a lot simpler.
If you just do a serial.println () (< place a variable or value between the braces), you will see what happens and probably why you aren't getting back what you are expecting.
The println adds a cr/lf so the next value will be printed at a new line (i think that is what you want anyway).
Ajay_Pyaraka:
Hai guys, i am trying to write a simple program which reads from the serial monitor and executes a function accoring to the given input.
It's impossible that you write the character 10 on monitor
if(Serial.available()>0){
// int y = ;
switch(Serial.read()){
case 10:
When you write on monitor, example 10 you not write value 10 but two char, '1' and after '0'.
Serial.read() read only one char at a time. '1' and after '0' or also 49 (ascii of '1') and 48 (ascii of '0')
If you write 'A' on monitor you send ascii value 65 ('A'=65) but ascii char 10 is not writable using keyboard.
The semantics of Enter versus Return in a Windows environment are application-specific and pretty inconsistent. The only way I know of to send a line-feed using the Arduino Serial monitor (without also sending other characters) is to select Newline as the line ending mode and then hit Enter or Return. That doesn't simply send the character you typed, it recognises the input character as being an end-of-line and sends the currently-configured end-of-line sequence.