Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save elktros/e62e4540deebe8a8e6affeeee0184917 to your computer and use it in GitHub Desktop.
Save elktros/e62e4540deebe8a8e6affeeee0184917 to your computer and use it in GitHub Desktop.
Interrupt based Button sketch with debounce for Arduino Interrupts Tutorial.
int ledPin = 13;
int buttonPin = 2;
int ledToggle;
int previousState = HIGH;
unsigned int previousPress;
volatile int buttonFlag;
int buttonDebounce = 20;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), button_ISR, CHANGE);
}
void loop()
{
if((millis() - previousPress) > buttonDebounce && buttonFlag)
{
previousPress = millis();
if(digitalRead(buttonPin) == LOW && previousState == HIGH)
{
ledToggle =! ledToggle;
digitalWrite(ledPin, ledToggle);
previousState = LOW;
}
else if(digitalRead(buttonPin) == HIGH && previousState == LOW)
{
previousState = HIGH;
}
buttonFlag = 0;
}
}
void button_ISR()
{
buttonFlag = 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment