Need Assistance Adding a Timeout, Please

Please excuse me for being a beginner but I need assistance with adding a timeout before a function.
In line 22 I need to add a 5-minute timeout before relay2 low

Any assistance would be much appreciated

Thank you

1 const int analogPin = A0;
2 const int relay1 = 8;
3 const int relay2 = 9;
4 const int threshold = 200;
5
6 void setup() {
7 pinMode(relay1, OUTPUT);
8 pinMode(relay2, OUTPUT);
9 Serial.begin(9600);
10 }
11
12 void loop() {
13
14 int analogValue = analogRead(analogPin);
15 digitalWrite(relay1, HIGH);
16 digitalWrite(relay2, HIGH);
17
18 if (analogValue > threshold) {
19 digitalWrite(relay1, LOW);
20 }
21 else {
22 // NEED TO ADD 5 MINUTE DELAY BEFORE RELAY2 LOW
23 digitalWrite(relay2, LOW);
24 }
25
26 Serial.println(analogValue);
27 delay(1000);
28 }

You will have to express better what your requirements are. What needs to happen if you reach line 22 but somewhere along the line you get to line 19 again?

Anyway, Using millis() for timing. A beginners guide might get you on the way.

I have a photocell detecting light levels and when the level is above the 200 threshold the motor turns clockwise. When below the threshold, the motor turns counterclockwise but I need to wait five-minutes before turning the motor counterclockwise.

Something I didn’t think about when I first asked, I want to continue the delay(1000) iterations while the 5-minute timeout is counting down.

Thanks again for any guidance

see attached wiring diagram

Please don’t post code with line numbers as it cannot be compiled.

To make it easy for people to help you please use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

Also please use the AutoFormat tool to indent your code for easier reading.

In general it is not a good idea to use delay() as it blocks the Arduino from doing other things. As well as the link given by @sterretje the demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

…R

const int analogPin = A0;
const int relay1 = 8;
const int relay2 = 9;
const int threshold = 200;

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

void loop() {

  int analogValue = analogRead(analogPin);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);

  if (analogValue > threshold) {
    digitalWrite(relay1, LOW);
  } 
  else {
    // NEED TO ADD 5 MINUTE DELAY BEFORE RELAY2 LOW
    digitalWrite(relay2, LOW);
  }

  Serial.println(analogValue);
  delay(1000);
}

What you still did not explain is what needs to happen when the level goes above the threshold again while 'waiting'.

Retribution:
I have a photocell detecting light levels and when the level is above the 200 threshold the motor turns clockwise. When below the threshold, the motor turns counterclockwise but I need to wait five-minutes before turning the motor counterclockwise.

boolean is_above_200 = false;
boolean is_clockwise = true;
uint32_t below_200_ms;

loop() {
  boolean was_above_200 = is_above_200;
  is_above_200 = alalogRead(pin) > 200;

  if(is_above_200) {
    if(!is_clockwise) {
      turn the motor clockwise
      is_clockwise = true;
    }
  }
  else {
    if(was_above_200) {
      below_200_ms = millis();  
    }

    if(is_clockwise && millis() - below_200_ms >= 5L * 60L * 1000L) {
      turn the motor counterclockwise
      is_clockwise = false;
    } 
  }
}

Something I didn’t think about when I first asked, I want to continue the delay(1000) iterations while the 5-minute timeout is counting down.

Thanks again for any guidance

Hmm.

boolean is_above_200 = false;
boolean is_clockwise = true;
uint32_t below_200_ms;
uint32_t countdown_ms;

loop() {
  boolean was_above_200 = is_above_200;
  is_above_200 = alalogRead(pin) > 200;

  if(is_above_200) {
    if(!is_clockwise) {
      turn the motor clockwise
      is_clockwise = true;
    }
  }
  else {
    if(was_above_200) {
      below_200_ms = millis();  
      countdown_ms = millis();
      Serial.println("starting countdown!");
    }

    if(is_clockwise && millis() - countdown_ms >= 1000) {
      Serial.println("counting down");
       countdown_ms += 1000;
    }

    if(is_clockwise && millis() - below_200_ms >= 5L * 60L * 1000L) {
      turn the motor counterclockwise
      is_clockwise = false;
    } 
  }
}