Advertisement
phuongpupil2

clock_kong final ver-1

Nov 13th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 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. #define NOTE_C4 262
  8. #define NOTE_D4 294
  9. #define NOTE_E4 330
  10. #define NOTE_F4 349
  11. #define NOTE_G4 392
  12. #define NOTE_A4 440
  13. #define NOTE_AS4 466
  14. #define NOTE_C5 523
  15.  
  16. int melody[] = {
  17.  
  18. NOTE_C4,4, NOTE_C4,8,
  19. NOTE_D4,-4, NOTE_C4,-4, NOTE_F4,-4,
  20. NOTE_E4,-2, NOTE_C4,4, NOTE_C4,8,
  21. NOTE_D4,-4, NOTE_C4,-4, NOTE_G4,-4,
  22. NOTE_F4,-2, NOTE_C4,4, NOTE_C4,8,
  23. NOTE_C5,-4, NOTE_A4,-4, NOTE_F4,-4,
  24. NOTE_E4,-4, NOTE_D4,-4, NOTE_AS4,4, NOTE_AS4,8,
  25. NOTE_A4,-4, NOTE_F4,-4, NOTE_G4,-4,
  26. NOTE_F4,-2,
  27.  
  28. };
  29. int buzzer = A1;
  30. int notes = sizeof(melody) / sizeof(melody[0]) / 2;
  31. int wholenote;
  32. int divider = 0, noteDuration = 0;
  33. int baothuc1lanthoi;
  34.  
  35.  
  36. const int ledPin = A0;
  37. unsigned long currentMillis;
  38. int ledState = LOW;
  39. unsigned long previousMillis = 0;
  40. const long interval = 500;
  41. unsigned long Millis1;
  42.  
  43. const int Mode_btn = 1;
  44. const int btn2 = A2;
  45. const int btn3 = A3;
  46. const int btn4 = A4;
  47.  
  48. int State_btn1 = 0;
  49. int State_btn2 = 0;
  50. int State_btn3 = 0;
  51. int State_btn4 = 0;
  52. int PushCounter_btn1 = 0;
  53. int lastState_btn1 = 1;
  54. int lastState_btn2 = 1;
  55. int lastState_btn3 = 1;
  56.  
  57. unsigned int Time;
  58. unsigned int Time_alarm;
  59. unsigned int hrs_alarm = 21;
  60. unsigned int min_alarm = 26;
  61.  
  62.  
  63. void setup()
  64. {
  65. pinMode(ledPin, OUTPUT);
  66. pinMode(buzzer, OUTPUT);
  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. pinMode(Mode_btn, INPUT_PULLUP);
  77. pinMode(btn2, INPUT_PULLUP);
  78. pinMode(btn3, INPUT_PULLUP);
  79. pinMode(btn4, INPUT_PULLUP);
  80.  
  81. Serial.begin(57600);
  82. }
  83.  
  84. void sound_alarm(int speed) {
  85.  
  86. wholenote = (60000 * 4) / speed;
  87. // iterate over the notes of the melody.
  88. // Remember, the array is twice the number of notes (notes + durations)
  89. for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
  90.  
  91. // calculates the duration of each note
  92. divider = melody[thisNote + 1];
  93. if (divider > 0) {
  94. // regular note, just proceed
  95. noteDuration = (wholenote) / divider;
  96. } else if (divider < 0) {
  97. // dotted notes are represented with negative durations!!
  98. noteDuration = (wholenote) / abs(divider);
  99. noteDuration *= 1.5; // increases the duration in half for dotted notes
  100. }
  101.  
  102. // we only play the note for 90% of the duration, leaving 10% as a pause
  103. tone(buzzer, melody[thisNote], noteDuration * 0.9);
  104.  
  105. // Wait for the specief duration before playing the next note.
  106. delay(noteDuration);
  107.  
  108. // stop the waveform generation before the next note.
  109. noTone(buzzer);
  110. }
  111. }
  112.  
  113. void loop(){
  114. // Led nhấp nháy theo giây
  115. unsigned long currentMillis = millis();
  116. if (currentMillis - previousMillis >= interval)
  117. {
  118. previousMillis = currentMillis;
  119. if (ledState == LOW)
  120. {
  121. ledState = HIGH;
  122. }
  123. else
  124. {
  125. ledState = LOW;
  126. }
  127. digitalWrite(ledPin, ledState);
  128. }
  129. // Đặt giờ
  130. tmElements_t tm;
  131. if (RTC.read(tm))
  132. {
  133. Time = tm.Hour * 100 + tm.Minute;
  134. }
  135.  
  136. // Đặt giờ báo thức __________________________________________________
  137. if (hrs_alarm == 24){
  138. hrs_alarm = 0;
  139. }
  140. if (min_alarm == 60){
  141. min_alarm = 0;
  142. hrs_alarm++;
  143. }
  144. Time_alarm = hrs_alarm*100+ min_alarm;
  145. //__________________________________________________
  146.  
  147. if (millis() - Millis1 >= 500){
  148. State_btn1 = digitalRead(Mode_btn); Serial.println(PushCounter_btn1);
  149. State_btn2 = digitalRead(btn2); Serial.print(State_btn2);
  150. State_btn3 = digitalRead(btn3); Serial.print(State_btn3);
  151. State_btn4 = digitalRead(btn4); Serial.println(State_btn4);
  152. if (State_btn1 != lastState_btn1) {
  153. if (State_btn1 == HIGH) {
  154. PushCounter_btn1++;
  155. }
  156. }
  157. lastState_btn1 = State_btn1;
  158. Millis1 = millis();
  159. }
  160. if (PushCounter_btn1 % 2 == 0) { //Chia hết cho 2 ~~> hiện giờ
  161. Display.setNumber(Time,2);
  162. }
  163. else if (PushCounter_btn1 % 2 == 1) { //Chia 2 dư 1 ~~> chỉnh báo thức
  164. Display.setNumber(Time_alarm,2);
  165. if (State_btn2 != lastState_btn2)
  166. {
  167. if (State_btn2 == HIGH) {
  168. hrs_alarm= hrs_alarm +1;}
  169. lastState_btn2 = State_btn2;
  170. }
  171. if (State_btn3 != lastState_btn3)
  172. {
  173. if (State_btn3 == HIGH) {
  174. min_alarm= min_alarm +1;}
  175. lastState_btn3 = State_btn3;
  176. }
  177. }
  178. if (Time == Time_alarm) {
  179. if (baothuc1lanthoi == 0)
  180. {
  181. sound_alarm(140);
  182. sound_alarm(160);
  183. sound_alarm(190);
  184. sound_alarm(220);
  185. baothuc1lanthoi++;
  186. }
  187. }
  188. Display.refreshDisplay();
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement