Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SevSeg.h"
- #include <Wire.h>
- #include <TimeLib.h>
- #include <DS1307RTC.h>
- SevSeg Display;
- #define NOTE_C4 262
- #define NOTE_D4 294
- #define NOTE_E4 330
- #define NOTE_F4 349
- #define NOTE_G4 392
- #define NOTE_A4 440
- #define NOTE_AS4 466
- #define NOTE_C5 523
- int melody[] = {
- NOTE_C4,4, NOTE_C4,8,
- NOTE_D4,-4, NOTE_C4,-4, NOTE_F4,-4,
- NOTE_E4,-2, NOTE_C4,4, NOTE_C4,8,
- NOTE_D4,-4, NOTE_C4,-4, NOTE_G4,-4,
- NOTE_F4,-2, NOTE_C4,4, NOTE_C4,8,
- NOTE_C5,-4, NOTE_A4,-4, NOTE_F4,-4,
- NOTE_E4,-4, NOTE_D4,-4, NOTE_AS4,4, NOTE_AS4,8,
- NOTE_A4,-4, NOTE_F4,-4, NOTE_G4,-4,
- NOTE_F4,-2,
- };
- int buzzer = A1;
- int notes = sizeof(melody) / sizeof(melody[0]) / 2;
- int wholenote;
- int divider = 0, noteDuration = 0;
- int baothuc1lanthoi;
- const int ledPin = A0;
- unsigned long currentMillis;
- int ledState = LOW;
- unsigned long previousMillis = 0;
- const long interval = 500;
- unsigned long Millis1;
- const int Mode_btn = 1;
- const int btn2 = A2;
- const int btn3 = A3;
- const int btn4 = A4;
- int State_btn1 = 0;
- int State_btn2 = 0;
- int State_btn3 = 0;
- int State_btn4 = 0;
- int PushCounter_btn1 = 0;
- int lastState_btn1 = 1;
- int lastState_btn2 = 1;
- int lastState_btn3 = 1;
- unsigned int Time;
- unsigned int Time_alarm;
- unsigned int hrs_alarm = 21;
- unsigned int min_alarm = 26;
- void setup()
- {
- pinMode(ledPin, OUTPUT);
- pinMode(buzzer, OUTPUT);
- byte numDigits = 4;
- byte digitPins[] = {10, 11, 12, 13};
- byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 9};
- bool resistorsOnSegments = true;
- bool updateWithDelaysIn = true;
- byte hardwareConfig = COMMON_ANODE;
- Display.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
- Display.setBrightness(100);
- pinMode(Mode_btn, INPUT_PULLUP);
- pinMode(btn2, INPUT_PULLUP);
- pinMode(btn3, INPUT_PULLUP);
- pinMode(btn4, INPUT_PULLUP);
- Serial.begin(57600);
- }
- void sound_alarm(int speed) {
- wholenote = (60000 * 4) / speed;
- // iterate over the notes of the melody.
- // Remember, the array is twice the number of notes (notes + durations)
- for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
- // calculates the duration of each note
- divider = melody[thisNote + 1];
- if (divider > 0) {
- // regular note, just proceed
- noteDuration = (wholenote) / divider;
- } else if (divider < 0) {
- // dotted notes are represented with negative durations!!
- noteDuration = (wholenote) / abs(divider);
- noteDuration *= 1.5; // increases the duration in half for dotted notes
- }
- // we only play the note for 90% of the duration, leaving 10% as a pause
- tone(buzzer, melody[thisNote], noteDuration * 0.9);
- // Wait for the specief duration before playing the next note.
- delay(noteDuration);
- // stop the waveform generation before the next note.
- noTone(buzzer);
- }
- }
- void loop(){
- // Led nhấp nháy theo giây
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval)
- {
- previousMillis = currentMillis;
- if (ledState == LOW)
- {
- ledState = HIGH;
- }
- else
- {
- ledState = LOW;
- }
- digitalWrite(ledPin, ledState);
- }
- // Đặt giờ
- tmElements_t tm;
- if (RTC.read(tm))
- {
- Time = tm.Hour * 100 + tm.Minute;
- }
- // Đặt giờ báo thức __________________________________________________
- if (hrs_alarm == 24){
- hrs_alarm = 0;
- }
- if (min_alarm == 60){
- min_alarm = 0;
- hrs_alarm++;
- }
- Time_alarm = hrs_alarm*100+ min_alarm;
- //__________________________________________________
- if (millis() - Millis1 >= 500){
- State_btn1 = digitalRead(Mode_btn); Serial.println(PushCounter_btn1);
- State_btn2 = digitalRead(btn2); Serial.print(State_btn2);
- State_btn3 = digitalRead(btn3); Serial.print(State_btn3);
- State_btn4 = digitalRead(btn4); Serial.println(State_btn4);
- if (State_btn1 != lastState_btn1) {
- if (State_btn1 == HIGH) {
- PushCounter_btn1++;
- }
- }
- lastState_btn1 = State_btn1;
- Millis1 = millis();
- }
- if (PushCounter_btn1 % 2 == 0) { //Chia hết cho 2 ~~> hiện giờ
- Display.setNumber(Time,2);
- }
- else if (PushCounter_btn1 % 2 == 1) { //Chia 2 dư 1 ~~> chỉnh báo thức
- Display.setNumber(Time_alarm,2);
- if (State_btn2 != lastState_btn2)
- {
- if (State_btn2 == HIGH) {
- hrs_alarm= hrs_alarm +1;}
- lastState_btn2 = State_btn2;
- }
- if (State_btn3 != lastState_btn3)
- {
- if (State_btn3 == HIGH) {
- min_alarm= min_alarm +1;}
- lastState_btn3 = State_btn3;
- }
- }
- if (Time == Time_alarm) {
- if (baothuc1lanthoi == 0)
- {
- sound_alarm(140);
- sound_alarm(160);
- sound_alarm(190);
- sound_alarm(220);
- baothuc1lanthoi++;
- }
- }
- Display.refreshDisplay();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement