phuongpupil2

clock_kong final ver999

Nov 14th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #include "SevSeg.h"
  2. #include <Wire.h>
  3. #include <TimeLib.h>
  4. #include <DS1307RTC.h>
  5. SevSeg Display;
  6.  
  7. //Source https://github.com/robsoncouto/arduino-songs/tree/master/happybirthday
  8. #define NOTE_C4 262 //Đặt tần số còi ~> nốt nhạc
  9. #define NOTE_D4 294
  10. #define NOTE_E4 330
  11. #define NOTE_F4 349
  12. #define NOTE_G4 392
  13. #define NOTE_A4 440
  14. #define NOTE_AS4 466
  15. #define NOTE_C5 523
  16. int melody[] = { //Giai điệu bài Happy Birthday
  17. NOTE_C4,4, NOTE_C4,8,
  18. NOTE_D4,-4, NOTE_C4,-4, NOTE_F4,-4,
  19. NOTE_E4,-2, NOTE_C4,4, NOTE_C4,8,
  20. NOTE_D4,-4, NOTE_C4,-4, NOTE_G4,-4,
  21. NOTE_F4,-2, NOTE_C4,4, NOTE_C4,8,
  22. NOTE_C5,-4, NOTE_A4,-4, NOTE_F4,-4,
  23. NOTE_E4,-4, NOTE_D4,-4, NOTE_AS4,4, NOTE_AS4,8,
  24. NOTE_A4,-4, NOTE_F4,-4, NOTE_G4,-4,
  25. NOTE_F4,-2,
  26. };
  27. int buzzer = A1;
  28. int notes = sizeof(melody) / sizeof(melody[0]) / 2;
  29. int wholenote;
  30. int divider = 0, noteDuration = 0;
  31. int baothuc1lanthoi;
  32. //__________________________________________________
  33.  
  34. const int ledPin = A0; //Led nhấy nháy và millis()
  35. int ledState = LOW;
  36. unsigned long currentMillis;
  37. unsigned long previousMillis = 0;
  38. const long interval = 500;
  39. unsigned long Millis1;
  40.  
  41. // Các nút
  42. const int Mode_btn = 1;
  43. const int btn2 = A2;
  44. const int btn3 = A3;
  45. int State_btn1 = 0;
  46. int State_btn2 = 0;
  47. int State_btn3 = 0;
  48. int PushCounter_btn1 = 0;
  49. int lastState_btn1 = 1;
  50. int lastState_btn2 = 1;
  51. int lastState_btn3 = 1;
  52.  
  53. unsigned int Time;
  54. unsigned int Time_alarm;
  55. unsigned int hrs_alarm = 21; // đặt giá trị giờ báo thức ban đầu
  56. unsigned int min_alarm = 26; // đặt giá trị nút báo thức ban đầu
  57.  
  58. void setup()
  59. {
  60. pinMode(ledPin, OUTPUT);
  61. pinMode(buzzer, OUTPUT);
  62. pinMode(Mode_btn, INPUT_PULLUP);
  63. pinMode(btn2, INPUT_PULLUP);
  64. pinMode(btn3, INPUT_PULLUP);
  65.  
  66. //Setup led 7 segments
  67. byte numDigits = 4;
  68. byte digitPins[] = {10, 11, 12, 13};
  69. byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
  70. bool resistorsOnSegments = true;
  71. bool updateWithDelaysIn = true;
  72. byte hardwareConfig = COMMON_ANODE;
  73. Display.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  74. Display.setBrightness(100);
  75. }
  76.  
  77. //Chương trình con chạy còi báo thức__________________________________________________
  78. void sound_alarm(int speed) {
  79. wholenote = (60000 * 4) / speed;
  80. for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
  81. divider = melody[thisNote + 1];
  82. if (divider > 0) {
  83. noteDuration = (wholenote) / divider;
  84. } else if (divider < 0) {
  85. noteDuration = (wholenote) / abs(divider);
  86. noteDuration *= 1.5;
  87. }
  88. tone(buzzer, melody[thisNote], noteDuration * 0.9); //Chỉ chạy 90% thời lượng, còn lại 10% để dừng
  89. delay(noteDuration); // Dừng 1 tí trước khi lặp lần tiếp theo
  90. noTone(buzzer); // Tắt còi
  91. }
  92. }
  93.  
  94. void loop(){
  95. // Led nhấp nháy theo giây__________________________________________________
  96. unsigned long currentMillis = millis();
  97. if (currentMillis - previousMillis >= interval)
  98. {
  99. previousMillis = currentMillis;
  100. if (ledState == LOW)
  101. {
  102. ledState = HIGH;
  103. }
  104. else
  105. {
  106. ledState = LOW;
  107. }
  108. digitalWrite(ledPin, ledState);
  109. }
  110.  
  111. // Đặt giờ__________________________________________________
  112. tmElements_t tm;
  113. if (RTC.read(tm))
  114. {
  115. Time = tm.Hour * 100 + tm.Minute;
  116. }
  117.  
  118. // Đặt giờ báo thức __________________________________________________
  119. if (hrs_alarm == 24){
  120. hrs_alarm = 0;
  121. }
  122. if (min_alarm == 60){
  123. min_alarm = 0;
  124. hrs_alarm++;
  125. }
  126. Time_alarm = hrs_alarm*100+ min_alarm;
  127.  
  128. //Nhận giá trị các nút__________________________________________________
  129. if (millis() - Millis1 >= 500){
  130. State_btn1 = digitalRead(Mode_btn);
  131. State_btn2 = digitalRead(btn2);
  132. State_btn3 = digitalRead(btn3);
  133. if (State_btn1 != lastState_btn1) {
  134. if (State_btn1 == HIGH) {
  135. PushCounter_btn1++;
  136. }
  137. }
  138. lastState_btn1 = State_btn1;
  139. Millis1 = millis();
  140. }
  141.  
  142. //Chỉnh nút__________________________________________________
  143. if (PushCounter_btn1 % 2 == 0) { // Chia hết cho 2 ~~> hiện giờ
  144. Display.setNumber(Time,2);
  145. }
  146. else { // Chia 2 dư 1 ~~> chỉnh báo thức
  147. Display.setNumber(Time_alarm,2);
  148. if (State_btn2 != lastState_btn2) {
  149. if (State_btn2 == HIGH) {hrs_alarm++;}
  150. lastState_btn2 = State_btn2;
  151. }
  152. if (State_btn3 != lastState_btn3) {
  153. if (State_btn3 == HIGH) {min_alarm++;}
  154. lastState_btn3 = State_btn3;
  155. }
  156. }
  157.  
  158. //Báo thức__________________________________________________
  159. if (Time == Time_alarm) {
  160. if (baothuc1lanthoi == 0){
  161. sound_alarm(140);
  162. sound_alarm(160);
  163. sound_alarm(190);
  164. sound_alarm(220);
  165. baothuc1lanthoi++;
  166. }
  167. }
  168. Display.refreshDisplay();
  169. }
Add Comment
Please, Sign In to add comment