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.
02_Digital/Debounce.cpp@1:9a043ee174de, 2019-07-05 (annotated)
- Committer:
- epremeaux
- Date:
- Fri Jul 05 04:52:18 2019 +0000
- Revision:
- 1:9a043ee174de
cleaning up and organizing directories. added a few programs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
epremeaux | 1:9a043ee174de | 1 | #ifdef COMPILE_Debounce |
epremeaux | 1:9a043ee174de | 2 | |
epremeaux | 1:9a043ee174de | 3 | DigitalOut led1(LED1); |
epremeaux | 1:9a043ee174de | 4 | |
epremeaux | 1:9a043ee174de | 5 | InterruptIn button1(USER_BUTTON); |
epremeaux | 1:9a043ee174de | 6 | volatile bool button1_pressed = false; // Used in the main loop |
epremeaux | 1:9a043ee174de | 7 | volatile bool button1_enabled = true; // Used for debouncing |
epremeaux | 1:9a043ee174de | 8 | Timeout button1_timeout; // Used for debouncing |
epremeaux | 1:9a043ee174de | 9 | |
epremeaux | 1:9a043ee174de | 10 | // Enables button when bouncing is over |
epremeaux | 1:9a043ee174de | 11 | void button1_enabled_cb(void) |
epremeaux | 1:9a043ee174de | 12 | { |
epremeaux | 1:9a043ee174de | 13 | button1_enabled = true; |
epremeaux | 1:9a043ee174de | 14 | } |
epremeaux | 1:9a043ee174de | 15 | |
epremeaux | 1:9a043ee174de | 16 | // ISR handling button pressed event |
epremeaux | 1:9a043ee174de | 17 | void button1_onpressed_cb(void) |
epremeaux | 1:9a043ee174de | 18 | { |
epremeaux | 1:9a043ee174de | 19 | if (button1_enabled) { // Disabled while the button is bouncing |
epremeaux | 1:9a043ee174de | 20 | button1_enabled = false; |
epremeaux | 1:9a043ee174de | 21 | button1_pressed = true; // To be read by the main loop |
epremeaux | 1:9a043ee174de | 22 | button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms |
epremeaux | 1:9a043ee174de | 23 | } |
epremeaux | 1:9a043ee174de | 24 | } |
epremeaux | 1:9a043ee174de | 25 | |
epremeaux | 1:9a043ee174de | 26 | int main() |
epremeaux | 1:9a043ee174de | 27 | { |
epremeaux | 1:9a043ee174de | 28 | //button1.mode(PullUp); // Activate pull-up |
epremeaux | 1:9a043ee174de | 29 | button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event |
epremeaux | 1:9a043ee174de | 30 | |
epremeaux | 1:9a043ee174de | 31 | int idx = 0; // Just for printf below |
epremeaux | 1:9a043ee174de | 32 | |
epremeaux | 1:9a043ee174de | 33 | while(1) { |
epremeaux | 1:9a043ee174de | 34 | if (button1_pressed) { // Set when button is pressed |
epremeaux | 1:9a043ee174de | 35 | button1_pressed = false; |
epremeaux | 1:9a043ee174de | 36 | printf("Button pressed %d\n", idx++); |
epremeaux | 1:9a043ee174de | 37 | led1 = !led1; |
epremeaux | 1:9a043ee174de | 38 | } |
epremeaux | 1:9a043ee174de | 39 | } |
epremeaux | 1:9a043ee174de | 40 | } |
epremeaux | 1:9a043ee174de | 41 | |
epremeaux | 1:9a043ee174de | 42 | #endif |