Pages: [1]   Go Down
Author Topic: SERIAL COMMUNICATION  (Read 322 times)
0 Members and 1 Guest are viewing this topic.
Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

<EXPECTED ANSWER>
INVALID!
INVALID!

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

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!".
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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<cr><lf>" and the cr and lf are what is invalid.
Logged

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: