Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Last active October 23, 2023 10:14
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 kakopappa/b8db56b3315c83be6bda1069e9b6a5c8 to your computer and use it in GitHub Desktop.
Save kakopappa/b8db56b3315c83be6bda1069e9b6a5c8 to your computer and use it in GitHub Desktop.
#if defined(ESP8266)
#define BUTTON_PIN D4
#elif defined(ESP32)
#define BUTTON_PIN 34
#elif (ARDUINO_ARCH_RP2040)
#define BUTTON_PIN 7
#endif
int button_state;
int lastbutton_state = LOW;
unsigned long last_debounce_time = 0;
unsigned long debounce_delay = 50; // increase if needed.
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT);
}
void handle_button_press() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastbutton_state) {
last_debounce_time = millis();
}
if ((millis() - last_debounce_time) > debounce_delay) {
if (reading != button_state) {
button_state = reading;
if (button_state == HIGH) {
Serial.printf("Pressed!\r\n");
}
}
}
lastbutton_state = reading;
}
void loop() {
handle_button_press();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment