// for Discord and Forum
// Code at https://wokwi.com/projects/327202747064517203
const byte buttonPin = 2;
const bool AbsoluteNotDifference = false; // Report absolute time or differences
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(2500000);
Serial.print("ButtonBounceDemo.ino -- Wokwi simulation of bouncing\n"
"If you push the button, the sketch will report the\n"
"microsecond times of the signal on Digital Pin 2.\n"
"This is good for checking for noisy buttons.\n"
);
}
//globals
int oldButtonState = LOW;
unsigned long lastTransitionUs = 0; // Time of the last switching
unsigned long deadtime = 0; // deadtime for state latching mode debouncing
// See https://github.com/thomasfredericks/Bounce2#lock-out-interval for a
// good diagram.
// Set deadtime above 0us to inhibit repeated transitions
// deadtime 2200us isn't quite enough to debounce the Wokwi pushbutton
void loop() {
int buttonState = digitalRead(buttonPin);
unsigned long now = micros();
if (buttonState != oldButtonState && (now - lastTransitionUs >= deadtime)) {
if (AbsoluteNotDifference) {
Serial.print(now);
} else {
Serial.print(now - lastTransitionUs);
}
Serial.print(buttonState ? "+ " : "- ");
lastTransitionUs = now;
oldButtonState = buttonState;
}
}