Re: Ретро часы на ИВ-6
парадокс, но при питании ds3231 от 3.3 в так же перестает мигать разделительная точка.
#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,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();// опрос времени
year = DateTime.year;
month = DateTime.month;
day = DateTime.day;
hour = DateTime.hour;
minute = DateTime.minute;
}
void loop(){ DateTime=clock.getDateTime();// опрос времени
if(analogRead(A1)>900){menu++;delay(200);if(menu>5){menu=0;w=1;}}
/////////// ВРЕМЯ /////////////
if(menu==0&&w2==0){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
}
w2=0;
////// вывод секунд - нажать и удерживать кнопку +/sec /////////////////////////////////////
if(analogRead(A2)>900&&menu==0){w2=1;times = DateTime.second;digitalWrite(13,HIGH);
a[0]=11;
a[1]=10;
a[2]=times/10;
a[3]=times%10;}
////// вывод температуры - нажать и удерживать кнопку -/temp /////////////////////////////////////
if(analogRead(A3)>900&&menu==0){w2=1;times = int(clock.readTemperature());digitalWrite(13,HIGH);
a[0]=12;
a[1]=10;
a[2]=times/10;
a[3]=times%10;}
///////// коррекция минут /////////////////
if(menu==1){
if(analogRead(A2)>900){minute++;delay(300);if(minute>59){minute=0;}}
if(analogRead(A3)>900){minute--;delay(300);if(minute<0){minute=0;}}
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;}}
if(analogRead(A3)>900){hour--;delay(300);if(hour<0){hour=0;}}
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;}}
if(analogRead(A3)>900){day--;delay(300);if(day<0){day=0;}}
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;}}
if(analogRead(A3)>900){month--;delay(300);if(month<0){month=0;}}
times=month;
a[0]=4;
a[1]=10;
a[2]=times/10;
a[3]=times%10;}
///////// коррекция год /////////////////
if(menu==5){
if(analogRead(A2)>900){year++;delay(300);if(year>99){year=19;}}
if(analogRead(A3)>900){year--;delay(300);if(year<19){year=19;}}
times=year;
a[0]=5;
a[1]=10;
a[2]=times/10;
a[3]=times%10;}
if(w==1){clock.setDateTime(year+2000, month, day, hour, minute, 59 );// год (19),месяц(11),день(15),час(23),минуты(32),секунды
w=0;}
} // 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);}