Dopo varie capocciate sul muro per cercare di capire come dichiarare le varie variabili finalmente sono riuscito ad ottenere ciò che volevo.
Ora mi chiedo se esiste un metodo più snello ed elegante per avere lo stesso risultato.
/*
Esempio per accensione ed spegnimento relè temporizzati
*/
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <LiquidCrystal_I2C.h>
#include <DS3232RTC.h>
#include <Wire.h>
AlarmId id;
// lcd object
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
//================================= DEFINISCO LE VARIABILI DEI RELE' ====================================
const int releCO2 = 8; //relè CO2 R1 NC
const int relePlafo = 9; //relè Plafoniera R4 NA
const int releTermo = 10; //relè Temoriscaldatore R3 NA
const int releAereatore = 12; //relè Aereatore R2 NA
#define RelayOn HIGH
#define RelayOff LOW
#define RelayAirOn LOW
#define RelayAirOff HIGH
#define RelayTermoOn LOW
#define RelayTermoOff HIGH
#define RelayPlafoOn LOW
#define RelayPlafoOff HIGH
//unsigned long CO2Timer[4]; // array per ore, minuti, secondi, durata
unsigned long AirTimer[4]; // array per ore, minuti, secondi, durata
//unsigned longPlafoTimer[4]; // array per ore, minuti, secondi, durata
unsigned long CO2Timer [4] = {10, 3, 0, 10}; // imposto timer alle 08:14:00 per la durata di 4 ore
unsigned long PlafoTimer [4] = {10, 11, 40, 10}; // imposto timer Plafo alle 11:14:30 per la durata di 30 ore
void setup() {
Serial.begin(57600);
while (!Serial) ; // wait for Arduino Serial Monitor
setSyncProvider(RTC.get); // the function to get the time from the RTC
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
//setTime(01, 59, 30, 3, 24, 18); // set time to Saturday 8:29:00am Jan 1 2011
lcd.init();
lcd.clear();
lcd.backlight();
digitalWrite(releCO2 , RelayOn);
pinMode(releCO2 , OUTPUT);
digitalWrite(releAereatore , RelayAirOff);
pinMode(releAereatore , OUTPUT);
digitalWrite(releTermo , RelayTermoOn);
pinMode(releTermo , OUTPUT);
digitalWrite(relePlafo , RelayPlafoOff);
pinMode(relePlafo , OUTPUT);
// create the alarms, to trigger at specific times
Alarm.alarmRepeat(CO2Timer[0], CO2Timer[1], CO2Timer[2], CO2); // 18:430 every day
Alarm.alarmRepeat(PlafoTimer[0], PlafoTimer[1], PlafoTimer[2], Plafo); // 21:43:00 every day
digitalClockDisplay();
unsigned long ora[3] = {hour(), minute(), second()};
}
void loop() {
printTime();
Alarm.delay(0); // wait one second between clock display
checkTimers();
}
// functions to be called when an alarm triggers:
void CO2() {
Serial.println("Alarm: accendo CO2");
lcd.setCursor(0, 0);
lcd.print("CO2:ON AIR:OFF");
digitalWrite(releCO2 , RelayOn); // Accendo CO2
digitalWrite(releAereatore , RelayAirOff); // Spengo Aereatore
Alarm.timerOnce(0, 0, CO2Timer[3], EndCO2); // tra quanto tempo in ore, minuti, secondi intervenire
}
void EndCO2() {
Serial.println("Spengo CO2 - accendo AIR");
lcd.setCursor(0, 0);
lcd.print("CO2:OFF AIR:ON ");
digitalWrite(releAereatore , RelayAirOn); //Accendo Aereatore
}
void Plafo() {
lcd.setCursor(0, 0);
lcd.print("PLAFO:ON ");
Serial.println("Alarm: Accendo LUCI");
digitalWrite(relePlafo , RelayOn); // Accendo luci
Alarm.timerOnce(0, 0, PlafoTimer[3], EndPlafo); // tra quanto tempo in ore, minuti, secondi intervenire
}
void EndPlafo() {
Serial.println("Spengo LUCI");
lcd.setCursor(0, 0);
lcd.print("PLAFO:OFF ");
digitalWrite(relePlafo , RelayOff); //Spengo luci
// esegui le varie istruzioni
}
void p2dig(uint8_t v)
// Print 2 digits with leading zero
{
if (v < 10) lcd.print("0");
lcd.print(v);
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
const char *dow2String(uint8_t code)
// Day of week to string. DOW 1=Sunday, 0 is undefined
{
static const char *str[] = {"---", "Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab"};
if (code > 7) code = 0;
return (str[code]);
}
void printTime(void)
// Display the current time on the display
{
// display the current time
lcd.setCursor(0, 1);
p2dig (hour());
lcd.print(":");
p2dig(minute());
lcd.print(":");
p2dig(second());
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(' ');
Serial.print(day());
Serial.print('/');
Serial.print(month());
Serial.print('/');
Serial.print(year());
//Serial.println();
Alarm.delay(1000); // wait one second between clock display
}
void checkTimers() {
// verifico l'ora attuale per rispettare i timers in caso di mancanza di corrente
unsigned long ora[3] = {hour(), minute(), second()};
unsigned long nows = ( (ora[0] * 3600) + (ora[1] * 60) + ora[2] ); // trasformo l'ora attuale in secondi
unsigned long CO2Start = ( (CO2Timer[0] * 3600) + (CO2Timer[1] * 60) + CO2Timer[2] );
unsigned long PlafoStart = ( (PlafoTimer[0] * 3600) + (PlafoTimer[1] * 60) + PlafoTimer[2] );
unsigned long CO2End = (CO2Timer[3] * 3600);
unsigned long PlafoEnd = (PlafoTimer[3] * 3600);
Serial.print("CO2Start: ");
Serial.print (CO2Start);
Serial.print(" Durata: ");
Serial.print (CO2End);
Serial.print(" Totale Secondi: ");
Serial.print (CO2Start + CO2End);
/* Serial.print(" - PlafoStart: ");
Serial.print (PlafoStart);
Serial.print(" PlafoEnd: ");
Serial.print (PlafoEnd);
*/
Serial.print(" - now: ");
Serial.print(nows);
Serial.print (" ");
if ( (nows >= CO2Start) && (nows <= (CO2Start + CO2End))) {
Serial.print (" CO2 ACCESA - AIR SPENTA ");
lcd.setCursor(0, 0);
lcd.print("CO2:ON-AIR:OFF");
digitalWrite(releCO2 , RelayOn); // Accendo CO2
digitalWrite(releAereatore , RelayAirOff); // Spengo Aereatore
}
else {
Serial.print (" CO2 SPENTA - AIR ACCESA ");
lcd.setCursor(0, 0);
lcd.print("CO2:OFF AIR:ON ");
digitalWrite(releCO2 , RelayOff); // Spengo CO2
digitalWrite(releAereatore , RelayAirOn); // Accendo Aereatore
}
if ((nows >= PlafoStart) && (nows <= PlafoStart + PlafoEnd)) {
Serial.print (" PLAFO ACCESA ");
lcd.setCursor(9, 1);
lcd.print("LED:ON");
digitalWrite(relePlafo , RelayPlafoOn); // Accendo Plafo
}
else {
Serial.print (" PLAFO SPENTA ");
lcd.setCursor(9, 1);
lcd.print("LED:OFF");
digitalWrite(relePlafo , RelayPlafoOff); // Spengo Plafo
}
Serial.println ();
}