debouncing a regular switch with Frequency timer 2

I am already using FrequencyTimer2 to poll and debounce two encoders by calling a function every 1ms. (detailed in another post)

If I also want to use this function to debounce a regular switch is as simple as just including a line to read the pin the switch is on? (i.e. I don't need to keep track of previous state?).

You do need to keep track of the previous state, or how do you know if the switch has changed / is bouncing.

Also, I think 1ms is too short for a de-bounce time. I have always worked on 20ms. I wouldn't want to go shorter than 5ms.



So if I have a function being called every 1ms I can put a counter in it to count to 20 (20ms debounce)

Any issue with code like this :


#define pushButtonPin  7
#define pushButtonDebounce 20  //20ms debounce for the pusbutton
int  pushButtonTimer = 0;
int  pushButtonPrevious = HIGH;
int  pushButtonState = HIGH;

Inside the function called by FrequencyTimer2 every 1ms:

  pusButtonReading = digitalRead(pushButtonPin);
  //Read the pushbutton every 1ms
//low reading is pressed pushbutton since using inverse logic
  if (pushButtonReading == LOW && pushButtonPrevious == HIGH && pushButtonTimer > pushButtonDebounce) {
    if (pushButtonState == LOW)
    pushButtonState = HIGH;
       pushButtonState = LOW;
     pushButtonTimer = 0;