debouncing pushbutton, so frustrating

I've been trying to debounce a pushbutton for 1 hour and half without getting the best result. What i want the code do to:

Press the button, jump into a while function, press again, jump out of the while function, simple as that (both pushes sensitive to the possitive edge triggering). Mine is triggering at the negative edge of the push first time and at the possitive push second time. Here it's what i've done:

buttonState = digitalRead(dateButtonPin);
  if (buttonState != lastButtonState)
    {
    if (buttonState == HIGH && millis() - lastEvent >= interval)
      {
        x=1;
        lastEvent = millis();
      }
      lastButtonState=buttonState;  
      if(millis() - lastEvent >= interval)
      {
        displayDate();
      }       
    }

 displayDate()
{
while(x==1)
{

 unimportant part of code here...
...
...
...

           if (digitalRead(dateButtonPin) == HIGH)
          {
          x=0;          
          lastEvent=millis();
          }     

}
}

Maybe you could help me out with a better piece of code, simpler for debouncing a pushbutton in and out a loop function. Thank you!

have you tried using an interrupt by connecting the switch to pin-D2 and use attachInterrumpt using "Rising" or "Falling" arguments for the statement.

http://arduino.cc/en/Reference/attachInterrupt

i like the interrupt method but you can do it this way if you do not use any blocking code (e.g. delay() or while() loops) in your code blocks identified as " do state stuff..."

untested, but should be OK:

int dateButtonPin = 2;

int lastButtonState;
unsigned long lastEvent;
unsigned long interval = 50UL;
int state;

void setup()
{
  pinMode(dateButtonPin, INPUT);
}

void loop()
{
  int buttonState = digitalRead(dateButtonPin);
  if (buttonState != lastButtonState)
  {
    if (buttonState == HIGH && millis() - lastEvent >= interval)
    {
      state = 1 - state;
      lastEvent = millis();
    }       
  }
  lastButtonState = buttonState;
  if (state)
  {
    // do state == 1 stuff
  }
  else
  {
    // do state == zero stuff
  }
}
buttonState = digitalRead(dateButtonPin);
  if (buttonState != lastButtonState)
    {
    if (buttonState == HIGH && millis() - lastEvent >= interval)
      {
        x=1;
        lastEvent = millis();
      }
      lastButtonState=buttonState;  
      if(millis() - lastEvent >= interval)
      {
        displayDate();
      }       
    }

displayDate() isn't conditional upon either edge. Because the positive edge resets the debounce time, displayDate() won't be fired on the positive edge. It will, however, be fired on the negative edge.

You need to have it enter the loop on the positive edge, and only exit the while loop if you detect another positive edge.

I have some debouncing code here:

http://www.gammon.com.au/switches

alexmath: I've been trying to debounce a pushbutton for 1 hour and half without getting the best result. What i want the code do to:

Can you write a version of your code that makes no attempt at debouncing and post the whole program.

In my personal experience there is seldom a need for explicit debounce code but it is impossible to give advice without seeing the full program in simple form.

I usually store the state of a switch IF the switch is pressed and then reset the state variable at the end of the called function.

If you want the button to exit a WHILE loop before it completes it may be that a WHILE loop is not the appropriate solution. You could, perhaps, reset the switch state vaariable after one iteration of the WHILE loop and next time the switch is pressed update the value being tested by WHILE to exceed the terminating value. But I suspect you would be better with a different design.

...R

BulldogLowell's code is perfect! Thank you so much for your help. Have a nice day. :)