Simple LED counter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut myled1(LED1);
00004 DigitalOut myled2(LED2);
00005 DigitalOut myled3(LED3);
00006 DigitalOut myled4(LED4);
00007 
00008 void update_leds (int myleds)
00009 {
00010   // update the LEDs
00011   myled1 = myleds  & 1;
00012   myled2 = (myleds & 2) >> 1;
00013   myled3 = (myleds & 4) >> 2;
00014   myled4 = (myleds & 8) >> 3;
00015 }
00016 
00017 int main() 
00018 {
00019     unsigned int    count, count_up, speed_up   = 0  ;
00020     float           count_interval              = 0.1;
00021 
00022     count_up = ~ count_up;
00023 
00024     while(1) 
00025     {
00026         // update LEDs with the current count value
00027         update_leds (count);
00028         
00029         // count up or down depending on the value of count_up
00030         if (count_up)
00031             count++;
00032         else
00033             count--;
00034         
00035         // interval to next count    
00036         wait(count_interval);
00037         
00038         // update the count interval when the LED count gets to 16
00039         // decrement the interval every 16 lots of counts
00040         if (speed_up && count % 16 == 0)
00041           count_interval /= 2;
00042         else if (count % 16 == 0)
00043           count_interval *= 2;   
00044         
00045         if (count % 256 == 0)
00046           speed_up = ~ speed_up;
00047         
00048         // change the count direction every 32 lots of counts
00049         if (count % 512 == 0)
00050           count_up = ~ count_up;
00051           
00052     }
00053 }