// https://wokwi.com/projects/363256151401922561
// based on https://wokwi.com/projects/363250216927001601
// https://forum.arduino.cc/t/stepper-limit-switch-confusion/1120713
# include <ezButton.h>
ezButton button(7); // create ezButton object that attach to pin 7;
unsigned buttonDebounceInterval = 2000; // large delay for detecting differences
// stick-built responsive debounce infrastructure
static unsigned long buttonStateChangeTime = -buttonDebounceInterval; // negative to induce rollover
static uint8_t buttonState = HIGH;
void setup() {
Serial.begin(9600);
button.setDebounceTime(buttonDebounceInterval);
pinMode(12,OUTPUT);
pinMode(3,OUTPUT);
}
void loop() {
button.loop(); // MUST call the loop() function first
// Responsive debounce change detection
uint8_t buttonTransient = digitalRead(7);
if (buttonTransient != buttonState && millis() - buttonStateChangeTime >= buttonDebounceInterval) {
buttonStateChangeTime = millis();
buttonState = buttonTransient;
Serial.print("Responsive button changed to ");
if (buttonState == HIGH) { // rising edge
Serial.println("UP");
digitalWrite(3,LOW);
} else { // falling edge
Serial.println("DOWN");
digitalWrite(3,HIGH);
}
}
if (button.isPressed()){
Serial.println("The ezButton is pressed");
digitalWrite(12,HIGH);
}
if (button.isReleased()){
Serial.println("The ezButton is released");
digitalWrite(12,LOW);
}
static unsigned long last;
if (millis() - last > 777) {
last = millis();
Serial.print("the ezButton is ");
if (button.getState())
Serial.println("UP");
else
Serial.println("DOWN");
}
}