SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <Wire.h> | |
| 2 | #include "RTClib.h" | |
| 3 | ||
| 4 | RTC_DS1307 rtc; | |
| 5 | #define DD 86400UL // numero secondi in un giorno | |
| 6 | #define HH 3600UL // numero secondi in un'ora | |
| 7 | #define MM 60UL // numero secondi in un minuto | |
| 8 | ||
| 9 | - | bool out_status = false; // variabile di stato |
| 9 | + | |
| 10 | // in questo modo posso conoscere con maggior chiarezza quale timer è attualmente attivo | |
| 11 | // e volendo differenziare le azioni da compiere | |
| 12 | struct timer_n {
| |
| 13 | unsigned long start; | |
| 14 | unsigned long stop ; | |
| 15 | bool state; | |
| 16 | unsigned int pin; | |
| 17 | } ; | |
| 18 | ||
| 19 | // ed un array per contenere NUM_TIMER strutture | |
| 20 | // Usiamo ad esempio 3 timer | |
| 21 | #define NUM_TIMER 3 | |
| 22 | timer_n timers[NUM_TIMER]; | |
| 23 | ||
| 24 | /* Potrei anche definire ciasccun timer con un più semplice | |
| 25 | timer_n timer4; | |
| 26 | timer4.start = 10*HH + 30*MM; | |
| 27 | timer4.stop = 11*HH + 30*MM; | |
| 28 | ma usando un array posso, ad esempio, facilmente iterare gli n timer con un ciclo for | |
| 29 | */ | |
| 30 | ||
| 31 | void setup () {
| |
| 32 | pinMode(2, OUTPUT); | |
| 33 | pinMode(3, OUTPUT); | |
| 34 | pinMode(4, OUTPUT); | |
| 35 | - | Serial.begin(57600); |
| 35 | + | Serial.begin(115200); |
| 36 | Wire.begin(); | |
| 37 | rtc.begin(); | |
| 38 | ||
| 39 | // Inizializzo i 3 timer con dei valori di esempio | |
| 40 | // nella versione definitiva può essere una stringa formattata JSON o qualsiasi altro metodo "dinamico" | |
| 41 | // in modo che l'utente possa definire in autonomia gli intervalli senza dover ricompilare ogni volta | |
| 42 | timers[0].start = 10*HH + 30*MM; // Start 10:30 | |
| 43 | timers[0].stop = 10*HH + 40*MM; // Stop 10:40 | |
| 44 | timers[0].pin = 2; | |
| 45 | timers[1].start = 12*HH + 11*MM; | |
| 46 | timers[1].stop = 12*HH + 12*MM; | |
| 47 | timers[1].pin = 3; | |
| 48 | timers[2].start = 23*HH + 30*MM; | |
| 49 | timers[2].stop = 03*HH + 30*MM; | |
| 50 | timers[2].pin = 4; | |
| 51 | ||
| 52 | if (! rtc.isrunning()) {
| |
| 53 | Serial.println("RTC is NOT running!");
| |
| 54 | // following line sets the RTC to the date & time this sketch was compiled | |
| 55 | // rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); | |
| 56 | // This line sets the RTC with an explicit date & time, for example to set | |
| 57 | // January 21, 2014 at 3am you would call: | |
| 58 | // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); | |
| 59 | } | |
| 60 | - | DateTime now = rtc.now(); |
| 60 | + | |
| 61 | - | // Stampiamo la data corrente in un formato facilmente leggibile |
| 61 | + | |
| 62 | - | char buf[20]; |
| 62 | + | |
| 63 | - | sprintf(buf, "%02d/%02d/%04d %02d:%02d:%02d", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second()); |
| 63 | + | DateTime now = rtc.now(); |
| 64 | - | Serial.println(buf); |
| 64 | + | // Calcoliamo in formato Unix Timestamp il valore corrispondente all'inizio del giorno |
| 65 | unsigned long start_of_day = now.unixtime() - now.hour()*3600UL - now.minute()*60UL - now.second(); | |
| 66 | - | // Stampiamo la data corrente nel formato Unix Timestamp ed anche il valore corrispondente all'inizio del giorno |
| 66 | + | |
| 67 | // Controlliamo se uno tra gli NUM_TIMER timer definiti deve essere attivato | |
| 68 | - | Serial.print("Epoch time: ");
|
| 68 | + | |
| 69 | - | Serial.println(now.unixtime()); |
| 69 | + | |
| 70 | - | Serial.print("Epoch start_of_day: ");
|
| 70 | + | |
| 71 | - | Serial.println(start_of_day); |
| 71 | + | |
| 72 | ||
| 73 | // Salviamo lo stato corrente in una variabile temporanea | |
| 74 | - | out_status = false; |
| 74 | + | bool oldstate = timers[i].state; |
| 75 | ||
| 76 | // Se stop_time è minore di start_time, allora fa riferimento al giorno successivo | |
| 77 | if(stop_time < start_time) | |
| 78 | stop_time += DD ; | |
| 79 | ||
| 80 | // é ora di cambiare stato? | |
| 81 | if( (actual_time > start_time) && (actual_time < stop_time) ){
| |
| 82 | - | stop_time += 86400UL; |
| 82 | + | timers[i].state = true; |
| 83 | - | Serial.print("Timer ");
|
| 83 | + | } else {
|
| 84 | - | Serial.print(i); |
| 84 | + | timers[i].state = false; |
| 85 | - | Serial.print(": Start ");
|
| 85 | + | } |
| 86 | - | Serial.print(start_time); |
| 86 | + | |
| 87 | - | Serial.print(" - Stop ");
|
| 87 | + | // Scriviamo lo stato attuale dell'uscita associata al timer |
| 88 | - | Serial.print(stop_time); |
| 88 | + | digitalWrite(timers[i].pin, timers[i]state); |
| 89 | - | if( (actual_time > start_time)&&(actual_time < stop_time) ){
|
| 89 | + | |
| 90 | - | out_status = true; |
| 90 | + | // Stato dell'uscita è cambiato, un po' di informazioni di debug |
| 91 | - | timers[i].state = out_status; |
| 91 | + | if(oldstate != timers[i].state){
|
| 92 | - | } |
| 92 | + | // Stampiamo la data corrente in un formato facilmente leggibile |
| 93 | char buf[20]; | |
| 94 | - | Serial.print(" Stato: ");
|
| 94 | + | sprintf(buf, "%02d/%02d/%04d %02d:%02d:%02d", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second()); |
| 95 | - | if( timers[i].state) |
| 95 | + | Serial.println(buf); |
| 96 | - | Serial.println("ATTIVO");
|
| 96 | + | |
| 97 | - | else |
| 97 | + | Serial.print("Timer ");
|
| 98 | - | Serial.println("NON ATTIVO");
|
| 98 | + | Serial.print(i); |
| 99 | - | } |
| 99 | + | Serial.print(": Start ");
|
| 100 | Serial.print(start_time); | |
| 101 | - | // Scriviamo lo stato delle uscite |
| 101 | + | Serial.print(" - Stop ");
|
| 102 | - | digitalWrite(2, out_status); |
| 102 | + | Serial.println(stop_time); |
| 103 | - | digitalWrite(3, timers[1].state); |
| 103 | + | Serial.print("Stato uscita: ");
|
| 104 | Serial.println(timers[i].state); | |
| 105 | - | delay(5000); |
| 105 | + | } |
| 106 | ||
| 107 | } |