Detect if the button isn't pressed for 10s

Hello everyone,

So far I have a code allowing me to put and update temperature changes to EEPROM. There are 2 push buttons which are used to change the temp value (up or down depends on how many times I press), and I wanna save the new value to EEPROM only when the pushbuttons are not pressed for 10s.

Any ideas how to detect if the buttons are not pressed for 10s? Thank you!

Save the millis() value when the buttons become not pressed and set a boolean to true. Then, each time through loop() if the boolean is true test whether 10 seconds has elapsed. If so then save the value and set the boolean to false

The code below from Robin2's post here is probably what you need, and is slightly simpler (although perhaps not as intuitive) than UKHeliBob's approach above:

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {          // assumes LOW when pressed
   lastTimeButtonWasNotPressed = millis();
}

if (millis() - lastTimeButtonWasNotPressed >= interval) {
   // do stuff
}

It essentially keeps "zeroing" the timer if the button is not pressed.

Whilst the button remains not pressed lastTimeButtonWasNotPressed will be repeatedly updated to the current value of millis(). Will the test of whether the period has elapsed ever be true ?

meltDown:
It essentially keeps "zeroing" the timer if the button is not pressed.

I think in this case the OP needs the logic inverted - in other words the timer should be zeroed if the button IS pressed - so

buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {          // assumes LOW when pressed
   lastTimeButtonWasPressed = millis();
}

if (millis() - lastTimeButtonWasPressed >= interval) {
   // do stuff
}

...R

Yes my bad, in my head I was looking to time the press not the un-press.

The code met my thinking but my thinking was inverted :slight_smile:

Of course when saving data to the EEPROM the question remains if that should be done continuously or just once after a minimum of 10s release.

static bool buttonWasPressed=false;
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {          // assumes LOW when pressed
   lastTimeButtonWasPressed = millis();
   buttonWasPressed=true;
}

if ((millis() - lastTimeButtonWasPressed >= interval) && (buttonWasPressed)) {
  buttonWasPressed=false;
   // do stuff
}

It works, many thanks! Have a great day everyone :))