Timed switch

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
faif
Date:
Sun Mar 15 17:19:54 2015 +0000
Parent:
0:5d9b6304f982
Commit message:
revised

Changed in this revision

mbed.bld Show annotated file Show diff for this revision Revisions of this file
timed_switch.cpp Show annotated file Show diff for this revision Revisions of this file
timed_switch.h Show annotated file Show diff for this revision Revisions of this file
--- a/mbed.bld	Sat Feb 11 18:39:32 2012 +0000
+++ b/mbed.bld	Sun Mar 15 17:19:54 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
+http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
\ No newline at end of file
--- 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
--- a/timed_switch.h	Sat Feb 11 18:39:32 2012 +0000
+++ b/timed_switch.h	Sun Mar 15 17:19:54 2015 +0000
@@ -2,17 +2,19 @@
 #define TIMED_SWITCH_H
 
 enum {LedOff = 0, LedOn = 1};
-enum {False, True};
+
+const float WAIT_TIME = 0.2;    // to avoid bouncing
+const float WAIT_ON = 0.5;
+const float WAIT_OFF = 0.5;
+const unsigned short time_correction = WAIT_TIME * 1000; // correction in msec.
 
-const float wait_time = 0.2;    // to avoid bouncing
-const float wait_on = 0.5;
-const float wait_off = 0.5;
-const unsigned short time_correction = wait_time * 1000; // correction in msec.
+Timer t;                               // for tracking time
+unsigned int button_hold_duration;     // for tracking how long was the button holded (in msec)
 
-Timer t;                        // for tracking time
-unsigned int button_holded;     // for tracking how long was the button holded
+DigitalIn button(p20);
+DigitalOut myled(LED3);
 
-DigitalIn button (p19);
-DigitalOut myled (LED3);
+void flash_led();
+void debounce();
 
 #endif