#include <LiquidCrystal.h>
#include <Wire.h>
#include <TimeLib.h>
#include <DS1307RTC.h>
const int ledPin[] = {6,7,8,9};
const int btn[] = {10,11,12,13};
const int rs = 5, en = 4, d4 = 3, d5 = 2, d6 = 1, d7 = 0;
int timerStart[4][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int timerFinish[4][3] = {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}};
int count = 0, flashCountCol = 5, flashCountRow = 0;
tmElements_t tm;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
lcd.begin(16, 2);
for (int i = 6; i < 10; i++) {
pinMode(i, OUTPUT);
}
for (int i = 10; i < 14; i++) {
pinMode(i, INPUT_PULLUP);
}
}
void loop() {
if (RTC.read(tm)) {
switch (count) {
case 0:
scrn("Time: ", "Date: ");
break;
case 1:
timer("Start1: ", "Finish: ", timerStart, timerFinish);
break;
case 2:
timer("Start2: ", "Finish: ", timerStart, timerFinish);
break;
case 3:
timer("Start3: ", "Finish: ", timerStart, timerFinish);
break;
case 4:
timer("Start4: ", "Finish: ", timerStart, timerFinish);
break;
}
} else {
lcd.setCursor(0, 0);
lcd.print("RTC read error");
}
delay(150);
if(!digitalRead(btn[0])){
while(!digitalRead(10));
delay(10);
count += 1;
if(count ==5) count = 0;
flashCountCol = 5;
flashCountRow = 0;
}
if( !digitalRead(btn[1])){
while(!digitalRead(11));
delay(10);
flashCountCol += 3;
if(flashCountCol >15 && flashCountRow == 1){ flashCountCol = 5; flashCountRow = 0;}
else if(flashCountCol >15){ flashCountCol = 8; flashCountRow = 1;}
}
if(count > 0)
switch(flashCountCol){
case 8: flash(flashCountCol, flashCountRow);
break;
case 11: flash(flashCountCol, flashCountRow);
break;
case 14: flash(flashCountCol, flashCountRow);
break;
}
if(!digitalRead(btn[2])){
delay(10);
if(flashCountCol == 8 && flashCountRow ==0){
timerStart[count - 1][0] += 1;
if(timerStart[count - 1][0] == 25) timerStart[count - 1][0] = 0;
}
else if(flashCountCol == 11 && flashCountRow ==0){
timerStart[count - 1][1] += 1;
if(timerStart[count - 1][1] == 60) timerStart[count - 1][1] = 0;
}
else if(flashCountCol == 14 && flashCountRow ==0){
timerStart[count - 1][2] += 1;
if(timerStart[count - 1][2] == 60) timerStart[count - 1][2] = 0;
}
else if(flashCountCol == 8 && flashCountRow ==1){
timerFinish[count - 1][0] += 1;
if(timerStart[count - 1][0] == 25) timerStart[count - 1][0] = 0;
}
else if(flashCountCol == 11 && flashCountRow ==1){
timerFinish[count - 1][1] += 1;
if(timerStart[count - 1][1] == 60) timerStart[count - 1][1] = 0;
}
else if(flashCountCol == 14 && flashCountRow ==1){
timerFinish[count - 1][2] += 1;
if(timerStart[count - 1][2] == 60) timerStart[count - 1][2] = 0;
}
}
if(!digitalRead(btn[3])){
delay(10);
if(flashCountCol == 8 && flashCountRow ==0){
timerStart[count - 1][0] -= 1;
if(timerStart[count - 1][0] == -1) timerStart[count - 1][0] = 24;
}
else if(flashCountCol == 11 && flashCountRow ==0){
timerStart[count - 1][1] -= 1;
if(timerStart[count - 1][1] == -1) timerStart[count - 1][1] = 59;
}
else if(flashCountCol == 14 && flashCountRow ==0){
timerStart[count - 1][2] -= 1;
if(timerStart[count - 1][2] == -1) timerStart[count - 1][2] = 59;
}
else if(flashCountCol == 8 && flashCountRow ==1){
timerFinish[count - 1][0] -= 1;
if(timerFinish[count - 1][0] == -1) timerFinish[count - 1][0] = 24;
}
else if(flashCountCol == 11 && flashCountRow ==1){
timerFinish[count - 1][1] -= 1;
if(timerFinish[count - 1][1] == -1) timerFinish[count - 1][1] = 59;
}
else if(flashCountCol == 14 && flashCountRow ==1){
timerFinish[count - 1][2] -= 1;
if(timerFinish[count - 1][2] == -1) timerFinish[count - 1][2] = 59;
}
}
if(timerStart[0][0] == tm.Hour && timerStart[0][1] == tm.Minute && timerStart[0][2] == tm.Second){
digitalWrite(ledPin[0], HIGH);
}
if(timerFinish[0][0] == tm.Hour && timerFinish[0][1] == tm.Minute && timerFinish[0][2] == tm.Second){
digitalWrite(ledPin[0], LOW);
}
if(timerStart[1][0] == tm.Hour && timerStart[1][1] == tm.Minute && timerStart[1][2] == tm.Second){
digitalWrite(ledPin[1], HIGH);
}
if(timerFinish[1][0] == tm.Hour && timerFinish[1][1] == tm.Minute && timerFinish[1][2] == tm.Second){
digitalWrite(ledPin[1], LOW);
}
if(timerStart[2][0] == tm.Hour && timerStart[2][1] == tm.Minute && timerStart[2][2] == tm.Second){
digitalWrite(ledPin[2], HIGH);
}
if(timerFinish[2][0] == tm.Hour && timerFinish[2][1] == tm.Minute && timerFinish[2][2] == tm.Second){
digitalWrite(ledPin[2], LOW);
}
if(timerStart[3][0] == tm.Hour && timerStart[3][1] == tm.Minute && timerStart[3][2] == tm.Second){
digitalWrite(ledPin[3], HIGH);
}
if(timerFinish[3][0] == tm.Hour && timerFinish[3][1] == tm.Minute && timerFinish[3][2] == tm.Second){
digitalWrite(ledPin[3], LOW);
}
}
void flash(int posCol, int posRow){
lcd.setCursor(posCol,posRow);
lcd.print(" ");
delay(150);
}
void scrn(String title1, String title2) {
lcd.setCursor(0, 0);
lcd.print(title1);
print2digits(tm.Hour);
lcd.print(":");
print2digits(tm.Minute);
lcd.print(":");
print2digits(tm.Second);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(title2);
lcd.print(tmYearToCalendar(tm.Year));
lcd.print("/");
print2digits(tm.Month);
lcd.print("/");
print2digits(tm.Day);
}
void timer(String title1, String title2,int timerStart[4][3],int timerFinish[4][3]){
lcd.setCursor(0, 0);
lcd.print(title1);
for(int i = 0; i<3;i++){
print2digits(timerStart[count -1][i]);
if(i < 2) lcd.print(":");
}
lcd.setCursor(0, 1);
lcd.print(title2);
for(int i = 0; i<3;i++){
print2digits(timerFinish[count -1][i]);
if(i < 2) lcd.print(":");
}
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
lcd.print('0');
}
lcd.print(number);
}