Advertisement
TolentinoCotesta

n Timer2

Jan 20th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. // Definiamo una struttura per il nostro Timer n
  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(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. }
  61.  
  62. void loop () {
  63.     DateTime now = rtc.now();    
  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.    
  67.     // Controlliamo se uno tra gli NUM_TIMER timer definiti deve essere attivato
  68.     for (byte i=0; i<NUM_TIMER; i++){
  69.       unsigned long start_time = timers[i].start + start_of_day;
  70.       unsigned long stop_time = timers[i].stop + start_of_day;
  71.       unsigned long actual_time = now.unixtime();
  72.  
  73.       // Salviamo lo stato corrente in una variabile temporanea
  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.          timers[i].state = true;     
  83.       } else {    
  84.          timers[i].state = false;    
  85.       }
  86.  
  87.       // Scriviamo lo stato attuale dell'uscita associata al timer
  88.       digitalWrite(timers[i].pin, timers[i]state);   
  89.      
  90.       // Stato dell'uscita è cambiato, un po' di informazioni di debug
  91.       if(oldstate != timers[i].state){
  92.         // Stampiamo la data corrente in un formato facilmente leggibile
  93.         char buf[20];
  94.         sprintf(buf, "%02d/%02d/%04d %02d:%02d:%02d", now.day(), now.month(),  now.year(), now.hour(),  now.minute(), now.second());
  95.         Serial.println(buf);
  96.    
  97.         Serial.print("Timer ");
  98.         Serial.print(i);
  99.         Serial.print(": Start ");
  100.         Serial.print(start_time);
  101.         Serial.print(" - Stop ");
  102.         Serial.println(stop_time);  
  103.         Serial.print("Stato uscita: ");
  104.         Serial.println(timers[i].state);
  105.      }
  106.    
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement