Fastest way to read a de-bounced button?

I would like to know which is a better use of my processing time for listening for a de-bounced button press?
Option A:


void buttonPress(){
    //de-bouncing code and button pressed..
   // best method for de-bouncing an attach interrupt?

or Option B:

long pastTimer = 0;
int thumpCounter;
const int thumpPin = 36;
boolean thumping = false;
long lastThump;

void setup()
  pinMode(thumpPin, INPUT);
  digitalWrite(thumpPin, LOW);
  //set timer4 interrupt at 1 per .01sec
  TCCR4A = 0;// set entire TCCR1A register to 0
  TCCR4B = 0;// same for TCCR1B
  TCNT4  = 0;//initialize counter value to 0
  // set compare match register for 1hz increments
  OCR4A = 750;// = (16*10^6) / (1*1024) - 1 (must be <65536)
  // turn on CTC mode
  TCCR4B |= (1 << WGM42);
  // Set CS12 and CS10 bits for 1024 prescaler
  TCCR4B |= (1 << CS42) | (1 << CS40);  
  // enable timer compare interrupt
  TIMSK4 |= (1 << OCIE4A);

  unsigned long timer = millis();
  if (timer - pastTimer > 10){
    pastTimer = timer;
    int thumpState = digitalRead(thumpPin);
    if (thumpState == HIGH && thumping == false) {
      lastThump = millis();
      thumping = true;
    else if(thumpState == LOW)
      thumping = false;

Also if option B are there any improvements that can be made? / a simpler de-bounce method that works without millis()?

Option a looks way bether. Do never fill your interrupthandels with to much balast.

Why not just debounce the button with hardware(capacitor and resistor) instead of software? But if you want to do it with software, option A is the better way.

Here's code I have for a remote control with 4 buttons. Most of the time, there is not much going on in sketches - so polling for a person pressed buttons is all that is needed. Scan all the buttons, note whether the debounce time has pressed since the last press, and allow an action if it has. Also allows for press & hold while limiting how many presses per second are recognized.

  // Pace mode, D button push advances the seconds
  if (millis() - DPresstime >=200){  // debounce
    if (digitalRead (RemoteD) == 1){
      DPresstime = millis();
      advanced_tenths = 1;
      // Serial.println("time advanced");

I wouldn't use an interrupt at all. If you don't want to spend the time to debounce, you might consider a tecnique I've used in the past.

First, you check for a longish preiod of time in which the switch has not been pressed (say 10 milliseconds). You can then set a boolean to indicate that the switch is not pressed. Then, in loop(), just check the switch with a digitalRead. If it shows as pressed, assume that a person has actually hit the switch, because it would not have shown as pressed at all, if someone hadn't pressed it. At this point, you call it pressed, ignoring any bounces, clear the non-pressed boolean, and start looking again for a non-pressed condition lasting a longish time. Call your pressed() routine, and away you go.

Yes, you don't need any delays. However you can ignore any subsequent press which occurs shortly after the first. I have an example here:

Damn Nick - Where do you find time to put all that together! Impressive.

Typically I would agree with all of you for a simple de-bounce. However I need to be very sure that I never miss a button press and reliability is a big deal with this system. I don't want to just call a "set this var to true" in the attach interrupt and then run the rest of the code in the loop because i already have a lot of code running in the loop and some of it takes time. I use the interrupt bit to force the button pressed code to happen on time. What I am finding though is the ISR's are taking a good deal of processing power to run them once every x fractions of a second. I will see if I can get the buttons hardware de bounced so I can simplify the code on my end. Then I can just use the attach interrupts like a normal person ;)

I recant. After some testing and finding out that even my reasonably large portion of loop code runs in less than a millis() I will try removing the ISR's and see how it runs :)

Nick G is a friggin champ. When he writes a guide on how to do something, the conversation is over. :D

Option B uses timers that are already in use for generating PWM (analogWrite()) I'd eliminate it entirely, just for that reason.

I spent some time writing some really solid button code that doesn't rely on anything but a millis() timer to de-bounce it . I feel like this is the best way to go about a single button press. While this is still no good for my project (because i read a few slow sensors in the loop) I wanted to post it here for all of you to benefit from.

const int pin = 2;
long lastPress;
boolean alreadyPressed;

void setup() {

void loop() {
  unsigned long deBounce = millis();  
  if (deBounce - lastPress > 30){
    lastPress = deBounce;
    while(alreadyPressed == false && digitalRead(pin) == HIGH){
        alreadyPressed = true;
    while(alreadyPressed == true && digitalRead(pin) == LOW){
      alreadyPressed = false;

The major advantage to working a button this way is when the button is not being pressed the loop is not doing anything over and over again other than looking for the press.