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;
}
}
}