0

I am building something where a Raspberry Pi connects to a database to check if a value is right. If the value is right it sends a signal to an Arduino through Serial.write(). Then the Arduino reads this data and if the Arduino reads 1 it should send a HIGH signal trough pin 13.

This worked a few months ago, but when I tried it last week it only worked a few times. It seems as if the Arduino isn’t always able to read the signal the Pi sends. Most of the times it only works the first time after I make a change and then it seems like it doesn’t read the signals anymore.

Arduino code:

int n;

void setup(){
  pinMode(13,OUTPUT);
  pinMode(10,INPUT);
  Serial.begin(9600);
  n=7;
}

void loop(){
  if (Serial.available()){
    n = Serial.read() - '0';
  }
  if(n==1){
    digitalWrite(13,HIGH);
  }
  else{
    digitalWrite(13,LOW);
  }
  int rst = digitalRead(10);
  if(rst==HIGH){
     n=7;
  }
}

Raspberry Pi Python code which sends the signal via Serial.write ((arduinoSerialData.write()).

if strscan == barcode:
    print ('code valid')
    arduinoSerialData.write('1')
    break
else:
    print ('code invalid')
    arduinoSerialData.write('2')