Configurable countdown timer
Dependencies: mbed PinDetect TextLCD
Revision 3:3facd92a3f37, committed 2020-06-07
- Comitter:
- MarceloSalazar
- Date:
- Sun Jun 07 23:37:02 2020 +0000
- Parent:
- 2:ecbc6a14824c
- Commit message:
- Push buttons and buzzer working
Changed in this revision
| PinDetect.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r ecbc6a14824c -r 3facd92a3f37 PinDetect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Sun Jun 07 23:37:02 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/AjK/code/PinDetect/#cb3afc45028b
diff -r ecbc6a14824c -r 3facd92a3f37 main.cpp
--- a/main.cpp Sat Jun 06 23:17:30 2020 +0000
+++ b/main.cpp Sun Jun 07 23:37:02 2020 +0000
@@ -1,16 +1,29 @@
#include "mbed.h"
#include "TextLCD.h"
+#include "PinDetect.h"
DigitalOut myled(LED1);
+DigitalOut buzzer(p24);
+PinDetect pb1(p23); // Increase minutes
+PinDetect pb2(p22); // Decrease minutes
+PinDetect pb3(p21); // Start / pause
+
+
//TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2);
// Note: VCC=5V, V0=0V, via 330 resistor)
TextLCD lcd(p15, p16, p17, p18, p19, p20);
Ticker flipper;
+Timeout wait_time;
+volatile int start = 0;
volatile int t1s = 0;
+volatile unsigned char a = 0;
+volatile unsigned char b = 0;
+volatile unsigned char c = 0;
+
volatile char seconds = 0;
volatile char minutes = 25;
volatile char hours = 0;
@@ -18,7 +31,7 @@
void update_lcd() {
lcd.locate (0,1);
- lcd.printf("%02d:%02d:%02d",hours, minutes, seconds);
+ lcd.printf("%02d:%02d:%02d %2x%2x%2x",hours, minutes, seconds,a,b,c);
}
void flip() {
@@ -48,23 +61,77 @@
return;
}
}
-
-int main () {
-
+
+
+// Buzzer beep for t seconds
+void beep(float t) {
+ buzzer = 1;
+ wait(t);
+ buzzer = 0;
+}
+
+void beepn(int i, float t) {
+ while(i --){
+ buzzer = 1;
+ wait(t);
+ buzzer = 0;
+ wait(t);
+ }
+}
+
+// Callback routine is interrupt activated by a debounced pb2 hit
+void pb1_hit_callback (void) {
+ minutes++;
+}
+// Callback routine is interrupt activated by a debounced pb2 hit
+void pb2_hit_callback (void) {
+ minutes--;
+}
+// Callback routine is interrupt activated by a debounced pb3 hit
+void pb3_hit_callback (void) {
+ start = (start + 1) & 0x01;
+}
+
+
+int main () {
+
// Clean screen
lcd.cls();
lcd.printf("Countdown");
+ beep(0.1); // init buzzer
+
update_lcd();
-
+
flipper.attach(&flip, 1.0); // interval 1 second
+ pb1.attach_deasserted(&pb1_hit_callback);
+ pb2.attach_deasserted(&pb2_hit_callback);
+ pb3.attach_deasserted(&pb3_hit_callback);
+
+ pb1.setSampleFrequency();
+ pb2.setSampleFrequency();
+ pb3.setSampleFrequency();
+
while(1) {
if (t1s){
t1s = 0;
myled = !myled;
- count_down();
- update_lcd();
- }
+
+ if(start)
+ count_down();
+
+ update_lcd();
+
+ // Last 5 seconds
+ if(seconds > 0 && seconds <= 5 && minutes == 0 && hours == 0) {
+ beep(0.2);
+ }
+ // Countdown finished
+ else if(seconds == 0 && minutes == 0 && hours == 0) {
+ flipper.detach();
+ beepn(5, 0.2);
+ }
+ }
}
}
\ No newline at end of file