# Problem: Determine how much time a button is idle after releasing and sets flag

Just want to start things off by saying thanks to this community for sharing knowledge on working with the Arduino. I've learned quite a lot from looking thru topics and examples ever since I obtained an Arduino unit about a year ago.

Anyhow, I'm currently stuck on a problem with a project I'm working on and it deals with a button that's active low (when ON, it's connected to ground). What I'm trying to achieve is this:

1. When the button is released, I want to know how long it's idling for and if it's been idle for more than 2 seconds, I want to set a flag indicating this condition is met (true).

2. When the button is released and the idle time is less than 2 seconds or when the button is pressed, the flag will remain false.

I'm not sure what I'm doing wrong in the code, but it doesn't seem like it's executing the "updateState" call function. Can someone please help? It's much appreciated!

``````const int buttonPin = 5; //Active low input
int buttonState = 1;     // current state of the button
int lastButtonState = 1; // previous state of the button
int startPressed = 0;    // the moment the button was pressed
int idleTime = 0;        // how long the button was idle
boolean buttonflag = false;
boolean buttontrans = false;
unsigned long beginTimer = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {

pinMode(buttonPin, INPUT_PULLUP); // initialize the button pin as a input
beginTimer = millis();
Serial.begin(9600);        // initialize serial communication
}

void loop() {

if ((millis() - lastDebounceTime) > debounceDelay) {

if (buttonState == HIGH && lastButtonState == LOW && buttontrans == false) { // button state changed
buttontrans = true;
}
lastDebounceTime = millis();
lastButtonState = buttonState;
}
}

if (buttontrans == true && buttonState == HIGH) {
startPressed = millis();
idleTime = startPressed - beginTimer;

if (idleTime > 2000 && buttonflag == false) {
buttonflag = true;
Serial.println("Button was pressed for more than 2 seconds");
Serial.println("Buttonflag =  ");
Serial.println(buttonflag);
}
}
else {

buttonflag = false;
Serial.println("Button was not pressed for more than 2 seconds");
Serial.println("Buttonflag =  ");
Serial.println(buttonflag);
}
}
}
``````

The code below is based on Robin2's post #2 here , with the logic changed for what you're looking for. (The code there checks how long the button is held; you want to chack how long it's released.)

You are concerned with the button being released, ie pin high. So the thinking here is to keep checking for the opposite, that is low, and as long as it's low keep on zeroing the clock.

In the code below, the led displays the flag's status. It keeps the led off as long as the button is pressed, and turns it on 2s after release. I think that's what you wanted.

``````// https://forum.arduino.cc/index.php?topic=678547.0
// 19 april 2020

const byte button = 5;
const byte led = 8;
bool theFlag = false;
unsigned long  lastTimeButtonWasPressed;
int interval = 2000;
bool buttonState = false;

void setup()
{
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
Serial.println("setup() done");
} //setup

void loop()
{
if (buttonState == LOW)  // assumes LOW when pressed
{
lastTimeButtonWasPressed = millis();
theFlag = false;
}

if (millis() - lastTimeButtonWasPressed >= (unsigned long) interval)
{
theFlag = true;
}

digitalWrite(led, theFlag);
Serial.println(theFlag);
} //loop
``````

Thank you so much for your reply twinkleyan! I'll give this a shot! Thanks again! Much appreciated.

twinkleyan, after a few changes, it works perfectly! Thanks again!