Skip to content

Instantly share code, notes, and snippets.

@phnahes
Created July 18, 2021 14:25
Show Gist options
  • Save phnahes/607f86ca49cc0afb237c497146e8ed4a to your computer and use it in GitHub Desktop.
Save phnahes/607f86ca49cc0afb237c497146e8ed4a to your computer and use it in GitHub Desktop.
ESP32 button with debounce and no repeat pressed while hold button
volatile unsigned long DebounceTimer;
volatile int ButtonPressed;
volatile unsigned int delayTime = 200;
volatile unsigned int delayRepeatTime = 500;
volatile byte state = LOW;
unsigned long lastInterrupt;
#define pin 34
#define ledPin 2
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(pin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pin), [] {
if (ButtonPressed += (millis() - DebounceTimer) >= (delayTime )) {
DebounceTimer = millis();
if (millis() - lastInterrupt > delayRepeatTime)
{
state = !state;
lastInterrupt = millis();
digitalWrite(ledPin, state);
Serial.println("Pressed");
Serial.println(state);
}
}
}, RISING);
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment