I can't figure out why the next script is not working. I have attached the Arduino to one USB port of the raspberry pi, in which I've installed pyserial. I just want to turn off/on a led depending of the number I write on the Raspi console (1-> on, 2-> off).
int led=13;
void setup(){
Serial.begin(115200);
pinMode(led,OUTPUT);
}
void loop(){
if(Serial.available()){
mode(Serial.read() - '0');
}
}
void mode(int n){
if (n== 1){
digitalWrite(led,HIGH);
}
if (n== 2){
digitalWrite(led,LOW);
}
}
PS: I double checked and the baudrate is configured also at 115200 at the raspi side. In addition, if I place digitalWrite(led,HIGH) just before the line "mode(Serial.read() - '0');" the led turn on when I send some information from the raspi shell (so, works well).
1 Answer 1
Finally I've found the error, it was the serial communication baud rate: 115200. It seems that at higher speeds it not works properly, lowering it to 9600 do the trick.
-
1Have you also tried other rates that worked (or not) between 9600 and 115200 bauds?jfpoilpret– jfpoilpret2014年05月03日 11:55:55 +00:00Commented May 3, 2014 at 11:55
-
That is weird. Anyone know why that's happening?asheeshr– asheeshr2014年05月03日 14:18:06 +00:00Commented May 3, 2014 at 14:18