#include <Plaquette.h>
////////////This program counts button presses between two LEDs to compare the effects of .debounce()
/////////////LEDs will light after a certain threshold has been met of button presses.
const int THRESHOLD = 10; //threshold for activating the LEDs is constant. Put const in ALL CAPS.
DigitalOut debouncedLED(8);
DigitalOut normalLED(9);
int normal = 0; //val for storing button presses from normal button
int debounced = 0; //val for storing button presses from debounced button
DigitalIn normalButton(2, INTERNAL_PULLUP);
DigitalIn debouncedButton(3, INTERNAL_PULLUP);
Metronome serialMetro (0.05); //make a metro so your Serial monitor isn't screaming
Alarm normalAlarm(0.2); //alarm so that we can actually see the LED lighting up
Alarm debouncedAlarm(0.2); //alarm so that we can actually see the LED lighting up
void begin() {
debouncedButton.debounce(); // debouncing the debounce button XD
}
void step() {
///////////////COUNT THE BUTTON PRESSES///////////////////////////////////
/* This doesn't work! Can you explain why?
if (normalButton) {
normal = normal + 1;
}
*/
if (normalButton.rose()) {
normal++;
printing(); //calling my own function
}
if (debouncedButton.rose()) {
debounced++;
printing(); // calling my own function again
}
////////////////////////DEBOUNCED LED////////////////////////////////
if (debounced == THRESHOLD) {
debouncedLED.on();
debouncedAlarm.start();
debounced = 0;
}
if (debouncedAlarm) {
debouncedLED.off();
debouncedAlarm.stop();
}
////////////////////NORMAL LED/////////////////////////////////////
if (normal == THRESHOLD) {
normalLED.on();
normalAlarm.start();
normal = 0;
}
if (normalAlarm) {
normalLED.off();
normalAlarm.stop();
}
/*
///////////////////SERIAL PRINT/////////////////////////////////////
if (serialMetro) {
print (debounced); //these numbers are accurate
print (" ");
println(normal); //these numbers do not reflect the amount of times you press the button
} */
}
/////////////FUNCTIONS//////////////////////////////////////
void printing() {
print (debounced); //these numbers are accurate
print (" ");
println(normal); //these numbers do not reflect the amount of times you press the button
}Debounced button
Button without debouncing