Rob Toulson / Mbed 2 deprecated PE_09-04_TwoTimerTasks

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*Program Example 9.4: Program which runs two time-based tasks
00002                                                                        */
00003 #include "mbed.h"
00004 Timer timer_fast;                    // define Timer with name "timer_fast"
00005 Timer timer_slow;                    // define Timer with name "timer_slow"
00006 DigitalOut ledA(LED1);
00007 DigitalOut ledB(LED4);
00008 
00009 void task_fast(void);                //function prototypes
00010 void task_slow(void);
00011 
00012 int main() {
00013   timer_fast.start();    //start the Timers
00014   timer_slow.start(); 
00015   while (1){
00016     if (timer_fast.read()>0.2){  //test Timer value
00017       task_fast();               //call the task if trigger time is reached
00018       timer_fast.reset();            //and reset the Timer
00019     }
00020     if (timer_slow.read()>1){     //test Timer value
00021       task_slow();
00022       timer_slow.reset();
00023     }
00024   }
00025 }
00026 
00027 void task_fast(void){          //”Fast” Task
00028   ledA = !ledA;
00029 }
00030 void task_slow(void){         //”Slow” Task
00031   ledB = !ledB;
00032 }
00033