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