Skip to content

Instantly share code, notes, and snippets.

@rubyist
Last active January 3, 2016 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubyist/8393949 to your computer and use it in GitHub Desktop.
Save rubyist/8393949 to your computer and use it in GitHub Desktop.
Timing an interrupt driven button
#define LONGPRESS_TIME 1000 // 1 second
volatile int trackButton = 0;
volatile unsigned long buttonChangeMillis = 0;
void setup() {
attachInterrupt(0, buttonPressed, CHANGE);
}
void buttonPressed()
{
if (trackButton == 0) {
buttonChangeMillis = millis();
trackButton = 1;
} else {
trackButton = 0;
unsigned long now = millis();
if ((now - buttonChangeMillis) <= LONGPRESS_TIME) {
// Short press
} else {
// Long press
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment