Advertisement
TolentinoCotesta

nTimer Ds1307

Feb 12th, 2019
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <RTClib.h>
  3. RTC_DS1307 RTC;
  4.  
  5. #include <hd44780.h>
  6. #include <hd44780ioClass/hd44780_I2Cexp.h>
  7. hd44780_I2Cexp lcd(0x3F, 16, 2);
  8. #define HD44780_LCDOBJECT
  9.  
  10. char daysOfTheWeek[7][10] = {"Do", "Lu", "Ma", "Me", "Gio", "Ve", "Sa"};
  11. char bufferRX[50];
  12. DateTime now;
  13. unsigned long secondsOfDay;
  14.  
  15. // Usiamo una struttura per definire le caratteristiche generali dei timer
  16. struct myTimer {
  17.   String timerName;  
  18.   unsigned long startTime = 0;  // Orario di start del temporizzatore (default 0)
  19.   unsigned long stopTime =0;    // Orario di stop del temporizzatore (default 0)
  20.   boolean timerState = LOW;    // Variabile che andremo a settare quando vogliamo attivare il timer (default LOW)
  21.   boolean level_on = LOW;      // Se i relè si attivano con GND metter LOW, altrimenti HIGH (default LOW)
  22.   byte outPin;                  // Piedino di uscita controllato dal timer
  23. };
  24.  
  25.  
  26. // Adesso andiamo a dichiarare i nostri 3 timer usando la struttura definita in precedenza
  27. myTimer myTimer1, myTimer2, myTimer3;
  28.  
  29. void setupTimers()
  30. {
  31.   myTimer1.timerName = "Timer Luci";
  32.   myTimer1.outPin = 2;  
  33.   digitalWrite(myTimer1.outPin, HIGH);  // Serve per evitare che il relè venga eccitato per pochi ms durante il boot
  34.   pinMode(myTimer1.outPin, OUTPUT);
  35.   myTimer2.timerName = "Timer Pompa";
  36.   myTimer2.outPin = 3;  
  37.   digitalWrite(myTimer2.outPin, HIGH);
  38.   pinMode(myTimer2.outPin, OUTPUT);
  39.   myTimer3.timerName = "Timer Atomizzatore";
  40.   myTimer3.outPin = 4;
  41.   digitalWrite(myTimer3.outPin, HIGH);
  42.   pinMode(myTimer3.outPin, OUTPUT);  
  43. }
  44.  
  45. void setup()
  46. {
  47.   setupTimers();
  48.   Serial.begin(9600);
  49.   Serial.println("Per aggiornare data e ora inviare il seguente comando: ");
  50.   Serial.println("\"setDateTime anno, mese, giorno, ora, minuti, secondi\"");
  51.   Serial.println("Esempio: setDateTime, 2018, 9, 8, 17, 30, 00");
  52.   Serial.println("Per impostare uno dei timer giornaliero inviare i seguenti comando: ");
  53.   Serial.println("\"setTimer1Start, ora, minuti, secondi\"");
  54.   Serial.println("\"setTimer1Stop, ora, minuti, secondi\"");
  55.   Serial.println("Esempio: setTimer1Start, 16, 30, 00");
  56.   Serial.println("Esempio: setTimer1Stop, 19, 30, 00");
  57.   lcd.init();
  58.   lcd.backlight();
  59.   now = RTC.now();
  60. }
  61. void loop()
  62. {
  63.   //Aggiorniamo una volta al secondo
  64.   static unsigned long updateTime;
  65.   if (millis() - updateTime > 1000) {
  66.     updateTime = millis();
  67.     now = RTC.now();    
  68.     lcd.clear();
  69.     lcd.setCursor(0, 0);
  70.     LCDPrintNum(now.hour());
  71.     lcd.print(":");
  72.     LCDPrintNum(now.minute());
  73.     lcd.print(":");
  74.     LCDPrintNum(now.second());
  75.     lcd.print(" ");
  76.     lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
  77.     lcd.setCursor(0, 1);
  78.     LCDPrintNum(now.day());
  79.     lcd.print("/");
  80.     LCDPrintNum(now.month());
  81.     lcd.print("/");
  82.     lcd.print(now.year(), DEC);
  83.    
  84.     Serial.print(now.hour(), DEC);
  85.     Serial.print(':');
  86.     Serial.print(now.minute(), DEC);
  87.     Serial.print(':');
  88.     Serial.print(now.second(), DEC);
  89.     Serial.println();
  90.    
  91.   }
  92.   // Vediamo se arriva qualcosa dalla seriale
  93.   readSerial();
  94.  
  95.   secondsOfDay = now.hour() * 3600 + now.minute() * 60 + now.second();
  96.   updateTimer(&myTimer1);
  97.   updateTimer(&myTimer2);
  98.   updateTimer(&myTimer3);
  99.  
  100. }
  101.  
  102.  
  103. // Funzione che aggiorna lo stato del timer
  104. // Gli passiamo come parametro il puntatore al timer che vogliamo aggiornare
  105. void updateTimer(myTimer * myTimerN){
  106.  
  107.   // Controlliamo se è tempo di attivare qualche timer
  108.   if (secondsOfDay >= myTimerN->startTime && secondsOfDay < myTimerN->stopTime) {
  109.     // Eseguiamo queste istruzioni solo alla transizione STOP/START
  110.     if (myTimerN->timerState == LOW){  
  111.       Serial.print(myTimerN->timerName);
  112.       Serial.println(" attivato");
  113.       myTimerN->timerState = HIGH;
  114.       digitalWrite(myTimerN->outPin, myTimerN->level_on);
  115.     }
  116.   }
  117.   else {
  118.     // Eseguiamo queste istruzioni solo alla transizione START/STOP
  119.     if (myTimerN->timerState == HIGH){
  120.       Serial.print(myTimerN->timerName);
  121.       Serial.println(" disattivato");
  122.       myTimerN->timerState = LOW;
  123.       digitalWrite(myTimerN->outPin, ! myTimerN->level_on);   // Attenzione alla negazione "!"  
  124.     }
  125.   }
  126.  
  127.  
  128. }
  129.  
  130.  
  131.  
  132. // Funzione per stampare ad esempio 02 al posto di 2
  133. void LCDPrintNum(int num) {
  134.   if (num < 10)
  135.     lcd.print("0");
  136.   lcd.print(num);
  137. }
  138.  
  139. int tdArray[6];
  140. void stringToArray(){
  141.   char * pch = strtok (bufferRX, " ,");   // Split string into tokens
  142.     unsigned int i = 0;
  143.     while (pch != NULL) {
  144.       if (i > 0)                            // Il primo valore trovato "setDateTime" non lo salvo nell'array
  145.         tdArray[i - 1] = atoi(pch);         // Converto la stringa in un intero con l'istruzione atoi()
  146.       pch = strtok (NULL, " ,");            // troviamo la substring successiva
  147.       i++;
  148.     }  
  149. }
  150.  
  151. void readSerial() {  
  152.   while (Serial.available() > 0) {
  153.     // Leggiamo tutto quello che arriva dalla seriale e salviamolo nell'array di char bufferRX[]
  154.     Serial.readBytesUntil('\n', bufferRX, sizeof(bufferRX) - 1);
  155.     /*
  156.      *  Se c'è il comando giusto eseguiamo l'aggiornamento dell'orario dell'RTC
  157.      *  Verrà usata la funzione C++ strtok che consente di splittare una stringa con uno specifico delimitatore,
  158.      *  nel nostro caso la virgola ','. I valori verranno quindi salvati nell'array di int tdArray[] per usarli
  159.      *  facilmente dove necessario per impostare il nuovo orario.
  160.     */
  161.     if (strstr(bufferRX, "setDateTime,")) {            
  162.       // Per creare una variabile DateTime è necessario questo formato: (anno, mese, giorno, ora, minuti, secondi)
  163.       stringToArray();
  164.       DateTime newTime = DateTime(tdArray[0], tdArray[1], tdArray[2], tdArray[3], tdArray[4], tdArray[5]);
  165.       RTC.adjust(newTime);
  166.       Serial.println("Impostato nuovo orario.");
  167.     }
  168.    
  169.     if (strstr(bufferRX, "setTimer1Start,")) {  
  170.       stringToArray();          
  171.       myTimer1.startTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];      
  172.       Serial.println("Impostato Start Timer 1");
  173.     }
  174.    
  175.     if (strstr(bufferRX, "setTimer1Stop,")) {  
  176.       stringToArray();          
  177.       myTimer1.stopTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];      
  178.       Serial.println("Impostato Stop Timer 1");
  179.     }
  180.  
  181.     if (strstr(bufferRX, "setTimer2Start,")) {  
  182.       stringToArray();          
  183.       myTimer2.startTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];      
  184.       Serial.println("Impostato Start Timer 2");
  185.     }
  186.    
  187.     if (strstr(bufferRX, "setTimer2Stop,")) {  
  188.       stringToArray();          
  189.       myTimer2.stopTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];      
  190.       Serial.println("Impostato Stop Timer 2");
  191.     }
  192.  
  193.     if (strstr(bufferRX, "setTimer3Start,")) {  
  194.       stringToArray();          
  195.       myTimer3.startTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];      
  196.       Serial.println("Impostato Start Timer 3");
  197.     }
  198.    
  199.     if (strstr(bufferRX, "setTimer3Stop,")) {  
  200.       stringToArray();          
  201.       myTimer3.stopTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];      
  202.       Serial.println("Impostato Stop Timer 3");
  203.     }
  204.   }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement