#include <Plaquette.h>
////////////This program counts button presses between two LEDs to compare the effects of .debounce()
/////////////LEDs will toggle after a certain threshold of detected button presses.
DigitalOut debouncedLed(8);
DigitalOut normalLed(9);
DigitalIn normalButton(2, INTERNAL_PULLUP); //avoiding using extra resistors on the breadboard with internal pullup
DigitalIn debouncedButton(3, INTERNAL_PULLUP);
Metronome serialMetro (0.1); // slowdown your serial prints
Monitor monitor(9600);
int normal = 0; //val for storing button presses from normal button
int debounced = 0; //val for storing button presses from debounced button
// const variables are not allowed to change. Put const in ALL CAPS.
const int THRESHOLD = 5; //threshold val for activating the LEDs.
void begin() {
println ("Welcome to my amazing debounce demonstration");
debouncedButton.debounce(); // debouncing the debounce button
//initialize the LEDs to off
debouncedLed.off();
normalLed.off();
}
void step() {
///////////////COUNT THE BUTTON PRESSES///////////////////////////////////
if (normalButton.rose()) { //test normal button
normal++;
if (normal >= THRESHOLD) {
normal = 0; // reset counter
normalLed.on();
}
if (normal == 1) normalLed.off();
printing();
}
//test debounced button
if (debouncedButton.fell()) {
debounced++;
if (debounced >= THRESHOLD) {
debouncedLed.on();
debounced = 0; // reset counter
}
if (debounced ==1) debouncedLed.off();
printing();
}
}
//////////DIY FUNCTION! ABOVE OR BELOW THE MAIN CODE!//////////////////////////////////////
void printing() {
print ("Debounced: ");
print (debounced); // these numbers are accurate
print (" ");
print ("Normal: ");
println(normal); //these numbers do not reflect the amount of times you press the button
}Debounced button
Button without debouncing