Strange delay in my serial code?

I’ve got a set-up using two arduinos with apc220 transceivers; one arduino has a photo-diode which is used as a touch button and the other arduino has an led; when I cover the photo-diode on one arduino it sends a signal to the other via serial through the APC transceiver which interrupts a while loop for a set period of time turning off the led.

However the led is switching off for a lot longer on the receiver than the set delay time; I have a feeling it will have something to do with a backlog of characters coming through the serial but I can’t be certain.
If that is the case, is there a way to flush the serial and if not does anyone have any ideas on what might be causing the issue?

Also I appreciate that this isn’t the ideal code for achieving the above however I have a few projects that need this setup and I want to get this problem fixed on a simple prototype setup before pushing ahead with the real thing.

Many thanks for any suggestions.

Transmitter code here

const int analogInPin = A0; 
int sensorValue = 0;     
int led = 13;

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(analogInPin);            
  
  if(sensorValue < 400){
    Serial.write("1"); 
    digitalWrite(led, HIGH);
}
  else{ 
    Serial.write("0");
    digitalWrite(led, LOW);
  }
                     
}

Receiver code here.

int micropause = 200;
int led = 13;

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop() {

while(1 == 1){
    char c = 0;

    digitalWrite(led, HIGH);
    c = Serial.read();
    if(c == '1'){ break; }
    delayMicroseconds(micropause);
    
    digitalWrite(led, HIGH);
    c = Serial.read();
    if(c == '1'){ break; }
    delayMicroseconds(micropause);
    
    digitalWrite(led, HIGH);
    c = Serial.read();
    if(c == '1'){ break; }
    delayMicroseconds(micropause); 
    }

digitalWrite(led, LOW);
char c = 0;
delay(1000);
}

You should send ONE value when the reading goes over the threshold (is now, but was not, over the threshold) and ONE value when the reading goes below the threshold, rather than sending values when the reading IS over, or under, the threshold.

Of course, that means that you have to keep track of the previous reading.

Nice one, many thanks, that fixed it!

You should also not attempt to read a character from the Serial on the receiving device, unless there is actually a character there to be read.

How come? Wouldn't it be the case that if there's nothing there it'll just skip over it?

Wouldn't it be the case that if there's nothing there it'll just skip over it?

Three pronouns in one sentence without referents. That has to be some kind of record.

Want to try again using nouns this time?