Debounce with timer help please

Hello all,

I was wondering if someone could please help me with this, I have spent hours trying to figure this out.

Basically I am trying to modify the default debounce code that comes with the arduino IDE to include a timer that switches off the ledPin after X amount of time if it is on. I want to retain the on off toggle function of the push-button.

Any help much appreciated.

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  
  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

antknight: Basically I am trying to modify the default debounce code that comes with the arduino IDE to include a timer that switches off the ledPin after X amount of time if it is on. I want to retain the on off toggle function of the push-button.

something like this?

EDIT: modified for internal pull=up resistor, you may have to invert your logic

#define ON_TIME 10000UL

const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

int ledState = HIGH;         // the current state of the output pin
//int buttonState;             // the current reading from the input pin
int lastButtonState;   // the previous reading from the input pin
unsigned long lastDebounceTime;  // the last time the output pin was toggled
unsigned long debounceDelay = 50UL;    // the debounce time; increase if the output flickers

void setup() 
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() 
{
  int reading = digitalRead(buttonPin);
  if (reading != lastButtonState && (millis() - lastDebounceTime > debounceDelay)) 
  {
    if (reading == HIGH) 
    {
      lastDebounceTime = millis();
      ledState = !ledState;
      digitalWrite(ledPin, ledState);
    }
  }
  lastButtonState = reading;
  if (ledState)
  {
    if (millis() - lastDebounceTime > ON_TIME)
    {
      digitalWrite(ledPin, LOW);
    }
  }   
}

Basically I am trying to modify the default debounce code that comes with the arduino IDE to include a timer that switches off the ledPin after X amount of time if it is on. I want to retain the on off toggle function of the push-button.

Do you know what debouncing means? It means ignoring transitions from pressed to not pressed, or from not pressed to pressed, that occur within some short interval after the initial transition.

That has NOTHING to do with turning a pin off some amount of time after the initial transition has been detected.

int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

Personally, I like names that look like they go together, like currButtonState and prevButtonState.

  int reading = digitalRead(buttonPin);

How many variables do you need to keep track of the current state and the previous state? Three is the wrong answer.

The debounce code you are starting from is crap.

You need to look at the state change detection example. Record when the transition of interest happens and turn the pin on. If another transition of the same kind happens less than x milliseconds later, ignore it.

Independently, check how long it has been since the transition of interest happened, if a transition of interest has happened. If one has happened, and enough time has passed since it happened, turn the pin off.

@BulldogLowell Wow, very concise solution. Thank you!

@PaulS Hmm the state change example doesn't include debounce, which I need.

Also, yes, you are correct. Why are there three variables to keep track of the button state? I can't get my head around that. Would you care to explain it? Or anyone else?

Thanks for the quick replies