Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "RTClib.h"
- RTC_DS1307 rtc;
- #define DD 86400UL // numero secondi in un giorno
- #define HH 3600UL // numero secondi in un'ora
- #define MM 60UL // numero secondi in un minuto
- bool out_status = false; // variabile di stato
- // Definiamo una struttura per il nostro Timer n
- // in questo modo posso conoscere con maggior chiarezza quale timer è attualmente attivo
- // e volendo differenziare le azioni da compiere
- struct timer_n {
- unsigned long start;
- unsigned long stop ;
- bool state;
- } ;
- // ed un array per contenere NUM_TIMER strutture
- // Usiamo ad esempio 3 timer
- #define NUM_TIMER 3
- timer_n timers[NUM_TIMER];
- /* Potrei anche definire ciasccun timer con un più semplice
- timer_n timer4;
- timer4.start = 10*HH + 30*MM;
- timer4.stop = 11*HH + 30*MM;
- ma usando un array posso, ad esempio, facilmente iterare gli n timer con un ciclo for
- */
- void setup () {
- pinMode(2, OUTPUT);
- pinMode(3, OUTPUT);
- Serial.begin(57600);
- Wire.begin();
- rtc.begin();
- // Inizializzo i 3 timer con dei valori di esempio
- // nella versione definitiva può essere una stringa formattata JSON o qualsiasi altro metodo "dinamico"
- // in modo che l'utente possa definire in autonomia gli intervalli senza dover ricompilare ogni volta
- timers[0].start = 10*HH + 30*MM; // Start 10:30
- timers[0].stop = 10*HH + 40*MM; // Stop 10:40
- timers[1].start = 12*HH + 11*MM;
- timers[1].stop = 12*HH + 12*MM;
- timers[2].start = 23*HH + 30*MM;
- timers[2].stop = 03*HH + 30*MM;
- if (! rtc.isrunning()) {
- Serial.println("RTC is NOT running!");
- // following line sets the RTC to the date & time this sketch was compiled
- // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
- }
- }
- void loop () {
- DateTime now = rtc.now();
- // Stampiamo la data corrente in un formato facilmente leggibile
- char buf[20];
- sprintf(buf, "%02d/%02d/%04d %02d:%02d:%02d", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
- Serial.println(buf);
- // Stampiamo la data corrente nel formato Unix Timestamp ed anche il valore corrispondente all'inizio del giorno
- unsigned long start_of_day = now.unixtime() - now.hour()*3600UL - now.minute()*60UL - now.second();
- Serial.print("Epoch time: ");
- Serial.println(now.unixtime());
- Serial.print("Epoch start_of_day: ");
- Serial.println(start_of_day);
- // Controlliamo se uno tra gli NUM_TIMER timer definiti deve essere attivato
- out_status = false;
- for (byte i=0; i<NUM_TIMER; i++){
- unsigned long start_time = timers[i].start + start_of_day;
- unsigned long stop_time = timers[i].stop + start_of_day;
- unsigned long actual_time = now.unixtime();
- // Se stop_time è minore di start_time, allora fa riferimento al giorno successivo
- if(stop_time < start_time)
- stop_time += 86400UL;
- Serial.print("Timer ");
- Serial.print(i);
- Serial.print(": Start ");
- Serial.print(start_time);
- Serial.print(" - Stop ");
- Serial.print(stop_time);
- if( (actual_time > start_time)&&(actual_time < stop_time) ){
- out_status = true;
- timers[i].state = out_status;
- }
- Serial.print(" Stato: ");
- if( timers[i].state)
- Serial.println("ATTIVO");
- else
- Serial.println("NON ATTIVO");
- }
- // Scriviamo lo stato delle uscite
- digitalWrite(2, out_status);
- digitalWrite(3, timers[1].state);
- delay(5000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement