#include "RTClib.h"
#include "pitches.h"
RTC_DS1307 rtc;
#define buzzer 8
const byte buttonPin[] = {10, 11, 12, 13};
const int dataPin = 2; /*DS*/
const int clockPin = 3; /*SHCP*/
const int latchPin = 4; /*STCH*/
int hours, mins, secs, rob, Bstate, Breading;
int Sethours, Setmins, Setsecs;
bool check = false;
bool workSeg = true;
const byte digPatterns[10]={
B00111111, // 0
B00000110, // 1
B01011011, // 2
B01001111, // 3
B01100110, // 4
B01101101, // 5
B01111101, // 6
B00000111, // 7
B01111111, // 8
B01101111 // 9
};
int melody[] = {
NOTE_E7, NOTE_E7, 0, NOTE_E7,
0, NOTE_C7, NOTE_E7, 0,
NOTE_G7, 0, 0, 0,
NOTE_G6, 0, 0, 0
};
void setup() {
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(buzzer, OUTPUT);
for (byte Bpin : buttonPin) pinMode(Bpin, INPUT_PULLUP);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
}
void loop() {
DateTime now = rtc.now();
hours = now.hour();
mins = now.minute();
secs = now.second();
if (workSeg) {
SegmentClock(hours, mins, secs);
if (hours == Sethours && mins == Setmins && check == true)
BuzzerWorking();
}
if (!workSeg) {
ResetSegment(Sethours, Setmins);
Sethours = ButtonHour(Sethours);
Setmins = ButtonMin(Setmins);
CheckButton(true, true);
}
CheckButton(false, false);
ResetButton();
}
void CheckButton(bool cw, bool ck) {
Bstate = digitalRead(buttonPin[3]);
if (Breading == HIGH && Bstate == LOW) {
workSeg = cw;
check = ck;
}
Breading = Bstate;
}
void BuzzerWorking() {
for (int i = 0; i < 16; i++) {
tone(buzzer, melody[i], 83.33);
delay(110);
}
noTone(buzzer);
rob++;
if (rob > 15) {
check = false;
Sethours = 0;
Setmins = 0;
rob = 0;
}
}
int ButtonHour(int h) {
if (digitalRead(buttonPin[2]) == LOW) {
h = h < 23 ? h + 1 : 0;
delay(200);
}
return h;
}
int ButtonMin(int m) {
if (digitalRead(buttonPin[1]) == LOW) {
m = m < 59 ? m + 1 : 0;
delay(200);
}
return m;
}
void ResetButton(){
if (digitalRead(buttonPin[0]) == LOW){
workSeg = true;
check = false;
Sethours = 0;
Setmins = 0;
rob = 0;
delay(200);
}
}
void ResetSegment(int Sethours, int Setmins) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[0]);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[0]);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[Setmins % 10]);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[Setmins / 10]);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[Sethours % 10]);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[Sethours / 10]);
digitalWrite(latchPin, HIGH);
delay(100);
}
void SegmentClock(int hours, int mins, int secs) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[secs % 10]);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[secs / 10]);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[mins % 10]);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[mins / 10]);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[hours % 10]);
shiftOut(dataPin, clockPin, MSBFIRST, ~digPatterns[hours / 10]);
digitalWrite(latchPin, HIGH);
delay(100);
}