#define Center_switch 7 //assuming we use D2 on Arduino
static bool state;
int duration = 10;
int count=duration/2;
void setup() {
Serial.begin(115200);
pinMode(Center_switch, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if (debounce()) {
digitalWrite(LED_BUILTIN, true);
}
else {
digitalWrite(LED_BUILTIN, false);
}
}
bool debounce() {
if (digitalRead(Center_switch)) {
count++;
}
else{
count--;
}
if (count > duration) {
state = true;
count = 10;
}
if (count < 0) {
state = false;
count = 0;
}
Serial.print("count:");
Serial.print(count);
Serial.print(", debounce:");
Serial.println(state);
delay(5);
return (state);
}