Timed switch

Dependencies:   mbed

Revision:
1:a523eaac3b7c
Parent:
0:5d9b6304f982
--- a/timed_switch.cpp	Sat Feb 11 18:39:32 2012 +0000
+++ b/timed_switch.cpp	Sun Mar 15 17:19:54 2015 +0000
@@ -1,36 +1,42 @@
 #include "mbed.h"
 #include "timed_switch.h"
 
-int main (void) 
-{    
-    t.start ();
-    
-    while (True) 
-    {
-        if (True == button)
-        {
+void flash_led()
+{
+    myled = LedOn;
+    wait(WAIT_ON);
+    myled = LedOff;
+    wait(WAIT_OFF);
+};
+
+void debounce()
+{
+    // debouncing
+    while (button) {
+        wait(WAIT_TIME);
+    }
+};
+
+int main(void)
+{
+    t.start();
+
+    while (true) {
+        if (button) {
             // start counting time when the button is pressed
-            t.reset ();
+            t.reset();
             
-            // debouncing
-            while (True == button)
-            {
-                wait (wait_time);   
-            }
-            
+            debounce();
+
             // remove the debouncing delay from the counted time
-            button_holded = t.read_ms () - time_correction;
-            
-            /* start couting time again and flashing the LED 
+            button_hold_duration = t.read_ms() - time_correction;
+
+            /* start couting time again and flash the LED
                after the button is released */
-            t.reset ();
-            while (t.read_ms () < button_holded)
-            {
-                myled = LedOn;
-                wait (wait_on);
-                myled = LedOff;
-                wait (wait_off);
+            t.reset();
+            while (t.read_ms() < button_hold_duration) {
+                flash_led();
             }
         }
-    }   
+    }
 }
\ No newline at end of file