counting time with interrupt


I want to know the time a button is pressed, the debounce time is 20?s, so if it is more, I want to know exactly how much it is. the button will be on interrupt pin, I can't find any examples how to achieve that. I suppose I have to use the interrupt CHANGE to know when button pressed/released

Can someone can give me some hints ?

Do you really mean 20us and not 20ms?

I’ve just quickly knocked this up and tested it. In my case pin 1 has serial data coming in from a GPS receiver. It measures the gap between transitions EVERY time there is a transition. I’ve limited the print commands to only occur every second so I can keep up with the data. Otherwise there’s too much to print too often and the output becomes garbled.

I’m using code like this to read serial data by interrupts. You could potentially time the gap between transitions and decide, if it’s over 20?s, what you want to do. My Arduino Duemilanove measures down to 4?s resolution.

volatile unsigned long previousTime = 0;
volatile unsigned long period = 0;

void setup()
  attachInterrupt(1, interrupt, CHANGE);

void loop()
  Serial.print("Period: ");

void interrupt()
  unsigned long currentTime = micros();
  period = currentTime - previousTime;
  previousTime = currentTime;

sixeyes has a very good point - a mechanical switch would no normally have any kind of bounce in the tens of microsecond range, but 20 milliseconds is much more believable.