Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mbed.h"
- #define Close 0
- #define Open 1
- #define Alarm 2
- Timer timer2;
- PwmOut Buzzer(p21);
- DigitalOut Door_LED(LED1);
- DigitalOut warning(LED4);
- InterruptIn switchEvent(p8);
- InterruptIn Door_Stat(p15);
- int status = 0;
- int timer_status = 0;
- void Buzzer_ctrl(int n)
- {
- if(n == 1)
- Buzzer = 0.5; // buzzer on
- else
- Buzzer = 0; // buzzer off
- }
- void Alarm_off()
- {
- // debouncing
- wait_ms(50);
- if(switchEvent == 0)
- {
- Buzzer_ctrl(0);
- }
- }
- void Door_Closed()
- {
- wait_ms(50);
- if(Door_Stat == 1) //Case of hole sensor indicate 1(closed)
- {
- if(timer2.read_ms()>10000)
- status = Alarm;
- else
- status = Close;
- timer2.reset();
- timer_status = 0;
- }
- }
- void Door_Opened()
- {
- wait_ms(50);
- if(Door_Stat == 0) //Case of hole sensor indicate 0(open)
- status = Open; // go to status Opened
- if(timer_status == 0)
- {
- timer2.start();
- timer_status = 1;
- }
- }
- int main()
- {
- //buzzer setting
- Buzzer.period(0.010);
- Buzzer = 0;
- switchEvent.fall(Alarm_off);
- Door_Stat.fall(Door_Opened);
- Door_Stat.rise(Door_Closed);
- while (true)
- {
- if(status == Close)
- {
- Door_LED = Close;
- timer2.reset();
- timer_status = 0;
- }
- else if(status == Open)
- {
- Door_LED = Open;
- //when the open, timer starts.
- if(timer2.read_ms()>10000)
- Buzzer_ctrl(1);
- }
- else if(status == Alarm)
- {
- Door_LED = Close;
- timer2.reset();
- timer_status = 0;
- }
- else
- {
- warning = 1;
- }
- }
- }
Add Comment
Please, Sign In to add comment