/*
LeadingEdgeDebounce.ino
This sketch demonstrates the Arduino debounce example from
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Debounce
against
A leading edge debounce scheme
John Wasser's debounce scheme from https://forum.arduino.cc/t/leading-edge-debouncing/964407/4
The https://github.com/thomasfredericks/Bounce2 scheme
Note that the leading edge and Wasser schemes trigger at the beginning of the press
and that the Arduino and Bounce2 schemes trigger after a delay.
Live demo at https://wokwi.com/arduino/projects/324855073217708626
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
minimum delay between toggles to debounce the circuit (i.e. to ignore noise).
Artifical noise is generated on pin 3. Flip the switch to inject the simulated
noisy square wave into pin 2.
The circuit:
- LED attached from pin 13 to ground through 220 ohm resistor
- pushbutton attached from pin 2 to +5V
- 10 kilohm resistor attached from pin 2 to ground
- Switch between pin 2 and 3
- Note: On most Arduino boards, there is already an LED on the board connected
to pin 13, so you don't need any extra components for this example.
created 21 Nov 2006
by David A. Mellis
modified 30 Aug 2011
by Limor Fried
modified 28 Dec 2012
by Mike Walters
modified 30 Aug 2016
by Arturo Guadalupi
modified 28 Feb 2021
by DaveX
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Debounce
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int ledPin2 = 12; // the number of the 2nd LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// 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.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
//
const byte ButtonPin = buttonPin;
const unsigned long DebounceTime = debounceDelay;
boolean ButtonWasPressed; // Defaults to 'false'
unsigned long ButtonStateChangeTime = 0; // Debounce timer
// Try Bounce2's lockout form
// (Doesn't work, 2022-03-26 -- Issue https://github.com/thomasfredericks/Bounce2/issues/85 )
//#define BOUNCE_LOCK_OUT // per https://forum.arduino.cc/t/leading-edge-debouncing/964407/15?u=davex
// defines do not control library compilation of their cpp files,
// only the included version of the
// header files
// If you want BOUNCE_LOCK_OUT behavior it needs definition in the Bounce2.h file.
// The #define BOUNCE_LOCK_OUT is uncommented in the modified local copy of
// Bounce2 for this sim.
// The #define BOUNCE_WITH_PROMPT_DETECTION also enables leading-edge debounce behavior, arming for
// change detection after a stable period
#include "Bounce2BLO.h" // https://github.com/thomasfredericks/Bounce2
Bounce b = Bounce(); // Instantiate a Bounce object
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.begin(115200);
// set initial LED state
digitalWrite(ledPin, ledState);
b.attach(buttonPin, INPUT);
b.interval(debounceDelay);
}
void loop() {
// read the state of the switch and time into local variables for consistency:
int reading = digitalRead(buttonPin);
unsigned long loopMillis = millis();
// individual counts per method:
static int buttonCount;
static int buttonCount2, buttonCount3, buttonCount4;
b.update(); // Update the Bounce instance
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = loopMillis;
}
if ((loopMillis - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
buttonCount++;
Serial.print(loopMillis);
Serial.print("ms Arduino Debounce.ino: ");
Serial.println(buttonCount);
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
// Leading Edge debounce
static unsigned long buttonLast = 0;
if (reading == HIGH) { // process the button
if ( loopMillis - buttonLast > debounceDelay ) { // long enough
// Some oneshot button code:
buttonCount2++;
digitalWrite(ledPin2, !digitalRead(ledPin2));
Serial.print(loopMillis);
Serial.print("ms leading Edge Debounce: ");
Serial.println(buttonCount2);
}
// reset the timer whenever the button is pressed:
buttonLast = loopMillis;
}
// Scheme per JohnWasser @ https://forum.arduino.cc/t/leading-edge-debouncing/964407/4
boolean buttonIsPressed = reading == HIGH; // Active HIGH
// Check for button state change and do debounce
if (buttonIsPressed != ButtonWasPressed &&
loopMillis - ButtonStateChangeTime > DebounceTime)
{
// Button state has changed
ButtonStateChangeTime = loopMillis;
ButtonWasPressed = buttonIsPressed;
if (ButtonWasPressed)
{
buttonCount3++;
Serial.print(loopMillis);
Serial.print("ms Wasser Debounce: ");
Serial.println(buttonCount3);
// Button was just pressed
}
else
{
// Button was just released
}
}
// Bounce2 reporting
if ( b.rose() ) { // Call code if button transitions from LOW to HIGH
//ledState = !ledState; // Toggle LED state
//digitalWrite(LED_PIN,ledState); // Apply new LED state
buttonCount4++;
Serial.print(loopMillis);
Serial.print("ms Bounce2 Debounce: ");
Serial.println(buttonCount4);
// Button was just pressed
}
// reporting:
simulateNoise(3);
}
// Simulate noise on pin 3
void simulateNoise(const byte noisePin) {
// This simulates noise on pin
static int noiseMode = 0;
static int noiseEntry = 0;
static unsigned long interval = 501000;
const unsigned long constPeriod = 499000;
const unsigned long noisePeriod = 1000;
unsigned long now = micros(); // cast to int for speed
static unsigned long last = 0;
//Serial.print(noiseMode);
if (now - last >= interval) {
last += interval;
switch (noiseMode) {
case 0:
pinMode(noisePin, OUTPUT);
digitalWrite(noisePin, HIGH);
noiseMode = 2;
// Serial.print('0');
break;
case 2:
interval = 1;
digitalWrite(noisePin, generateNoise());
if (++noiseEntry == noisePeriod) { // period of high
noiseEntry = 0;
noiseMode = 3;
digitalWrite(noisePin, HIGH);
interval = constPeriod;
//Serial.print('+');
}
break;
case 3: // noise->LOW
interval = 1;
digitalWrite(noisePin, generateNoise());
if (++noiseEntry == noisePeriod) { // period of LOW
noiseEntry = 0;
noiseMode = 2;
digitalWrite(noisePin, LOW);
interval = constPeriod;
//Serial.print('-');
}
break;
}
}
}
/// from https://arduino.stackexchange.com/a/18092/6628
/* initialize with any 32 bit non-zero unsigned long value. */
#define LFSR_INIT 0xfeedfaceUL
/* Choose bits 32, 30, 26, 24 from http://arduino.stackexchange.com/a/6725/6628
or 32, 22, 2, 1 from
http://www.xilinx.com/support/documentation/application_notes/xapp052.pdf
or bits 32, 16, 3,2 or 0x80010006UL per http://users.ece.cmu.edu/~koopman/lfsr/index.html
and http://users.ece.cmu.edu/~koopman/lfsr/32.dat.gz
or generate a one with https://github.com/hayguen/mlpolygen
*/
#define LFSR_MASK ((unsigned long)( 1UL<<31 | 1UL <<15 | 1UL <<2 | 1UL <<1 ))
unsigned int generateNoise() {
// See https://en.wikipedia.org/wiki/Linear_feedback_shift_register#Galois_LFSRs
static unsigned long int lfsr = LFSR_INIT; /* 32 bit init, nonzero */
/* If the output bit is 1, apply toggle mask.
The value has 1 at bits corresponding
to taps, 0 elsewhere. */
if (lfsr & 1) {
lfsr = (lfsr >> 1) ^ LFSR_MASK ;
return (1);
}
else {
lfsr >>= 1;
return (0);
}
}