How to reset timer after if()

I'm building a smart water bottle which periodically reminds you to hydrate yourself.
When you haven't picked up your bottle for a while, the RGB light goes from green → yellow → red and after an hour of not drinking the buzzer goes off. Using an LDR sensor, the Arduino will know when you are holding your bottle and should stop counting up to activate the lights and buzzer and reset once you let go of your bottle again.
So far I'm able to make everything work except for the timer to reset; once you let go of your bottle and light hits the LDR sensor the alarms continue to go off.
I've tried using millis() and resetting the timer in the else().

//times are shortened to few seconds to see result quicker of course
const unsigned long alarm_1 = 3000;
const unsigned long alarm_2 = 5000;
const unsigned long alarm_3 = 7000;
const unsigned long alarm_4 = 10000;

uint32_t timer;

void setup () {
  delay(5);
  timer = millis();
  
}

void loop() {

  int ldrStatus = analogRead(ldrPin);

//green → yellow → red → buzzer
  if (ldrStatus >= 800) {
    if (millis() > alarm_1) {
      digitalWrite(ledRed, LOW);
      digitalWrite(ledBlue, LOW);
      digitalWrite(ledGreen, HIGH);
    }
    
    if (millis() > alarm_2) {
      digitalWrite(ledRed, HIGH);
      digitalWrite(ledBlue, LOW);
      digitalWrite(ledGreen, HIGH);
    }
    
    if (millis() > alarm_3) {
      digitalWrite(ledRed, HIGH);
      digitalWrite(ledBlue, LOW);
      digitalWrite(ledGreen, LOW);
    }
    
    if (millis() > alarm_4) {
      tone(buzzerPin, 100);
    }

  }
  else {
//when LDR sensor reads dark it should reset the millis() of the alarms
    timer = 0;

    noTone(buzzerPin);
    digitalWrite(ledRed, LOW);
    digitalWrite(ledGreen, LOW);
    digitalWrite(ledBlue, LOW);
  
  }
}

Could I get any help with which directions to take cause this either isn't ideal for this project or I must have made a mistake somewhere.
Thanks in advance!

@khadiii, your topic has been moved to a more suitable location on the forum.

1 Like

millis() is since the beginning of your program.

Generally to see if a time has elapsed, you use the difference between now ( millis() ), and the time from which you are measuring.

In setup, you

timer = millis();

that’s marking or storing or remembering, whatever, now.

Then you should look at the elapsed time, which is quite simply the current “now” minus when you started, like

if (millis() - timer > 800)…

or whatever.

Then, the secret part… move your mark up to the current, new idea of now

timer = millis();

to fix a new start of timing for subsequent test of elapsed time since. Now.

Try to work that into your if statements, and move the timer mark up at the end of you tests.

Not sure that’s all that is quite not right, but that is the idea your logic lacks just now at least.

HTH

a7

1 Like

It was the math behind it that has confused me it seems... It works perfectly now, thank you a7!

if (millis() - timer > alarm_1) {
      digitalWrite(ledRed, LOW);
      digitalWrite(ledBlue, LOW);
      digitalWrite(ledGreen, HIGH);
    }