interrupt based debouncing registers two presses

i tried to use interrupts to debounce my buttons but my code registers two button presses, and the second "press" comes in at my debounce time (i.e. if i set debounce time to 1 second, the second press comes in after 1 sec).

    // CHANGES THE DEFAULT GAME TIME
    while(dip1==1){
      dip1 = digitalRead(timePin);    // read the input pin for dipswitch 1
      
      if(millis() - interrupt0Time >= 1000){
        attachInterrupt(0, digitSelect, RISING);
      }
      if(millis() - interrupt1Time >= 1000){
        attachInterrupt(1, timeSelect, RISING);
      }
      
      if(dip1==0){
      disp1Metro.reset();
      disp2Metro.reset();
      }
      
      if(blinkMetro.check() == 1){
        blinkMetro.reset();
        shiftOut(dataPin, clockPin, MSBFIRST, d2);
        shiftOut(dataPin, clockPin, MSBFIRST, c2);
        shiftOut(dataPin, clockPin, MSBFIRST, b2);
        shiftOut(dataPin, clockPin, MSBFIRST, a2);
        timeDelay = millis();
      }
      if(millis() - timeDelay >= 100){
        shiftOut(dataPin, clockPin, MSBFIRST, d);
        shiftOut(dataPin, clockPin, MSBFIRST, c);
        shiftOut(dataPin, clockPin, MSBFIRST, b);
        shiftOut(dataPin, clockPin, MSBFIRST, a);
      }
    }

}

void digitSelect(void){
  setDigit += 1;
  if(setDigit >= 4){
    setDigit = 0;
  }
  
  switch(setDigit){
  case 0:
    d2 = 0x78;
    c2 = c;
    b2 = b;
    a2 = a;

    break;
    
  case 1:
    d2 = d;
    c2 = 0x78;
    b2 = b;
    a2 = a;

    break;
  
  case 2:
    d2 = d;
    c2 = c;
    b2 = 0x78;
    a2 = a;

    break;
  
  case 3:
    d2 = d;
    c2 = c;
    b2 = b;
    a2 = 0x78;

    break;  
  }
  
  interrupt0Time = millis();
  
  detachInterrupt(0);
}

void timeSelect(void){
  if(d2==0x78){
    d += 1;
    if(d >= 10){
      d = 0;
    }
  }
  else if(c2==0x78){
    c += 1;
    if(c >= 10){
      c = 0;
    }
  }
  else if(b2==0x78){
    b += 1;
    if(b >= 10){
      b = 0;
    }
  }
  else{
    a += 1;
    if(a >= 10){
      a = 0;
    }
  }
  
  interrupt1Time = millis();
  
  detachInterrupt(1);
}

btw, can i use pins 2 and 3 as normal inputs for the majority of my code (where i don't need debouncing) and still use them as interrupts in the portion of code i pasted above (where i need debouncing)?

If you want to do hardware debouncing I have a debounce circuit in
the datasheet for my prototyping board (see Loading...)

The circuit is from an EDN article by Ron Mancini and works quite
well.

(* jcl *)

yup i saw that from the other interrupt debouncing thread, i may have to go with hardware debouncing. thanks for the link.

i've tried many ways to debounce the buttons and i'm fortunate that only one portion of my code needs debouncing otherwise i would never have gotten this far. most of my posts for help with the various debouncing techniques have gone unanswered or i was told to do hardware debouncing. maybe its a question that has been asked far too many times and now tends to get mostly ignored, although i haven't found many useful threads when using search.

There are some ICs that do switch debouncing with an algorithm ---
switch closure detected, delay, check to see if closure is still present. They may
do some sort of averaging too.

The datasheets or design notes may have some ideas. The one I saw recently was the
Linear Technology LTC2950. There is a design note at --

(* jcl *)

MC14490 is nice chip that will debounce six inputs and is avalible in several different package types.

http://www.onsemi.com/PowerSolutions/product.do?id=MC14490P

Little pricey but designed for success. :wink:

Lefty

i will probably order one of those MC14490, $6.50 via digikey but it'll easily be worth it once it solves the problems i've been having. what i wonder though is if software debouncing really is hard or if i'm just too much of a coding newbie to do it.