#include <MsTimer2.h>
#include <Wire.h>
#include <DS3231.h>
DS3231 clock;RTCDateTime DateTime;
int an, segm,times,i;
byte a[4];
int menu,year,month,day,hour,minute,sec,w,w2;
void setup(){ Wire.begin();clock.begin();MsTimer2::set(2, to_Timer);MsTimer2::start();Serial.begin(9600);
// clock.setDateTime(__DATE__, __TIME__); // Устанавливаем время на часах, основываясь на времени компиляции скетча
pinMode(9,OUTPUT); // D9 === A0
pinMode(10,OUTPUT); // D10 === A1
pinMode(11,OUTPUT); // D11 === A2
pinMode(12,OUTPUT); // D12 === A3
pinMode(2,OUTPUT); // D2 === a
pinMode(3,OUTPUT); // D3 === b
pinMode(4,OUTPUT); // D4 === c
pinMode(5,OUTPUT); // D5 === d
pinMode(6,OUTPUT); // D6 === e
pinMode(7,OUTPUT); // D7 === f
pinMode(8,OUTPUT); // D8 === g
pinMode(13,OUTPUT);// D13 === dp
pinMode(A0,INPUT); // SQW
///// BUTTON //////
pinMode(A1,INPUT); // SET
pinMode(A2,INPUT); // +/sec
pinMode(A3,INPUT); // -/temp
//// текущее время перенос в переменные коррекции времени
DateTime=clock.getDateTime();// опрос времени
readTime();
}
void loop(){ DateTime=clock.getDateTime();// опрос времени
readTime();
if(analogRead(A1)>900){menu++;delay(200);if(menu>4){menu=0;}}
if(menu==0){
if(sec>=20 && sec<=25){times = DateTime.day*100+DateTime.month;
a[0]=times/1000;
a[1]=times/100%10;
a[2]=times/10%10;
a[3]=times%10%10;
digitalWrite(13,LOW);
}
else if(sec>=35 && sec<=40){times = int(clock.readTemperature());
a[0]=12;
a[1]=10;
a[2]=times/10;
a[3]=times%10;
digitalWrite(13,HIGH);
}
else if(sec>=55 && sec<=59){times = DateTime.second;digitalWrite(13,HIGH);
a[0]=11;
a[1]=10;
a[2]=times/10;
a[3]=times%10;}
else{times = DateTime.hour*100+DateTime.minute;
a[0]=times/1000;
a[1]=times/100%10;
a[2]=times/10%10;
a[3]=times%10%10;
if(analogRead(A0)>900){digitalWrite(13,HIGH);}else{digitalWrite(13,LOW);}// dp
}
}
///////// коррекция минут /////////////////
if(menu==1){
if(analogRead(A2)>900){minute++;delay(300);if(minute>59){minute=0;}korr_time();}
if(analogRead(A3)>900){minute--;delay(300);if(minute<0){minute=0;}korr_time();}
times=minute;
a[0]=1;
a[1]=10;
a[2]=times/10;
a[3]=times%10;}
///////// коррекция часов /////////////////
if(menu==2){
if(analogRead(A2)>900){hour++;delay(300);if(hour>23){hour=0;}korr_time();}
if(analogRead(A3)>900){hour--;delay(300);if(hour<0){hour=0;}korr_time();}
times=hour;
a[0]=2;
a[1]=10;
a[2]=times/10;
a[3]=times%10;}
///////// коррекция дата /////////////////
if(menu==3){
if(analogRead(A2)>900){day++;delay(300);if(day>31){day=0;}korr_time();}
if(analogRead(A3)>900){day--;delay(300);if(day<0){day=0;}korr_time();}
times=day;
a[0]=3;
a[1]=10;
a[2]=times/10;
a[3]=times%10;}
///////// коррекция месяц /////////////////
if(menu==4){
if(analogRead(A2)>900){month++;delay(300);if(month>12){month=0;}korr_time();}
if(analogRead(A3)>900){month--;delay(300);if(month<0){month=0;}korr_time();}
times=month;
a[0]=4;
a[1]=10;
a[2]=times/10;
a[3]=times%10;}
} // loop
void to_Timer(){
switch(i){
case 0: cl();segm=a[0]; an=0; anod(); segment();break;
case 1: cl();segm=a[1]; an=1; anod(); segment();break;
case 2: cl();segm=a[2]; an=2; anod(); segment();break;
case 3: cl();segm=a[3]; an=3; anod(); segment();break;
}i++;if(i>3){i=0;}}
void segment(){
switch(segm){
// A B C D E F G
case 0: ch(2,0);ch(3,0);ch(4,0);ch(5,0);ch(6,0);ch(7,0);ch(8,1);break;// 0
case 1: ch(2,1);ch(3,0);ch(4,0);ch(5,1);ch(6,1);ch(7,1);ch(8,1);break;// 1
case 2: ch(2,0);ch(3,0);ch(4,1);ch(5,0);ch(6,0);ch(7,1);ch(8,0);break;// 2
case 3: ch(2,0);ch(3,0);ch(4,0);ch(5,0);ch(6,1);ch(7,1);ch(8,0);break;// 3
case 4: ch(2,1);ch(3,0);ch(4,0);ch(5,1);ch(6,1);ch(7,0);ch(8,0);break;// 4
case 5: ch(2,0);ch(3,1);ch(4,0);ch(5,0);ch(6,1);ch(7,0);ch(8,0);break;// 5
case 6: ch(2,0);ch(3,1);ch(4,0);ch(5,0);ch(6,0);ch(7,0);ch(8,0);break;// 6
case 7: ch(2,0);ch(3,0);ch(4,0);ch(5,1);ch(6,1);ch(7,1);ch(8,1);break;// 7
case 8: ch(2,0);ch(3,0);ch(4,0);ch(5,0);ch(6,0);ch(7,0);ch(8,0);break;// 8
case 9: ch(2,0);ch(3,0);ch(4,0);ch(5,0);ch(6,1);ch(7,0);ch(8,0);break;// 9
case 10:ch(2,1);ch(3,1);ch(4,1);ch(5,1);ch(6,1);ch(7,1);ch(8,1);break;// ПУСТО
case 11:ch(2,0);ch(3,1);ch(4,0);ch(5,0);ch(6,1);ch(7,0);ch(8,0);break;// S
case 12:ch(2,1);ch(3,1);ch(4,1);ch(5,0);ch(6,0);ch(7,0);ch(8,0);break;// t
}}
void anod(){
switch(an){
case 0:ch(9,0);ch(10,1);ch(11,1);ch(12,1);break;
case 1:ch(9,1);ch(10,0);ch(11,1);ch(12,1);break;
case 2:ch(9,1);ch(10,1);ch(11,0);ch(12,1);break;
case 3:ch(9,1);ch(10,1);ch(11,1);ch(12,0);break;
}}
void cl(){
segm=10; an=0; segment(); anod(); an=1; segment(); anod(); an=2; segment(); anod(); an=3; segment();anod();
}
void ch(int pin, int logic){digitalWrite(pin,logic);}
void readTime(){
year = DateTime.year;
month = DateTime.month;
day = DateTime.day;
hour = DateTime.hour;
minute = DateTime.minute;
sec=DateTime.second;
}
void korr_time(){clock.setDateTime(year+2000, month, day, hour, minute, 59 );}