Advertisement
TolentinoCotesta

RTC multiple timers

Apr 16th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  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
  10.  
  11. // Definiamo una struttura per il nostro Timer n
  12. // in questo modo posso conoscere con maggior chiarezza quale timer è attualmente attivo
  13. // e volendo differenziare le azioni da compiere
  14. struct timer_n {
  15.    unsigned long start;
  16.    unsigned long stop ;
  17.    bool state;
  18. } ;
  19.  
  20. // ed un array per contenere NUM_TIMER strutture
  21. // Usiamo ad esempio 3 timer
  22. #define NUM_TIMER 3
  23. timer_n timers[NUM_TIMER];
  24.  
  25. /* Potrei anche definire ciasccun timer con un più semplice
  26. timer_n timer4;
  27. timer4.start = 10*HH + 30*MM;  
  28. timer4.stop  = 11*HH + 30*MM;
  29. ma usando un array posso, ad esempio, facilmente iterare gli n timer con un ciclo for
  30. */
  31.  
  32. void setup () {
  33.    pinMode(2, OUTPUT);  
  34.    pinMode(3, OUTPUT);  
  35.    Serial.begin(57600);
  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[1].start = 12*HH + 11*MM;    
  45.    timers[1].stop  = 12*HH + 12*MM;
  46.    timers[2].start = 23*HH + 30*MM;    
  47.    timers[2].stop  = 03*HH + 30*MM;
  48.  
  49.    if (! rtc.isrunning()) {
  50.       Serial.println("RTC is NOT running!");
  51.     // following line sets the RTC to the date & time this sketch was compiled
  52.     // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  53.     // This line sets the RTC with an explicit date & time, for example to set
  54.     // January 21, 2014 at 3am you would call:
  55.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  56.    }
  57. }
  58.  
  59. void loop () {
  60.     DateTime now = rtc.now();
  61.     // Stampiamo la data corrente in un formato facilmente leggibile
  62.     char buf[20];
  63.     sprintf(buf, "%02d/%02d/%04d %02d:%02d:%02d", now.day(), now.month(),  now.year(), now.hour(),  now.minute(), now.second());
  64.     Serial.println(buf);
  65.    
  66.     // Stampiamo la data corrente nel formato Unix Timestamp ed anche il valore corrispondente all'inizio del giorno
  67.     unsigned long start_of_day = now.unixtime() - now.hour()*3600UL  -  now.minute()*60UL - now.second();
  68.     Serial.print("Epoch time: ");
  69.     Serial.println(now.unixtime());    
  70.     Serial.print("Epoch start_of_day: ");
  71.     Serial.println(start_of_day);
  72.    
  73.     // Controlliamo se uno tra gli NUM_TIMER timer definiti deve essere attivato
  74.     out_status = false;
  75.     for (byte i=0; i<NUM_TIMER; i++){
  76.       unsigned long start_time = timers[i].start + start_of_day;
  77.       unsigned long stop_time = timers[i].stop + start_of_day;
  78.       unsigned long actual_time = now.unixtime();
  79.  
  80.       // Se stop_time è minore di start_time, allora fa riferimento al giorno successivo
  81.       if(stop_time < start_time)
  82.          stop_time += 86400UL;
  83.       Serial.print("Timer ");
  84.       Serial.print(i);
  85.       Serial.print(": Start ");
  86.       Serial.print(start_time);
  87.       Serial.print(" - Stop ");
  88.       Serial.print(stop_time);      
  89.       if( (actual_time > start_time)&&(actual_time < stop_time) ){
  90.          out_status = true;
  91.          timers[i].state = out_status;   
  92.       }      
  93.      
  94.       Serial.print(" Stato: ");
  95.       if( timers[i].state)
  96.          Serial.println("ATTIVO");   
  97.       else
  98.          Serial.println("NON ATTIVO");   
  99.     }
  100.    
  101.     // Scriviamo lo stato delle uscite
  102.     digitalWrite(2, out_status);    
  103.     digitalWrite(3, timers[1].state);
  104.    
  105.     delay(5000);
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement