#include <MsTimer2.h> // http://rcl-radio.ru/wp-content/uploads … Timer2.zip
#include <Wire.h>
#include <EEPROM.h>
#include <DS3231.h> // http://rcl-radio.ru/wp-content/uploads … DS3231.zip
DS3231 clock;RTCDateTime DateTime;
int an, segm,times,i,pd,pd_p;
byte a[4];
int brig = 1;
unsigned long tim;
byte w;
void setup(){ Wire.begin();clock.begin();
MsTimer2::set(2, to_Timer);MsTimer2::start();
//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(13,OUTPUT); // D13 === PD
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(0,INPUT_PULLUP); // pin button +brig
pinMode(1,INPUT_PULLUP); // pin button -brig
clock.setOutput(DS3231_1HZ);
pinMode(A0,INPUT); // SQW DS3231
brig = EEPROM.read(0);
if(brig>100){brig=1;}
}
void loop(){
if(digitalRead(0)==LOW){brig++;tim=millis();w=1;delay(50);if(brig>100){brig=100;}}
if(digitalRead(1)==LOW){brig--;tim=millis();w=1;delay(50);if(brig<=0){brig=1;}}
DateTime=clock.getDateTime();// опрос времени
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){pd_p=1;}else{pd_p=0;}
if(millis()-tim>10000 && w==1){EEPROM.update(0,brig);w=0;}
}// loop
void to_Timer(){
switch(i){
case 0: cl(); segm=a[0]; pd=0; an=0; delay(brig*20);anod(); segment();break;
case 1: cl(); segm=a[1]; pd=pd_p; an=1; delay(brig*20);anod(); segment();break;
case 2: cl(); segm=a[2]; pd=0; an=2; delay(brig*20);anod(); segment();break;
case 3: cl(); segm=a[3]; pd=0; an=3; delay(brig*20);anod(); segment();break;}i++;
if(i>3){i=0;}}
void segment(){
switch(segm){
// A B C D E F G PD
case 0: ch(2,1);ch(3,1);ch(4,1);ch(5,1);ch(6,1);ch(7,1);ch(8,0);ch(13,pd);break;// 0
case 1: ch(2,0);ch(3,1);ch(4,1);ch(5,0);ch(6,0);ch(7,0);ch(8,0);ch(13,pd);break;// 1
case 2: ch(2,1);ch(3,1);ch(4,0);ch(5,1);ch(6,1);ch(7,0);ch(8,1);ch(13,pd);break;// 2
case 3: ch(2,1);ch(3,1);ch(4,1);ch(5,1);ch(6,0);ch(7,0);ch(8,1);ch(13,pd);break;// 3
case 4: ch(2,0);ch(3,1);ch(4,1);ch(5,0);ch(6,0);ch(7,1);ch(8,1);ch(13,pd);break;// 4
case 5: ch(2,1);ch(3,0);ch(4,1);ch(5,1);ch(6,0);ch(7,1);ch(8,1);ch(13,pd);break;// 5
case 6: ch(2,1);ch(3,0);ch(4,1);ch(5,1);ch(6,1);ch(7,1);ch(8,1);ch(13,pd);break;// 6
case 7: ch(2,1);ch(3,1);ch(4,1);ch(5,0);ch(6,0);ch(7,0);ch(8,0);ch(13,pd);break;// 7
case 8: ch(2,1);ch(3,1);ch(4,1);ch(5,1);ch(6,1);ch(7,1);ch(8,1);ch(13,pd);break;// 8
case 9: ch(2,1);ch(3,1);ch(4,1);ch(5,1);ch(6,0);ch(7,1);ch(8,1);ch(13,pd);break;// 9
case 10: ch(2,0);ch(3,0);ch(4,0);ch(5,0);ch(6,0);ch(7,0);ch(8,0);ch(13,pd);break;// пусто
}}
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; pd=0; 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);}