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!