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.
blink without delay
example sketchdelay()
is a blocking function. It can't be interrupted. You need to refactor your code as others have suggested.