What is wrong in button debounce

hi All,

i trying to add button debounce and am not getting anywhere at it.

switch (select)
    {
        unsigned long buttonPressTimeStamp;
        unsigned long buttonPressTime;
        byte prevState;
      case 1: //Start
        clearSelectPointer(1);
        lcd.setCursor(0, 1);
        lcd.print("*");
        resetFlag = true;
        buttonState = digitalRead(buttonPin);
        if (buttonState == LOW && prevState == HIGH)
        {
          delay(20);
          buttonState = digitalRead(buttonPin);
          if (buttonState == LOW && prevState == HIGH)
          {
            buttonPressTimeStamp = millis();
          }
        }
        if (buttonState == HIGH && prevState == LOW)
        {
          delay(20);
          buttonState = digitalRead(buttonPin);
          if (buttonState == HIGH && prevState == LOW)
          {
            buttonPressTime = millis() - buttonPressTimeStamp;
          }
        }
        if (buttonPressTime >= preTime)
        {
          inConfigFlag = false; // exit config mode
          InMenu = true;       // enter submenu
          stayInMenu = 1;     // go to submenu in InMenu 1
          lcd.clear();
        }
        resetFlag = false;
        break;

When press 'buttonPin' the event is very fast and i need to release it quite fast to do what i intent to do. the above is part of a Menu structure.

          delay(20);

Yes, the stick-your-head-in-the-sand method of debouncing. It works. For many programs it works well enough that you don't need to do any more. Why do you need to measure all those times then?

Can you please show a working program which will compile? There's a number of issues which may or may not be problems, depending on the code around this.

Remember that you showed us the code which you think you don't understand. There's other code that you don't even know that you don't understand and that could be where the problem is.

here is the link to the code.

Please attach your code here. A lot of people will not download from external sources.

And from your description, you need to implement a statechange detection; you want to know when the button went from ‘not pressed’ to ‘pressed’, not if the button ‘is pressed’.

I know the state change and that is what i am trying to implement but failing. here is the second attempt but not working

buttonState = digitalRead(buttonPin);
        if (buttonState == LOW && prevButtonState == HIGH)
        {
          delay(20);
          if (buttonState == LOW)
          {
            btnPressedTimeStamp = millis();
          }
        }
        buttonState = digitalRead(buttonPin);
        if (buttonState == HIGH && prevButtonState == LOW)
        {
          btnReleasedTimeStamp = millis();
        }
        if ((btnReleasedTimeStamp - btnPressedTimeStamp) > 50)
        {
          btnPressedTimeStamp = 0;
          btnReleasedTimeStamp =0;
          prevButtonState = buttonState;
          resetFlag = true;
          inConfigFlag = false; // exit config mode
          InMenu = true;       // enter submenu
          stayInMenu = 1;     // go to submenu in InMenu 1
          resetFlag = false;
          lcd.clear();
        }

My orginal code is too big to be attached here hence linked it. Wasn't that obvious ?

well i’ve managed to reach this far but not what i expected.

while (inConfigFlag == true) // stay in config mode and wait for selector
  {
    clearSelectPointer(1);
    lcd.setCursor(0, 1);
    lcd.print("*");
    int select = map(analogRead(Pot), 0, 1023, 1, 6);
    switch (select)
    {
      case 1: //Start
        btnState = digitalRead(buttonPin);
        if (btnState != prevBtnState)
        {
          if (btnState == LOW)
          {

          }
          if (btnState == HIGH)
          {
            prevBtnState = btnState;
            resetFlag = true;
            inConfigFlag = false; // exit config mode
            InMenu = true;       // enter submenu
            stayInMenu = 1;     // go to submenu in InMenu 1
            resetFlag = false;
            lcd.clear();
          }
        }
        break;

then when that switch is true it goes to this

while (stayInMenu == 1) //Start
  {
    prevBtnState = btnState;
    int startSrv = map(analogRead(ServoPot2), 0, 1023, 0, 130);
    if  (startSrv < 25)
    {
      startSrv = 0;
    }
    lcd.setCursor(0, 0);
    lcd.print("Set Start Point  ");
    lcd.setCursor(1, 1);
    lcd.print("Set Start:");
    lcd.print(startSrv);
    lcd.print(char(223));
    lcd.print("  ");
    lcd.setCursor(1, 2);
    lcd.print("Stored   :");
    lcd.print(SrvPotMap2);
    lcd.print(char(223));
    myServo.attach(servoPin);
    myServo.write(startSrv);
    resetFlag = true;
    buttonState = digitalRead(buttonPin);
    if ((buttonState == LOW) && SrvPotMap2 != startSrv)     // Display message only if current value is different from the stored value
    {
      delay(200);
      buttonState = digitalRead(buttonPin);
      if ((buttonState == LOW) && SrvPotMap2 != startSrv)
      {
        SrvPotMap2 = startSrv;
        EEPROM.update(startSrvAddr, startSrv);
        myServo.detach();
        lcd.clear();
        lcd.setCursor(2, 2);
        lcd.print("SAVE SUCCESSFULL");
        lcd.setCursor(0, 3);
        lcd.print("value changed: ");
        lcd.print(startSrv);
        lcd.print(char(223));
        resetFlag = false;
        inConfigFlag = false;
        stayInMenu = 0;
        InMenu = 0;
        //saveSuccess();
        lcd.clear();
        lcd.setCursor(1, 0);
        lcd.print("CONFIGURATION MODE");
        configMenu();
      }
    }
    else if (buttonState == LOW)
    {
      delay(20);
      if (buttonState == LOW)
      {
        delay(20);
        buttonState = digitalRead(buttonPin);
        if (buttonState == HIGH)
        {
          myServo.detach();
          inConfigFlag = true;
          InMenu = false;
          stayInMenu = 0;
          configMenu();
        }
      }
    }
  }

but problem is as soon i enter into the first switch it just jumps to the second and after several button press it goes to the first switch and then goes no where after pressing the button.

Quick update, i've managed to get the button function working now

while (inConfigFlag == true) // stay in config mode and wait for selector
  {
    clearSelectPointer(1);
    lcd.setCursor(0, 1);
    lcd.print("*");
    int select = map(analogRead(Pot), 0, 1023, 1, 6);
    switch (select)
    {
      case 1: //Start
        btnState = digitalRead(buttonPin);
        if (btnState == HIGH && prevBtnState == LOW)
        {
          delay(50);
          btnState = digitalRead(buttonPin);
          if (btnState == HIGH && prevBtnState == LOW)
          {
            prevBtnState = btnState;
            resetFlag = true;
            inConfigFlag = false; // exit config mode
            InMenu = true;       // enter submenu
            stayInMenu = 1;     // go to submenu in InMenu 1
            resetFlag = false;
            lcd.clear();
          }
        } prevBtnState = btnState;
        break;

now entering this menu goes directly to the submenu. have to figure that out and flickering at the main menu (configuration Mode) as well.