Blinks the builtin LED for two seconds each time when the builtin button is pressed.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Demonstrates Timeout, by triggering an event
00002  * a fixed duration after a button press.
00003  */
00004 #include "mbed.h"
00005 Timeout wecker;          //create a Timeout, and name it "Response"
00006 DigitalIn button(BUTTON1);
00007 DigitalOut led(LED1);    //blinks with main while(1) loop
00008 volatile int state = 0;
00009 
00010 void noblink()           //this function is called at the end of the Timeout
00011 {
00012     state = 0;           // Stop blinking
00013 }
00014 
00015 int main() {
00016     while(1) {
00017         if(button==0) {
00018             wecker.attach(&noblink,2.0); //attach noblink function to Response
00019                                          //Timeout, to occur after 2 seconds
00020             state = 1;                   // Start binking LED
00021         }
00022         if(state) {
00023             led=!led;
00024             wait(0.2);
00025         } else {
00026             led = 0;
00027         }
00028     }
00029 }