// https://esp32io.com/tutorials/esp32-button-debounce
/*
This ESP32 code is created by esp32io.com
This ESP32 code is released in the public domain
For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-button-debounce
*/
// #define BUTTON_PIN 13 // GIOP21 pin connected to button
// #define DEBOUNCE_TIME 50 // the debounce time in millisecond, increase this time if it still chatters
// 13 14 25 26 27
// ********** Push Button Switch **********
const int DEBOUNCE_TIME = 50;
const int BUTTON_PIN_SW01 = 13;
const int BUTTON_PIN_SW02 = 14;
const int BUTTON_PIN_SW03 = 25;
const int BUTTON_PIN_SW04 = 26;
const int BUTTON_PIN_SW05 = 27;
const int led_01 = 04;
const int led_02 = 05;
const int led_03 = 16;
const int led_04 = 17;
const int led_05 = 18;
int status_sw01;
int status_sw02;
int status_sw03;
int status_sw04;
int status_sw05;
// Variables will change:
int lastSteadyState_01 = LOW; // the previous steady state from the input pin
int lastFlickerableState_01 = LOW; // the previous flickerable state from the input pin
int currentState_01; // the current reading from the input pin
unsigned long lastDebounceTime_01 = 0; // the last time the output pin was toggled
int lastSteadyState_02 = LOW;
int lastFlickerableState_02 = LOW;
int currentState_02;
unsigned long lastDebounceTime_02 = 0;
int lastSteadyState_03 = LOW;
int lastFlickerableState_03 = LOW;
int currentState_03;
unsigned long lastDebounceTime_03 = 0;
int lastSteadyState_04 = LOW;
int lastFlickerableState_04 = LOW;
int currentState_04;
unsigned long lastDebounceTime_04 = 0;
int lastSteadyState_05 = LOW;
int lastFlickerableState_05 = LOW;
int currentState_05;
unsigned long lastDebounceTime_05 = 0;
// ********** Push Button Switch end **********
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
void setup() {
Serial.begin(115200);
// ********** Push Button Switch **********
pinMode(BUTTON_PIN_SW01, INPUT_PULLUP);
pinMode(BUTTON_PIN_SW02, INPUT_PULLUP);
pinMode(BUTTON_PIN_SW03, INPUT_PULLUP);
pinMode(BUTTON_PIN_SW04, INPUT_PULLUP);
pinMode(BUTTON_PIN_SW05, INPUT_PULLUP);
pinMode(led_01, OUTPUT);
pinMode(led_02, OUTPUT);
pinMode(led_03, OUTPUT);
pinMode(led_04, OUTPUT);
pinMode(led_05, OUTPUT);
// ********** Push Button Switch end **********
}
void loop() {
// ********** Push Button Switch **********
debounce_switch_01();
debounce_switch_02();
debounce_switch_03();
debounce_switch_04();
debounce_switch_05();
digitalWrite(led_01, status_sw01);
digitalWrite(led_02, status_sw02);
digitalWrite(led_03, status_sw03);
digitalWrite(led_04, status_sw04);
digitalWrite(led_05, status_sw05);
/*
if (status_sw01 == 1) {
digitalWrite(led_01, status_sw01);
} else if (status_sw02 == 1) {
digitalWrite(led_02, status_sw02);
}
*/
// ********** Push Button Switch end **********
}
void debounce_switch_01() {
currentState_01 = digitalRead(BUTTON_PIN_SW01);
if (currentState_01 != lastFlickerableState_01) {
// reset the debouncing timer
lastDebounceTime_01 = millis();
// save the the last flickerable state
lastFlickerableState_01 = currentState_01;
}
if ((millis() - lastDebounceTime_01) > DEBOUNCE_TIME) {
if (lastSteadyState_01 == HIGH && currentState_01 == LOW) {
// Serial.println("The button is pressed");
status_sw01 = 1;
Serial.print("Switch 1=");
Serial.print(status_sw01);
Serial.print(",The button ");
Serial.print(BUTTON_PIN_SW01);
Serial.println(" is pressed");
} else if (lastSteadyState_01 == LOW && currentState_01 == HIGH) {
// Serial.println("The button is released");
status_sw01 = 0;
Serial.print("Switch 1=");
Serial.print(status_sw01);
Serial.print(",The button ");
Serial.print(BUTTON_PIN_SW01);
Serial.println(" is released");
}
lastSteadyState_01 = currentState_01; // save the the last steady state
}
} ////////// end void debounce_switch_01()
void debounce_switch_02() {
currentState_02 = digitalRead(BUTTON_PIN_SW02);
if (currentState_02 != lastFlickerableState_02) {
// reset the debouncing timer
lastDebounceTime_02 = millis();
// save the the last flickerable state
lastFlickerableState_02 = currentState_02;
}
if ((millis() - lastDebounceTime_02) > DEBOUNCE_TIME) {
if (lastSteadyState_02 == HIGH && currentState_02 == LOW) {
// Serial.println("The button is pressed");
status_sw02 = 1;
Serial.print("Switch 2=");
Serial.print(status_sw02);
Serial.print(",The button ");
Serial.print(BUTTON_PIN_SW02);
Serial.println(" is pressed");
} else if (lastSteadyState_02 == LOW && currentState_02 == HIGH) {
// Serial.println("The button is released");
status_sw02 = 0;
Serial.print("Switch 2=");
Serial.print(status_sw02);
Serial.print(",The button ");
Serial.print(BUTTON_PIN_SW02);
Serial.println(" is released");
}
lastSteadyState_02 = currentState_02; // save the the last steady state
}
} ////////// end void debounce_switch_02()
void debounce_switch_03() {
currentState_03 = digitalRead(BUTTON_PIN_SW03);
if (currentState_03 != lastFlickerableState_03) {
// reset the debouncing timer
lastDebounceTime_03 = millis();
// save the the last flickerable state
lastFlickerableState_03 = currentState_03;
}
if ((millis() - lastDebounceTime_03) > DEBOUNCE_TIME) {
if (lastSteadyState_03 == HIGH && currentState_03 == LOW) {
// Serial.println("The button is pressed");
status_sw03 = 1;
Serial.print("Switch 3=");
Serial.print(status_sw03);
Serial.print(",The button ");
Serial.print(BUTTON_PIN_SW03);
Serial.println(" is pressed");
} else if (lastSteadyState_03 == LOW && currentState_03 == HIGH) {
// Serial.println("The button is released");
status_sw03 = 0;
Serial.print("Switch 3=");
Serial.print(status_sw03);
Serial.print(",The button ");
Serial.print(BUTTON_PIN_SW03);
Serial.println(" is released");
}
lastSteadyState_03 = currentState_03; // save the the last steady state
}
} ////////// end void debounce_switch_03()
void debounce_switch_04() {
currentState_04 = digitalRead(BUTTON_PIN_SW04);
if (currentState_04 != lastFlickerableState_04) {
// reset the debouncing timer
lastDebounceTime_04 = millis();
// save the the last flickerable state
lastFlickerableState_04 = currentState_04;
}
if ((millis() - lastDebounceTime_04) > DEBOUNCE_TIME) {
if (lastSteadyState_04 == HIGH && currentState_04 == LOW) {
// Serial.println("The button is pressed");
status_sw04 = 1;
Serial.print("Switch 4=");
Serial.print(status_sw04);
Serial.print(",The button ");
Serial.print(BUTTON_PIN_SW04);
Serial.println(" is pressed");
} else if (lastSteadyState_04 == LOW && currentState_04 == HIGH) {
// Serial.println("The button is released");
status_sw04 = 0;
Serial.print("Switch 4=");
Serial.print(status_sw04);
Serial.print(",The button ");
Serial.print(BUTTON_PIN_SW04);
Serial.println(" is released");
}
lastSteadyState_04 = currentState_04; // save the the last steady state
}
} ////////// end void debounce_switch_04()
void debounce_switch_05() {
currentState_05 = digitalRead(BUTTON_PIN_SW05);
if (currentState_05 != lastFlickerableState_05) {
// reset the debouncing timer
lastDebounceTime_05 = millis();
// save the the last flickerable state
lastFlickerableState_05 = currentState_05;
}
if ((millis() - lastDebounceTime_05) > DEBOUNCE_TIME) {
if (lastSteadyState_05 == HIGH && currentState_05 == LOW) {
// Serial.println("The button is pressed");
status_sw05 = 1;
Serial.print("Switch 5=");
Serial.print(status_sw05);
Serial.print(",The button ");
Serial.print(BUTTON_PIN_SW05);
Serial.println(" is pressed");
} else if (lastSteadyState_05 == LOW && currentState_05 == HIGH) {
// Serial.println("The button is released");
status_sw05 = 0;
Serial.print("Switch 5=");
Serial.print(status_sw05);
Serial.print(",The button ");
Serial.print(BUTTON_PIN_SW05);
Serial.println(" is released");
}
lastSteadyState_05 = currentState_05; // save the the last steady state
}
} ////////// end void debounce_switch_05()