Timed switch

Dependencies:   mbed

Committer:
faif
Date:
Sun Mar 15 17:19:54 2015 +0000
Revision:
1:a523eaac3b7c
Parent:
0:5d9b6304f982
revised

Who changed what in which revision?

UserRevisionLine numberNew contents of line
faif 0:5d9b6304f982 1 #include "mbed.h"
faif 0:5d9b6304f982 2 #include "timed_switch.h"
faif 0:5d9b6304f982 3
faif 1:a523eaac3b7c 4 void flash_led()
faif 1:a523eaac3b7c 5 {
faif 1:a523eaac3b7c 6 myled = LedOn;
faif 1:a523eaac3b7c 7 wait(WAIT_ON);
faif 1:a523eaac3b7c 8 myled = LedOff;
faif 1:a523eaac3b7c 9 wait(WAIT_OFF);
faif 1:a523eaac3b7c 10 };
faif 1:a523eaac3b7c 11
faif 1:a523eaac3b7c 12 void debounce()
faif 1:a523eaac3b7c 13 {
faif 1:a523eaac3b7c 14 // debouncing
faif 1:a523eaac3b7c 15 while (button) {
faif 1:a523eaac3b7c 16 wait(WAIT_TIME);
faif 1:a523eaac3b7c 17 }
faif 1:a523eaac3b7c 18 };
faif 1:a523eaac3b7c 19
faif 1:a523eaac3b7c 20 int main(void)
faif 1:a523eaac3b7c 21 {
faif 1:a523eaac3b7c 22 t.start();
faif 1:a523eaac3b7c 23
faif 1:a523eaac3b7c 24 while (true) {
faif 1:a523eaac3b7c 25 if (button) {
faif 0:5d9b6304f982 26 // start counting time when the button is pressed
faif 1:a523eaac3b7c 27 t.reset();
faif 0:5d9b6304f982 28
faif 1:a523eaac3b7c 29 debounce();
faif 1:a523eaac3b7c 30
faif 0:5d9b6304f982 31 // remove the debouncing delay from the counted time
faif 1:a523eaac3b7c 32 button_hold_duration = t.read_ms() - time_correction;
faif 1:a523eaac3b7c 33
faif 1:a523eaac3b7c 34 /* start couting time again and flash the LED
faif 0:5d9b6304f982 35 after the button is released */
faif 1:a523eaac3b7c 36 t.reset();
faif 1:a523eaac3b7c 37 while (t.read_ms() < button_hold_duration) {
faif 1:a523eaac3b7c 38 flash_led();
faif 0:5d9b6304f982 39 }
faif 0:5d9b6304f982 40 }
faif 1:a523eaac3b7c 41 }
faif 0:5d9b6304f982 42 }