A collection of examples organized from basics to advanced.
Dependencies: mbed SDFileSystem
Mbed online compiler has no facility to easily manage a lot of programs or organized them in to related folders. This makes creating an examples and sample pack difficult.
This repository contains a single main.cpp file (which does very little), and a BuildOptions.h file. Simply uncomment the example you would like to compile from the build options. Each example is wrapped in a compiler directive.
If the directive does not include a description comment, it likely does not exist yet. If you would like to contribute to the Examples project, please contact me or fork and issue a pull request.
Diff: 02_Digital/Debounce.cpp
- Revision:
- 1:9a043ee174de
diff -r b471f7764d46 -r 9a043ee174de 02_Digital/Debounce.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/02_Digital/Debounce.cpp Fri Jul 05 04:52:18 2019 +0000 @@ -0,0 +1,42 @@ +#ifdef COMPILE_Debounce + +DigitalOut led1(LED1); + +InterruptIn button1(USER_BUTTON); +volatile bool button1_pressed = false; // Used in the main loop +volatile bool button1_enabled = true; // Used for debouncing +Timeout button1_timeout; // Used for debouncing + +// Enables button when bouncing is over +void button1_enabled_cb(void) +{ + button1_enabled = true; +} + +// ISR handling button pressed event +void button1_onpressed_cb(void) +{ + if (button1_enabled) { // Disabled while the button is bouncing + button1_enabled = false; + button1_pressed = true; // To be read by the main loop + button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms + } +} + +int main() +{ + //button1.mode(PullUp); // Activate pull-up + button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event + + int idx = 0; // Just for printf below + + while(1) { + if (button1_pressed) { // Set when button is pressed + button1_pressed = false; + printf("Button pressed %d\n", idx++); + led1 = !led1; + } + } +} + +#endif \ No newline at end of file