Guest User

Untitled

a guest
Nov 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #include "mbed.h"
  2. #define Close 0
  3. #define Open 1
  4. #define Alarm 2
  5.  
  6. Timer timer2;
  7. PwmOut Buzzer(p21);
  8. DigitalOut Door_LED(LED1);
  9. DigitalOut warning(LED4);
  10.  
  11. InterruptIn switchEvent(p8);
  12. InterruptIn Door_Stat(p15);
  13.  
  14. int status = 0;
  15. int timer_status = 0;
  16.  
  17.  
  18. void Buzzer_ctrl(int n)
  19. {
  20. if(n == 1)
  21. Buzzer = 0.5; // buzzer on
  22. else
  23. Buzzer = 0; // buzzer off
  24.  
  25. }
  26.  
  27. void Alarm_off()
  28. {
  29. // debouncing
  30. wait_ms(50);
  31. if(switchEvent == 0)
  32. {
  33. Buzzer_ctrl(0);
  34. }
  35. }
  36.  
  37. void Door_Closed()
  38. {
  39. wait_ms(50);
  40. if(Door_Stat == 1) //Case of hole sensor indicate 1(closed)
  41. {
  42. if(timer2.read_ms()>10000)
  43. status = Alarm;
  44. else
  45. status = Close;
  46.  
  47. timer2.reset();
  48. timer_status = 0;
  49. }
  50.  
  51. }
  52.  
  53. void Door_Opened()
  54. {
  55. wait_ms(50);
  56. if(Door_Stat == 0) //Case of hole sensor indicate 0(open)
  57. status = Open; // go to status Opened
  58. if(timer_status == 0)
  59. {
  60. timer2.start();
  61. timer_status = 1;
  62. }
  63.  
  64.  
  65. }
  66.  
  67.  
  68.  
  69. int main()
  70. {
  71.  
  72.  
  73.  
  74. //buzzer setting
  75. Buzzer.period(0.010);
  76. Buzzer = 0;
  77.  
  78. switchEvent.fall(Alarm_off);
  79.  
  80. Door_Stat.fall(Door_Opened);
  81. Door_Stat.rise(Door_Closed);
  82.  
  83. while (true)
  84. {
  85. if(status == Close)
  86. {
  87. Door_LED = Close;
  88. timer2.reset();
  89. timer_status = 0;
  90. }
  91. else if(status == Open)
  92. {
  93. Door_LED = Open;
  94. //when the open, timer starts.
  95. if(timer2.read_ms()>10000)
  96. Buzzer_ctrl(1);
  97. }
  98.  
  99. else if(status == Alarm)
  100. {
  101. Door_LED = Close;
  102. timer2.reset();
  103. timer_status = 0;
  104. }
  105. else
  106. {
  107. warning = 1;
  108.  
  109. }
  110.  
  111.  
  112. }
  113. }
Add Comment
Please, Sign In to add comment