Leo Matturi / Mbed 2 deprecated counter

Dependencies:   mbed

Revision:
1:6c8d46a9c3c9
Parent:
0:adb9fcb3b1bb
Child:
2:7cdadcca2c06
diff -r adb9fcb3b1bb -r 6c8d46a9c3c9 main.cpp
--- a/main.cpp	Sun May 09 01:25:51 2010 +0000
+++ b/main.cpp	Tue May 11 21:15:44 2010 +0000
@@ -7,6 +7,7 @@
 
 void update_leds (int myleds)
 {
+  // update the LEDs
   myled1 = myleds  & 1;
   myled2 = (myleds & 2) >> 1;
   myled3 = (myleds & 4) >> 2;
@@ -15,21 +16,38 @@
 
 int main() 
 {
-    int     count, count_up = 0;
-    float   count_interval = 1.0;
-    count = 0;
+    unsigned int    count, count_up, speed_up   = 0  ;
+    float           count_interval              = 1.0;
+
+    count_up = ~ count_up;
+
     while(1) 
     {
+        // update LEDs with the current count value
         update_leds (count);
-        count++;
+        
+        // count up or down depending on the value of count_up
+        if (count_up)
+            count++;
+        else
+            count--;
+        
+        // interval to next count    
         wait(count_interval);
         
-        if (count_up && count % 16 == 0)
-          count_interval *= 2;
+        // update the count interval when the LED count gets to 16
+        // decrement the interval every 16 lots of counts
+        if (speed_up && count % 16 == 0)
+          count_interval /= 2;
         else if (count % 16 == 0)
-          count_interval /= 2;   
+          count_interval *= 2;   
         
         if (count % 256 == 0)
+          speed_up = ~ speed_up;
+        
+        // change the count direction every 32 lots of counts
+        if (count % 512 == 0)
           count_up = ~ count_up;
+          
     }
 }