Temporized sensor

Hi!
I'm working on a project where I have to do the following:

An object is passing throught a photo interrupter sensor and 5 seconds later is passing another photo interrupter, both connected to Arduino.

A LED must be turned on if the object spends more than 5 seconds on passing the second sensor after passing the first sensor. What could I do?

did you even attempt to write a sketch?

Use millis() to record the time of the first passing into a variable (unsigned long) startTime . When the second sensor records the passing, subtract startTime from millis(). That give the elapsed time between passing. Is it more than 5000? Yes, turn on LED.

Hello again, thank you for your feedback. I've written this code, but I think it could be done better:

int sensorValueA0 = 0;
int sensorValueA1 = 0; 
unsigned long time1 = 0;
unsigned long time2 = 0;
unsigned long time3 = 0;
int control = 0;

void setup() {
  Serial.begin(9600); 
}

void loop() {
  
time3 = time2 - time1;  
if  (sensorValueA0 < 70 && control == 0)
{
  time1=millis();
  control=1;
}
else if (sensorValueA1 < 70 && control == 1)
{
  time2=millis();
  control=0;
}
else
{
}
if (time3 > 5000)
{
  Serial.println("LEDon");
}
else
{
  Serial.println("LEDon");
}
}
if  (sensorValueA0 < 70 && control == 0)

0 is less than 70, always. No real point in testing that.

Not. It is a photo interrupter sensor which is always giving positive signal (about 300 bits). When an object interrupts it, it gives 0.

Not. It is a photo interrupter sensor which is always giving positive signal (about 300 bits). When an object interrupts it, it gives 0.

Really?

int sensorValueA0 = 0;

Where do you assign a different value?

Hint, read up on state machines - you need a state to represent when an object
is between sensors, and a time-out for when it is over-due at the 2nd sensor.