Sending number over serial to arduino sending something different?

1

I have an arduino setup that turns on a light when it receives a 1, and turns off a light when it receives a 0. I don't think there is anything wrong with the code. I can send the Arduino a 1 with echo 1 > /dev/ttyACM0 and the light blinks, which confused me. Then I sent it 'h' and it did the same thing. I even opened a screen with screen /dev/ttyACM0 and typed in 1, and it blinked. I would think echoing 1 would echo 1. I have also used the following command stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts Am I doing anything wrong here? If you need any more information at all, just ask.

Arduino code (I don't think this is flawed):

void setup() {
Serial.begin(9600);
//set the LED pin to OUTPUT
pinMode(13, OUTPUT);
}

void loop() {
//wait until the serial connection is open
while (Serial.available() ==0);

//read from the serial connection; the - '0' is to cast the values as the int and not the ASCII code
int val = Serial.read() - '0';

//print to the console for testing
Serial.println(val);

//if we've recieved a '1', turn on the LED and print a message
if(val==1){
Serial.println("Received a 1");
digitalWrite(13, HIGH);
}
//if we've recieved a '0', turn off the LED and print a message
if(val==0){
Serial.println("Received a 0");
digitalWrite(13, LOW);
}
}

Derp

Posted 2013-09-08T19:30:32.000

Reputation: 11

2This might fall outside of the scope of Superuser so please be aware that this might not be answered. I know the Ardunio beta was closed due to inactivity so I would consider getting this moved to [so] and reviewing [ask] – 50-3 – 2013-09-08T20:36:27.970

Its not really a coding question. Its more of a commands question, which I might not have asked clearly. The only reason I posted the arduino code was for someone that was interested in what the arduino was doing when I sent it the 1. I fixed it anyway, I'm going to post an answer here in a second. – Derp – 2013-09-08T23:56:18.730

If you run it within the Arduino program and interact at the built-in serial console, does it work, and print out your "Received a 1" messages? I would be suspicious first of the method of sending the serial signal. (I've also seen the Serial.available test written as <= 0 instead of == 0) – beroe – 2013-09-09T18:49:30.543

Answers

0

First try blinking the led without the need to read serial.. if this works that means the board is fine and the led works. Next, try using a Python script that uses pyserial and write the data to the com Port. There are tutorials online on how to do this. ( Make sure you use the right com Port, use the command ls /dev/tty*[for Linux] in terminal to see the right com Port). Hope this works.

Rohan

Posted 2013-09-08T19:30:32.000

Reputation: 33