Why is this debounce not working (short code)

I have the debounce set for a rediculous 8 secs, just for debugging.

Anyways .. you should not be able to push the button any more than
once per every 8 secs.

But the little bugger is getting past it ...

I can push the button every sec and it accepts it ...

HOW ???

const int Request_Button = 4;      // this is the open and close button
unsigned long Previous_Time = 0;   // the time var's are for timing the opening
unsigned long Current_Time = 0;    // and closing making sure it isn't stuck
unsigned long Prev_Button_Time = 0;

bool Button_Was_Pressed = false;    // var for button condition
//******************************
void setup()
{
  pinMode(Request_Button, INPUT_PULLUP);       // pin 4
  Serial.begin(9600);
}

//****************************************************************

void loop()
{
  Check_For_Button();

  //Obey_The_Button();
  //Check_For_Open_Limit();
  //Check_For_Close_Limit();
  //Check_For_Stuck_Gate();
}

//**************************************************************

void Check_For_Button()
{
  Current_Time = millis();
  if (Current_Time - Prev_Button_Time >= 8000);    // shouldn't be able to get past here (quickly)
  {
    if (digitalRead(Request_Button) == LOW)
    {
      Button_Was_Pressed = true;
      Prev_Button_Time = millis();                                  // assign right now to prev time 
      Serial.print(" button was pushed ");
    }
  }
}

Here is a problem:

if (Current_Time - Prev_Button_Time >= 8000);

try this out :slight_smile:

const int Request_Button = 4;      // this is the open and close button
unsigned long Previous_Time = 0;   // the time var's are for timing the opening
unsigned long Current_Time = 0;    // and closing making sure it isn't stuck
unsigned long Prev_Button_Time = 0;

bool Button_Was_Pressed = false;    // var for button condition
//******************************
void setup()
{
  pinMode(Request_Button, INPUT_PULLUP);       // pin 4
  Serial.begin(9600);
}

//****************************************************************

void loop()
{
  // Check_For_Button();
  alt_Check_For_Button();
  //Obey_The_Button();
  //Check_For_Open_Limit();
  //Check_For_Close_Limit();
  //Check_For_Stuck_Gate();
}

//**************************************************************

void Check_For_Button()
{
  Current_Time = millis();
  if (Current_Time - Prev_Button_Time >= 8000);    // shouldn't be able to get past here (quickly)
  {
    if (digitalRead(Request_Button) == LOW)
    {
      Button_Was_Pressed = true;
      Prev_Button_Time = millis();                                  // assign right now to prev time
      Serial.println(" button was pushed ");
    }
  }
}

void alt_Check_For_Button() {
  static unsigned long AfterTimer;
  if (digitalRead(Request_Button) == LOW) {
    if ((millis() - AfterTimer) >= (100)) { // Debounce time = 100
      Serial.println("** button was pushed ");
    }
    _ATimer = millis(); // resets the timer while button is pressed prevents the delay timer from ever becoming true until button is released
  } 
}

Z

To add a hint to boolrules post, look very carefully at the very end of that code line ;;;;; :slight_smile:

; - )

Allllll right ... !!

That did it ... so glad it was a technical error and not a logic error.

Karma for everyone ..!!! drinks are on me.

:slight_smile:

MikeAmick:
I have the debounce set for a rediculous 8 secs, just for debugging.

Anyways .. you should not be able to push the button any more than
once per every 8 secs.

But the little bugger is getting past it ...

I can push the button every sec and it accepts it ...

Here's a debounce function that I use and it's rock solid:

// read a hardware button or switch. note that "pin" needs
// to be set as "pinMode (pin, INPUT_PULLUP);" and the button
// or switch pulls it to ground.
// returns 1 if button is pressed, 0 if not.

uint8_t getButton (int pin)
{
// adjust these to your needs. note that "debounce"
// must be greater than "threshold". 90% of "debounce"
// is a good value. Use 95 ... 99% for more stringent test.
// exec time is "debounce * 1 usec" plus small overhead.

#define DEBOUNCE 1000 // loop 1000 times (approx 1 msec)
#define THRESHOLD 900 // need at least 900 good reads

    uint16_t n = 0;
    uint16_t deb = DEBOUNCE;

    if (digitalRead (pin)) {
        return 0; // button up, return
    }
    // count "button pressed" state
    while (deb--) {
        n += digitalRead (pin) ? 0 : 1;
        __builtin_avr_delay_cycles ((F_CPU / 1e6) * 1); // 1 usec
    }
    // if "button pressed" state more than
    // "threshold" times, accept it
    return (n < THRESHOLD) ? 0 : 1;
}

this allows you to press the button as fast as you would like (unless you need to press it more than 10 times a second) but won't bounce.

void Check_For_Button() {
  static unsigned long AfterTimer;
  if (digitalRead(Request_Button) == LOW) {
    if ((millis() - AfterTimer) >= (100)) { // Debounce time = 100
      Serial.println("** button was pushed ");
    }
    _ATimer = millis(); // resets the timer while button is pressed prevents the delay timer from ever becoming true until button is released
  } 
}

Z