1

Тема: 4-х канальное реле времени (Arduino)

#include <Wire.h> 
#include <SPI.h>
#include <PCD8544.h>
#include <DS3231.h>
#include <EEPROM.h>
 DS3231 clock;RTCDateTime DateTime;
 PCD8544 lcd; // RST,CE,DC,DIN,CLK   VCC,BL,GND (3,4,5,6,7   +3.3V,200 OM +3.3V, GND)
 char * mes[12]{"/)&",",e&","Map$","A;p","Ma]","^_)`","^_{`","A&}","Ce)","Ok$","Ho*","#ek"};
 char * den[8]{"","!o)","B$p","Cp(","'$&","!*$","C@[","Bck"};
 int i,n,ok,menu,x,w,mem,d_mem,t,n_d,ind0,ind1,ind2,ind3;
 int m0_on[8],h0_on[8],m0_of[8],h0_of[8];
 int m1_on[8],h1_on[8],m1_of[8],h1_of[8];
 int m2_on[8],h2_on[8],m2_of[8],h2_of[8];
 int m3_on[8],h3_on[8],m3_of[8],h3_of[8];
 static const byte gradus[] = { B00000110,B00001001,B00001001,B00000110,B00000000 };// градус
 
 void setup(){
  Serial.begin(9600);Wire.begin();clock.begin();
  //  clock.setDateTime(__DATE__, __TIME__); // Устанавливаем время на часах, основываясь на времени компиляции скетча 
  lcd.begin(84, 48);lcd.setContrast(70);//  контрастность 0-127
  pinMode(A0,INPUT); // menu
  pinMode(A1,INPUT); // +
  pinMode(A2,INPUT); // -
  pinMode(A3,INPUT); // ok
  pinMode(9,OUTPUT);pinMode(10,OUTPUT);pinMode(11,OUTPUT);pinMode(12,OUTPUT);// выходы реле
  lcd.createChar(0, gradus);
  for(d_mem=0;d_mem<8;d_mem++){mem++;m0_on[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;h0_on[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;m0_of[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;h0_of[d_mem]=EEPROM.read(mem);} 
  for(d_mem=0;d_mem<8;d_mem++){mem++;m1_on[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;h1_on[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;m1_of[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;h1_of[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;m2_on[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;h2_on[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;m2_of[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;h2_of[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;m3_on[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;h3_on[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;m3_of[d_mem]=EEPROM.read(mem);}
  for(d_mem=0;d_mem<8;d_mem++){mem++;h3_of[d_mem]=EEPROM.read(mem);}
  if(EEPROM.read(100)==255){for(mem=0;mem<120;mem++){EEPROM.update(mem,0);}}// если ячейка памяти №100 равна 255 установить все значения реле времени на 0 (код работает только при первом включении)
  mem=0;
 }
 
 void loop(){
 ///////////////////////////// дата время ///////////////////////////////
  DateTime=clock.getDateTime();// опрос времени
  lcd.setCursor(0,0);
  lcd.print(clock.dateFormat("H:i:s ", DateTime));// выводим часы, минуты и секунды
  lcd.print(" ");for(i=0;i<8;i++){if(DateTime.dayOfWeek==i){lcd.print(den[i]);}}lcd.print(" ");// день недели
  lcd.setCursor(0,1);lcd.print("  ");lcd.print(DateTime.day);lcd.print(" "); // дата
  for(i=0;i<13;i++){if(DateTime.month==i){lcd.print(mes[i-1]);}}lcd.print(" "); // месяц
  lcd.print(DateTime.year);lcd.print(" "); // год
  lcd.setCursor(0,2);lcd.print("--------------");
 ////////////////////////////////////////////////////////////////////////
 
 if(analogRead(A0)>900){menu++;w=1;mem=0;ok=0;n=0;lcd.clear();if(menu>4||menu<0){menu=0;}} // перебор реле 1...4 
 x++; if(x>1){x=0;} // мигание регулировки параметров
 /////////////////////////////////////// Реле 1 /////////////////////////
 if(menu==1){ 
   if(analogRead(A3)>900){ok++;}if(ok>4){ok=0;} ///// кнопка ОК - перебор параметров реле
   if(analogRead(A1)>900&&ok==0){n++;}if(analogRead(A2)>900&&ok==0){n--;}if(n>7||n<1){n=1;} //// день недели 1...7
   lcd.setCursor(0,3);lcd.print(" Pe{e 1  ");if(x==0&&ok==0){lcd.print("   ");}else{lcd.print(den[n]);}lcd.print(" "); // вывод дня недели
 
   if(analogRead(A1)>900&&ok==1){m0_on[n]++;}if(analogRead(A2)>900&&ok==1){m0_on[n]--;}if(m0_on[n]>59||m0_on[n]<0){m0_on[n]=0;}
   if(analogRead(A1)>900&&ok==2){h0_on[n]++;}if(analogRead(A2)>900&&ok==2){h0_on[n]--;}if(h0_on[n]>23||h0_on[n]<0){h0_on[n]=0;}
   if(analogRead(A1)>900&&ok==3){m0_of[n]++;}if(analogRead(A2)>900&&ok==3){m0_of[n]--;}if(m0_of[n]>59||m0_of[n]<0){m0_of[n]=0;}
   if(analogRead(A1)>900&&ok==4){h0_of[n]++;}if(analogRead(A2)>900&&ok==4){h0_of[n]--;}if(h0_of[n]>23||h0_of[n]<0){h0_of[n]=0;}
 
   lcd.setCursor(0,4);lcd.print(" ");
   if(x==0&&ok==2){lcd.print("  ");}else{if(h0_on[n]<10){lcd.print("0");}lcd.print(h0_on[n]);}lcd.print(":");
   if(x==0&&ok==1){lcd.print("  ");}else{if(m0_on[n]<10){lcd.print("0");}lcd.print(m0_on[n]);}lcd.print("  "); 
   if(x==0&&ok==4){lcd.print("  ");}else{if(h0_of[n]<10){lcd.print("0");}lcd.print(h0_of[n]);}lcd.print(":");
   if(x==0&&ok==3){lcd.print("  ");}else{if(m0_of[n]<10){lcd.print("0");}lcd.print(m0_of[n]);}
   lcd.setCursor(0,5);str6();}
 ///////////////////////////////////////////////////////////////////////
 /////////////////////////////////////// Реле 2 /////////////////////////
 if(menu==2){ 
   if(analogRead(A3)>900){ok++;}if(ok>4){ok=0;} ///// кнопка ОК - перебор параметров реле
   if(analogRead(A1)>900&&ok==0){n++;}if(analogRead(A2)>900&&ok==0){n--;}if(n>7||n<1){n=1;} //// день недели 1...7
   lcd.setCursor(0,3);lcd.print(" Pe{e 2  ");if(x==0&&ok==0){lcd.print("   ");}else{lcd.print(den[n]);}lcd.print(" "); // вывод дня недели
 
   if(analogRead(A1)>900&&ok==1){m1_on[n]++;}if(analogRead(A2)>900&&ok==1){m1_on[n]--;}if(m1_on[n]>59||m1_on[n]<0){m1_on[n]=0;}
   if(analogRead(A1)>900&&ok==2){h1_on[n]++;}if(analogRead(A2)>900&&ok==2){h1_on[n]--;}if(h1_on[n]>23||h1_on[n]<0){h1_on[n]=0;}
   if(analogRead(A1)>900&&ok==3){m1_of[n]++;}if(analogRead(A2)>900&&ok==3){m1_of[n]--;}if(m1_of[n]>59||m1_of[n]<0){m1_of[n]=0;}
   if(analogRead(A1)>900&&ok==4){h1_of[n]++;}if(analogRead(A2)>900&&ok==4){h1_of[n]--;}if(h1_of[n]>23||h1_of[n]<0){h1_of[n]=0;}
 
   lcd.setCursor(0,4);lcd.print(" ");
   if(x==0&&ok==2){lcd.print("  ");}else{if(h1_on[n]<10){lcd.print("0");}lcd.print(h1_on[n]);}lcd.print(":");
   if(x==0&&ok==1){lcd.print("  ");}else{if(m1_on[n]<10){lcd.print("0");}lcd.print(m1_on[n]);}lcd.print("  "); 
   if(x==0&&ok==4){lcd.print("  ");}else{if(h1_of[n]<10){lcd.print("0");}lcd.print(h1_of[n]);}lcd.print(":");
   if(x==0&&ok==3){lcd.print("  ");}else{if(m1_of[n]<10){lcd.print("0");}lcd.print(m1_of[n]);}
   lcd.setCursor(0,5);str6();}
 ///////////////////////////////////////////////////////////////////////
 /////////////////////////////////////// Реле 3 /////////////////////////
 if(menu==3){ 
   if(analogRead(A3)>900){ok++;}if(ok>4){ok=0;} ///// кнопка ОК - перебор параметров реле
   if(analogRead(A1)>900&&ok==0){n++;}if(analogRead(A2)>900&&ok==0){n--;}if(n>7||n<1){n=1;} //// день недели 1...7
   lcd.setCursor(0,3);lcd.print(" Pe{e 3  ");if(x==0&&ok==0){lcd.print("   ");}else{lcd.print(den[n]);}lcd.print(" "); // вывод дня недели
 
   if(analogRead(A1)>900&&ok==1){m2_on[n]++;}if(analogRead(A2)>900&&ok==1){m2_on[n]--;}if(m2_on[n]>59||m2_on[n]<0){m2_on[n]=0;}
   if(analogRead(A1)>900&&ok==2){h2_on[n]++;}if(analogRead(A2)>900&&ok==2){h2_on[n]--;}if(h2_on[n]>23||h2_on[n]<0){h2_on[n]=0;}
   if(analogRead(A1)>900&&ok==3){m2_of[n]++;}if(analogRead(A2)>900&&ok==3){m2_of[n]--;}if(m2_of[n]>59||m2_of[n]<0){m2_of[n]=0;}
   if(analogRead(A1)>900&&ok==4){h2_of[n]++;}if(analogRead(A2)>900&&ok==4){h2_of[n]--;}if(h2_of[n]>23||h2_of[n]<0){h2_of[n]=0;}
 
   lcd.setCursor(0,4);lcd.print(" ");
   if(x==0&&ok==2){lcd.print("  ");}else{if(h2_on[n]<10){lcd.print("0");}lcd.print(h2_on[n]);}lcd.print(":");
   if(x==0&&ok==1){lcd.print("  ");}else{if(m2_on[n]<10){lcd.print("0");}lcd.print(m2_on[n]);}lcd.print("  "); 
   if(x==0&&ok==4){lcd.print("  ");}else{if(h2_of[n]<10){lcd.print("0");}lcd.print(h2_of[n]);}lcd.print(":");
   if(x==0&&ok==3){lcd.print("  ");}else{if(m2_of[n]<10){lcd.print("0");}lcd.print(m2_of[n]);}
   lcd.setCursor(0,5);str6();}
 ///////////////////////////////////////////////////////////////////////
 /////////////////////////////////////// Реле 4 /////////////////////////
 if(menu==4){ 
   if(analogRead(A3)>900){ok++;}if(ok>4){ok=0;} ///// кнопка ОК - перебор параметров реле
   if(analogRead(A1)>900&&ok==0){n++;}if(analogRead(A2)>900&&ok==0){n--;}if(n>7||n<1){n=1;} //// день недели 1...7
   lcd.setCursor(0,3);lcd.print(" Pe{e 4  ");if(x==0&&ok==0){lcd.print("   ");}else{lcd.print(den[n]);}lcd.print(" "); // вывод дня недели
 
   if(analogRead(A1)>900&&ok==1){m3_on[n]++;}if(analogRead(A2)>900&&ok==1){m3_on[n]--;}if(m3_on[n]>59||m3_on[n]<0){m3_on[n]=0;}
   if(analogRead(A1)>900&&ok==2){h3_on[n]++;}if(analogRead(A2)>900&&ok==2){h3_on[n]--;}if(h3_on[n]>23||h3_on[n]<0){h3_on[n]=0;}
   if(analogRead(A1)>900&&ok==3){m3_of[n]++;}if(analogRead(A2)>900&&ok==3){m3_of[n]--;}if(m3_of[n]>59||m3_of[n]<0){m3_of[n]=0;}
   if(analogRead(A1)>900&&ok==4){h3_of[n]++;}if(analogRead(A2)>900&&ok==4){h3_of[n]--;}if(h3_of[n]>23||h3_of[n]<0){h3_of[n]=0;}
 
   lcd.setCursor(0,4);lcd.print(" ");
   if(x==0&&ok==2){lcd.print("  ");}else{if(h3_on[n]<10){lcd.print("0");}lcd.print(h3_on[n]);}lcd.print(":");
   if(x==0&&ok==1){lcd.print("  ");}else{if(m3_on[n]<10){lcd.print("0");}lcd.print(m3_on[n]);}lcd.print("  "); 
   if(x==0&&ok==4){lcd.print("  ");}else{if(h3_of[n]<10){lcd.print("0");}lcd.print(h3_of[n]);}lcd.print(":");
   if(x==0&&ok==3){lcd.print("  ");}else{if(m3_of[n]<10){lcd.print("0");}lcd.print(m3_of[n]);}
   lcd.setCursor(0,5);str6();}
 ///////////////////////////////////////////////////////////////////////
 
 //////////////////// управление реле //////////////////////////////////
   t=DateTime.hour*100+DateTime.minute; // текущее время, например: 1026 = 10 часов 26 минут
   n_d=DateTime.dayOfWeek; // текущий день недели 1...7 вос...пон
  if(t>=(h0_on[n_d]*100+m0_on[n_d])&&t<(h0_of[n_d]*100+m0_of[n_d])&&(h0_on[n_d]*100+m0_on[n_d])!=(h0_of[n_d]*100+m0_of[n_d])){digitalWrite(9,HIGH);ind0=1;} else{digitalWrite(9,LOW);ind0=0;}
  if(t>=(h1_on[n_d]*100+m1_on[n_d])&&t<(h1_of[n_d]*100+m1_of[n_d])&&(h1_on[n_d]*100+m1_on[n_d])!=(h1_of[n_d]*100+m1_of[n_d])){digitalWrite(10,HIGH);ind1=1;}else{digitalWrite(10,LOW);ind1=0;}
  if(t>=(h2_on[n_d]*100+m2_on[n_d])&&t<(h2_of[n_d]*100+m2_of[n_d])&&(h2_on[n_d]*100+m2_on[n_d])!=(h2_of[n_d]*100+m2_of[n_d])){digitalWrite(11,HIGH);ind2=1;}else{digitalWrite(11,LOW);ind2=0;}
  if(t>=(h3_on[n_d]*100+m3_on[n_d])&&t<(h3_of[n_d]*100+m3_of[n_d])&&(h3_on[n_d]*100+m3_on[n_d])!=(h3_of[n_d]*100+m3_of[n_d])){digitalWrite(12,HIGH);ind3=1;}else{digitalWrite(12,LOW);ind3=0;}
 //////////////////////////////////////////////////////////////////////
 
  delay(100);
 
  if(menu==0){ 
    lcd.setCursor(0,3);if(ind0==0){lcd.print(" P1 OFF");}else{lcd.print(" P1 ON ");}if(ind1==0){lcd.print(" P2 OFF ");}else{lcd.print(" P2 ON  ");}
    lcd.setCursor(0,4);if(ind2==0){lcd.print(" P3 OFF");}else{lcd.print(" P3 ON ");}if(ind3==0){lcd.print(" P4 OFF ");}else{lcd.print(" P4 ON  ");}
    lcd.setCursor(0,5);lcd.print(" Te~;ep. ");lcd.print(clock.readTemperature(),1);lcd.write(0);lcd.print("C");
  }
 
///////////////////////////////// EEPROM //////////////////////////////  
  if(w==1){
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,m0_on[d_mem]);}
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,h0_on[d_mem]);}  
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,m0_of[d_mem]);}
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,h0_of[d_mem]);}   
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,m1_on[d_mem]);}
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,h1_on[d_mem]);}  
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,m1_of[d_mem]);}
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,h1_of[d_mem]);}   
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,m2_on[d_mem]);}
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,h2_on[d_mem]);}  
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,m2_of[d_mem]);}
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,h2_of[d_mem]);}   
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,m3_on[d_mem]);}
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,h3_on[d_mem]);}  
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,m3_of[d_mem]);}
    for(d_mem=0;d_mem<8;d_mem++){mem++;EEPROM.update(mem,h3_of[d_mem]);}
    w=0;}
 
    if(DateTime.hour==0&&DateTime.minute==0&&DateTime.second==0){lcd.clear();}// сбор экрана каждые сутки в 0ч 0м 0с
 
}///// loop
 
 void str6(){lcd.print(" Bk{    B?k{");}