Rob Toulson / Mbed 2 deprecated PE_09-12_EventDrivenLED

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Program Example 9.12: Event driven LED switching with switch debounce
00002                                                                        */
00003 #include "mbed.h"
00004 InterruptIn button(p18);    // Interrupt on digital pushbutton input p18
00005 DigitalOut led1(LED1);           // digital out to LED1
00006 Timer debounce;                  // define debounce timer
00007 void toggle(void);               // function prototype
00008 int main() {
00009   debounce.start();
00010   button.rise(&toggle);          // attach the address of the toggle
00011 }                                             // function to the rising edge 
00012 void toggle() {
00013 if (debounce.read_ms()>10)      // only allow toggle if debounce timer 
00014   led1=!led1;                                       // has passed 10 ms
00015   debounce.reset();              // restart timer when the toggle is performed
00016 }
00017