Configurable countdown timer

Dependencies:   mbed PinDetect TextLCD

Revision:
2:ecbc6a14824c
Parent:
1:0ed57c2fd528
Child:
3:3facd92a3f37
--- a/main.cpp	Sun May 24 23:02:30 2020 +0000
+++ b/main.cpp	Sat Jun 06 23:17:30 2020 +0000
@@ -7,32 +7,64 @@
 // Note: VCC=5V, V0=0V, via 330 resistor)
 TextLCD lcd(p15, p16, p17, p18, p19, p20);
 
-DigitalOut VO(p21); // pin put low
-DigitalOut RW(p22); // pin put low
+Ticker flipper;
+
+volatile int t1s = 0;
 
-int i = 0;
+volatile char seconds = 0;
+volatile char minutes = 25;
+volatile char hours = 0;
+volatile char alarm = 1;   // alarm set on/off ==> enables buzzer
  
-void count() {
+void update_lcd() {
     lcd.locate (0,1);
-    lcd.printf("%d",i);
-    i++;
+    lcd.printf("%02d:%02d:%02d",hours, minutes, seconds);
+}
+
+void flip() {
+    t1s = 1;
+}
+
+void count_down(){
+  
+  if(seconds == 0 && minutes == 0 && hours == 0)
+      return;
+  
+  if(seconds != 0) {
+      seconds -= 1;
+      return;
+  }
+  
+  if(minutes != 0){
+      minutes -= 1;
+      seconds = 59;
+      return;      
+  }
+  
+  if(hours != 0){
+      hours -= 1;
+      minutes = 59;
+      seconds = 59;
+      return;    
+  }    
 }
  
 int main () {
-
-    VO = 0;
-    RW = 0;
     
     // Clean screen
     lcd.cls();
-    
-    lcd.printf("Hello World!");
+    lcd.printf("Countdown");
 
+    update_lcd(); 
+          
+    flipper.attach(&flip, 1.0); // interval 1 second
+  
     while(1) {
-        myled = 1;
-        wait(0.5);
-        myled = 0;
-        wait(0.5);
-        count(); 
+        if (t1s){
+          t1s = 0;
+          myled = !myled;
+          count_down();
+          update_lcd(); 
+        }
    }
 }
\ No newline at end of file