I have created a device that allows my son to track time spent on activities and exchanges that time into a time he can use with his electronics (eTime).
I am having trouble coding the part of the code that handles the countdown from his earned eTime. As it is right now it can count down and give him a warning when his eTime has run out, but the process can't be interrupted.
I would like to have him be able to stop his eTime and use the remainder later. I tried putting the function in a while loop.
while(digitalRead(button_9)==LOW){
delay(60000);
avail_time -= 1;
if(avail_time<0){
playAlarm();
return;
}
This one registers the keypress but ignores the delay.
I have also tried using a for loop to count down and another to check every second for a keypress.
void useTime() {
for(int i=avail_time+1; i>0; i--){
displayTime(i);
for(int minute=60; minute>0; minute--){
if(digitalRead(button_9)==HIGH){
avail_time = i;
return;
}
delay(1000);
}
}
while(digitalRead(button_9)==LOW){
tone(buzzer, 2000);
}
noTone(buzzer);
avail_time=0;
displayOptions();
return;
}
That one does not register the keypress.
1 Answer 1
You can use the millis function to check the current 'time'. If you store this, and later call this function again, by subtracting you can see how much time has been passed. so instead of delay(500)
you use if (millis() - timeStamp >= 500) { ...
to continue your code.
As others said in comments, you also need to debounce to prevent multiple button triggers.
Edit: Debouncing
There is not a library for debouncing (at least not one on the Arduino site), but you can easily copy the needed code from the following sketch:
-
1I was able to use this solution. I have not implemented debounce yet but I'm looking into it. Is there a debounce library or do I need to implement it myself?Gabe Ruiz– Gabe Ruiz2020年02月05日 08:10:34 +00:00Commented Feb 5, 2020 at 8:10
-
I have updated my answer according to your question in the comment.Michel Keijzers– Michel Keijzers2020年02月05日 09:36:09 +00:00Commented Feb 5, 2020 at 9:36
Explore related questions
See similar questions with these tags.
blink without delay
example sketchdelay()
is a blocking function. It can't be interrupted. You need to refactor your code as others have suggested.