Marcelo Salazar / Mbed 2 deprecated countdown

Dependencies:   mbed PinDetect TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include "PinDetect.h"
00004 
00005 DigitalOut myled(LED1);
00006 
00007 DigitalOut buzzer(p24);
00008 PinDetect pb1(p23); // Increase minutes
00009 PinDetect pb2(p22); // Decrease minutes
00010 PinDetect pb3(p21); // Start / pause
00011 
00012 
00013 //TextLCD(PinName rs, PinName e, PinName d4, PinName d5, PinName d6, PinName d7, LCDType type = LCD16x2);
00014 // Note: VCC=5V, V0=0V, via 330 resistor)
00015 TextLCD lcd(p15, p16, p17, p18, p19, p20);
00016 
00017 Ticker flipper;
00018 Timeout wait_time;
00019 
00020 volatile int start = 0;
00021 volatile int t1s = 0;
00022 
00023 volatile unsigned char a = 0;
00024 volatile unsigned char b = 0;
00025 volatile unsigned char c = 0;
00026 
00027 volatile char seconds = 0;
00028 volatile char minutes = 25;
00029 volatile char hours = 0;
00030 volatile char alarm = 1;   // alarm set on/off ==> enables buzzer
00031  
00032 void update_lcd() {
00033     lcd.locate (0,1);
00034     lcd.printf("%02d:%02d:%02d  %2x%2x%2x",hours, minutes, seconds,a,b,c);
00035 }
00036 
00037 void flip() {
00038     t1s = 1;
00039 }
00040 
00041 void count_down(){
00042   
00043   if(seconds == 0 && minutes == 0 && hours == 0)
00044       return;
00045   
00046   if(seconds != 0) {
00047       seconds -= 1;
00048       return;
00049   }
00050   
00051   if(minutes != 0){
00052       minutes -= 1;
00053       seconds = 59;
00054       return;      
00055   }
00056   
00057   if(hours != 0){
00058       hours -= 1;
00059       minutes = 59;
00060       seconds = 59;
00061       return;    
00062   }    
00063 }
00064 
00065 
00066 // Buzzer beep for t seconds
00067 void beep(float t) {
00068     buzzer = 1;
00069     wait(t);
00070     buzzer = 0;
00071 }
00072 
00073 void beepn(int i, float t) {
00074     while(i --){
00075         buzzer = 1;
00076         wait(t);
00077         buzzer = 0;
00078         wait(t);
00079     }
00080 }
00081 
00082 // Callback routine is interrupt activated by a debounced pb2 hit
00083 void pb1_hit_callback (void) {
00084     minutes++;
00085 }
00086 // Callback routine is interrupt activated by a debounced pb2 hit
00087 void pb2_hit_callback (void) {
00088     minutes--;
00089 }
00090 // Callback routine is interrupt activated by a debounced pb3 hit
00091 void pb3_hit_callback (void) {
00092     start = (start + 1) & 0x01;
00093 }
00094 
00095 
00096 int main () {   
00097         
00098     // Clean screen
00099     lcd.cls();
00100     lcd.printf("Countdown");
00101 
00102     beep(0.1); // init buzzer
00103     
00104     update_lcd(); 
00105                   
00106     flipper.attach(&flip, 1.0); // interval 1 second
00107   
00108     pb1.attach_deasserted(&pb1_hit_callback);
00109     pb2.attach_deasserted(&pb2_hit_callback);
00110     pb3.attach_deasserted(&pb3_hit_callback);
00111     
00112     pb1.setSampleFrequency();
00113     pb2.setSampleFrequency();
00114     pb3.setSampleFrequency();
00115     
00116     while(1) {
00117         if (t1s){
00118           t1s = 0;
00119           myled = !myled;
00120           
00121           if(start)
00122               count_down();
00123               
00124           update_lcd();
00125           
00126           // Last 5 seconds
00127           if(seconds > 0 && seconds <= 5 && minutes == 0 && hours == 0) {
00128               beep(0.2);
00129           }
00130           // Countdown finished
00131           else if(seconds == 0 && minutes == 0 && hours == 0) {
00132             flipper.detach();
00133             beepn(5, 0.2);
00134           }
00135          }           
00136    }
00137 }