Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <RTClib.h>
- RTC_DS1307 RTC;
- #include <hd44780.h>
- #include <hd44780ioClass/hd44780_I2Cexp.h>
- hd44780_I2Cexp lcd(0x3F, 16, 2);
- #define HD44780_LCDOBJECT
- char daysOfTheWeek[7][10] = {"Do", "Lu", "Ma", "Me", "Gio", "Ve", "Sa"};
- char bufferRX[50];
- DateTime now;
- unsigned long secondsOfDay;
- // Usiamo una struttura per definire le caratteristiche generali dei timer
- struct myTimer {
- String timerName;
- unsigned long startTime = 0; // Orario di start del temporizzatore (default 0)
- unsigned long stopTime =0; // Orario di stop del temporizzatore (default 0)
- boolean timerState = LOW; // Variabile che andremo a settare quando vogliamo attivare il timer (default LOW)
- boolean level_on = LOW; // Se i relè si attivano con GND metter LOW, altrimenti HIGH (default LOW)
- byte outPin; // Piedino di uscita controllato dal timer
- };
- // Adesso andiamo a dichiarare i nostri 3 timer usando la struttura definita in precedenza
- myTimer myTimer1, myTimer2, myTimer3;
- void setupTimers()
- {
- myTimer1.timerName = "Timer Luci";
- myTimer1.outPin = 2;
- digitalWrite(myTimer1.outPin, HIGH); // Serve per evitare che il relè venga eccitato per pochi ms durante il boot
- pinMode(myTimer1.outPin, OUTPUT);
- myTimer2.timerName = "Timer Pompa";
- myTimer2.outPin = 3;
- digitalWrite(myTimer2.outPin, HIGH);
- pinMode(myTimer2.outPin, OUTPUT);
- myTimer3.timerName = "Timer Atomizzatore";
- myTimer3.outPin = 4;
- digitalWrite(myTimer3.outPin, HIGH);
- pinMode(myTimer3.outPin, OUTPUT);
- }
- void setup()
- {
- setupTimers();
- Serial.begin(9600);
- Serial.println("Per aggiornare data e ora inviare il seguente comando: ");
- Serial.println("\"setDateTime anno, mese, giorno, ora, minuti, secondi\"");
- Serial.println("Esempio: setDateTime, 2018, 9, 8, 17, 30, 00");
- Serial.println("Per impostare uno dei timer giornaliero inviare i seguenti comando: ");
- Serial.println("\"setTimer1Start, ora, minuti, secondi\"");
- Serial.println("\"setTimer1Stop, ora, minuti, secondi\"");
- Serial.println("Esempio: setTimer1Start, 16, 30, 00");
- Serial.println("Esempio: setTimer1Stop, 19, 30, 00");
- lcd.init();
- lcd.backlight();
- now = RTC.now();
- }
- void loop()
- {
- //Aggiorniamo una volta al secondo
- static unsigned long updateTime;
- if (millis() - updateTime > 1000) {
- updateTime = millis();
- now = RTC.now();
- lcd.clear();
- lcd.setCursor(0, 0);
- LCDPrintNum(now.hour());
- lcd.print(":");
- LCDPrintNum(now.minute());
- lcd.print(":");
- LCDPrintNum(now.second());
- lcd.print(" ");
- lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
- lcd.setCursor(0, 1);
- LCDPrintNum(now.day());
- lcd.print("/");
- LCDPrintNum(now.month());
- lcd.print("/");
- lcd.print(now.year(), DEC);
- Serial.print(now.hour(), DEC);
- Serial.print(':');
- Serial.print(now.minute(), DEC);
- Serial.print(':');
- Serial.print(now.second(), DEC);
- Serial.println();
- }
- // Vediamo se arriva qualcosa dalla seriale
- readSerial();
- secondsOfDay = now.hour() * 3600 + now.minute() * 60 + now.second();
- updateTimer(&myTimer1);
- updateTimer(&myTimer2);
- updateTimer(&myTimer3);
- }
- // Funzione che aggiorna lo stato del timer
- // Gli passiamo come parametro il puntatore al timer che vogliamo aggiornare
- void updateTimer(myTimer * myTimerN){
- // Controlliamo se è tempo di attivare qualche timer
- if (secondsOfDay >= myTimerN->startTime && secondsOfDay < myTimerN->stopTime) {
- // Eseguiamo queste istruzioni solo alla transizione STOP/START
- if (myTimerN->timerState == LOW){
- Serial.print(myTimerN->timerName);
- Serial.println(" attivato");
- myTimerN->timerState = HIGH;
- digitalWrite(myTimerN->outPin, myTimerN->level_on);
- }
- }
- else {
- // Eseguiamo queste istruzioni solo alla transizione START/STOP
- if (myTimerN->timerState == HIGH){
- Serial.print(myTimerN->timerName);
- Serial.println(" disattivato");
- myTimerN->timerState = LOW;
- digitalWrite(myTimerN->outPin, ! myTimerN->level_on); // Attenzione alla negazione "!"
- }
- }
- }
- // Funzione per stampare ad esempio 02 al posto di 2
- void LCDPrintNum(int num) {
- if (num < 10)
- lcd.print("0");
- lcd.print(num);
- }
- int tdArray[6];
- void stringToArray(){
- char * pch = strtok (bufferRX, " ,"); // Split string into tokens
- unsigned int i = 0;
- while (pch != NULL) {
- if (i > 0) // Il primo valore trovato "setDateTime" non lo salvo nell'array
- tdArray[i - 1] = atoi(pch); // Converto la stringa in un intero con l'istruzione atoi()
- pch = strtok (NULL, " ,"); // troviamo la substring successiva
- i++;
- }
- }
- void readSerial() {
- while (Serial.available() > 0) {
- // Leggiamo tutto quello che arriva dalla seriale e salviamolo nell'array di char bufferRX[]
- Serial.readBytesUntil('\n', bufferRX, sizeof(bufferRX) - 1);
- /*
- * Se c'è il comando giusto eseguiamo l'aggiornamento dell'orario dell'RTC
- * Verrà usata la funzione C++ strtok che consente di splittare una stringa con uno specifico delimitatore,
- * nel nostro caso la virgola ','. I valori verranno quindi salvati nell'array di int tdArray[] per usarli
- * facilmente dove necessario per impostare il nuovo orario.
- */
- if (strstr(bufferRX, "setDateTime,")) {
- // Per creare una variabile DateTime è necessario questo formato: (anno, mese, giorno, ora, minuti, secondi)
- stringToArray();
- DateTime newTime = DateTime(tdArray[0], tdArray[1], tdArray[2], tdArray[3], tdArray[4], tdArray[5]);
- RTC.adjust(newTime);
- Serial.println("Impostato nuovo orario.");
- }
- if (strstr(bufferRX, "setTimer1Start,")) {
- stringToArray();
- myTimer1.startTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];
- Serial.println("Impostato Start Timer 1");
- }
- if (strstr(bufferRX, "setTimer1Stop,")) {
- stringToArray();
- myTimer1.stopTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];
- Serial.println("Impostato Stop Timer 1");
- }
- if (strstr(bufferRX, "setTimer2Start,")) {
- stringToArray();
- myTimer2.startTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];
- Serial.println("Impostato Start Timer 2");
- }
- if (strstr(bufferRX, "setTimer2Stop,")) {
- stringToArray();
- myTimer2.stopTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];
- Serial.println("Impostato Stop Timer 2");
- }
- if (strstr(bufferRX, "setTimer3Start,")) {
- stringToArray();
- myTimer3.startTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];
- Serial.println("Impostato Start Timer 3");
- }
- if (strstr(bufferRX, "setTimer3Stop,")) {
- stringToArray();
- myTimer3.stopTime = tdArray[0] * 3600 + tdArray[1] * 60 + tdArray[2];
- Serial.println("Impostato Stop Timer 3");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement