Rob Toulson / Mbed 2 deprecated PE_09-07_TimeoutInterrupt

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*Program Example 9.7: Demonstrates the use of Timeout and interrupts, to allow response to an event-driven task while a time-driven task continues.
00002                                                                              */ 
00003 #include "mbed.h"
00004 void blink_end (void);      
00005 void blink (void); 
00006 void ISR1 (void);
00007 DigitalOut led1(LED1);
00008 DigitalOut led2(LED2);
00009 DigitalOut led3(LED3);
00010 Timeout Response;             //create a Timeout, and name it Response
00011 Timeout Response_duration;    //create a Timeout, and name it Response_duration
00012 InterruptIn button(p5);       //create an interrupt input, named button
00013 
00014 void blink() {           //This function is called when Timeout is complete
00015   led2=1;
00016   // set the duration of the led blink, with another timeout, duration 0.1 s    
00017   Response_duration.attach(&blink_end, 1);
00018 }
00019     
00020 void blink_end() {            //A function called at the end of Timeout Response_duration
00021   led2=0;
00022 }
00023     
00024 void ISR1(){
00025   led3=1;       //shows button is pressed; diagnostic and not central to program
00026   //attach blink1 function to Response Timeout, to occur after 2 seconds    
00027   Response.attach(&blink, 2.0);
00028 }
00029     
00030 int main() {
00031   button.rise(&ISR1);   //attach the address of ISR1 function to the rising edge
00032   while(1) {
00033     led3=0;                //clear LED3 
00034     led1=!led1;
00035     wait(0.2);
00036   }
00037 }
00038