Triggering if a value has been low for 1 second

Hello all,
I’ve been working on a code that uses a Pixy camera to move an eye on a gimble and track a target. When it loses the target, it returns the eye to the center looking forward. The problem I’m having is that the Pixy doesn’t detect continuously and therefore has false negatives between each detection. (see picture attached the of serial monitor showing several false negatives between each detection)

In order to avoid the eye attempting to return during these false negatives, I’m wanting to include a timer so that the eye only tries to return to the center after the target has been lost for 1 second. I’ve been trying to use millis() to act as a timer, but the closest I’ve gotten is the eye tracking the target and trying to return to center every 1 second.
Any suggestions? Thanks in advance.

pixy_and_eye_xy_return_test.ino (1.72 KB)

Every time you get a positive, save millis. Then you need one if statement in your code to see if it has been 1 second since you had the last positive. If you haven’t had a positive in over 1 second then … well there you go.

Here’s a simple example that will light an LED when a button has been not pressed for 1 second or more. See if you can understand how it works. Notice I didn’t say copy it into your code because this code won’t work with your code. I did that on purpose. Don’t copy it. Try to understand it. Once you do, then you will understand what you need to do in your code.

const byte buttonPin = 6;
const byte ledPin = 13;
unsigned long lastPress = 0;

void setup(){
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}


void loop(){
   // if button is pressed
  if(digitalRead(buttonPin) == LOW){
    // save the time
    lastPress = millis();     
  }
  //  it's been a second or more since the button was last pressed
  if(millis() - lastPress >= 1000){
    // turn on the LED
    digitalWrite(ledPin, HIGH);
  }  
  else {   // button has been pressed at some point in last second
    digitalWrite(ledPin, LOW);
  }
}