Patrick_M:
ho dato un'occhiata al readme.txt della libreria degli allarmi e ho trovato questo:
Alarm.timerOnce(Hour, Minute, Second, TimerFunction);
Description: As timerOnce above, but period is the number of seconds in the given Hour, Minute and Second parameters
quindi puoi impostare l'allarme all'orario che vuoi come gia facevi e impostare la durata per l'intervento con la timerOnce che accetta ore, minuti e secondi
quindi seguendo l'esempio
#include <Time.h>
#include <TimeAlarms.h>
void setup()
{
Serial.begin(9600);
setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011
// create the alarms
Alarm.alarmRepeat(8,30,0, MorningAlarm); // 8:30am every day
Alarm.alarmRepeat(17,45,0,EveningAlarm); // 5:45pm every day
//
// nel caso di mancanza corrente
// qui ci va il controllo se siamo in un orario successivo al previsto per l'allarme
//per l'attivazione/disattivazione dei relè
// qualche cosa tipo
// if (oraAdesso > oraPrimoAllarme && oraAdesso < oraFineAllarme)
// attiva/disattiva relè;
}
void loop(){
digitalClockDisplay(); //fai quello che serve
Alarm.delay(1000); // wait one second between clock display
}
// functions to be called when an alarm triggers:
void MorningAlarm(){
Serial.println("Alarm: - accendi relè 1");
// esegui le varie istruzioni
Alarm.timerOnce(Hour, Minute, Second, EndFunction); // tra quanto tempo in ore, minuti, secondi intervenire
}
void EndFunction() {
serial.println("Spengo relè 1");
// esegui le varie istruzioni
}
Grazie mille per avermi illuminato! Lo sketch inizia a prendere la strada giusta. Peccato però che se va via la corrente non considera l'allarme. Faccio un esempio allegando il codice aggiornato secondo le tue indcazioni:
/*
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 LOW
#define RelayOff HIGH
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 , RelayOff);
pinMode(releCO2 , OUTPUT);
digitalWrite(releAereatore , RelayOff);
pinMode(releAereatore , OUTPUT);
digitalWrite(releTermo , RelayOff);
pinMode(releTermo , OUTPUT);
digitalWrite(relePlafo , RelayOff);
pinMode(relePlafo , OUTPUT);
// create the alarms, to trigger at specific times
Alarm.alarmRepeat(23, 1, 0, CO2); // 18:430 every day
Alarm.alarmRepeat(23, 3, 30, Plafo); // 21:43:00 every day
digitalClockDisplay();
}
void loop() {
printTime();
Alarm.delay(0); // wait one second between clock display
//CO2();
}
// 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 , RelayOff); // Spengo Aereatore
Alarm.timerOnce(1, 0, 00, EndCO2); // tra quanto tempo in ore, minuti, secondi intervenire
}
void EndCO2() {
Serial.println("Spengo CO2");
lcd.setCursor(0, 0);
lcd.print("CO2:OFF AIR:OFF");
digitalWrite(releCO2 , RelayOff); // Spengo CO2
digitalWrite(releAereatore , RelayOn); //Accendo Aereatore
}
void Plafo() {
lcd.setCursor(0, 0);
lcd.print("PLAFO:ON ");
Serial.println("Alarm: Accendo LUCI");
digitalWrite(relePlafo , RelayOn); // Accendo luci
Alarm.timerOnce(3, 0, 0, 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
}
Secondo questo esempio se carico lo sketch alle ore 23:02:00 il codice non mi tiene conto dell'allarme CO2 impostato alle ore 23:01:00, considerando invece correttamente quello Plafo delle 23:3:30.
Inoltre mi assale anche un altro dubbio. La libreria da me usata gestisce automaticamente il passaggio all'ora solare/legale? Considera automaticamente anche gli anni bisestili?
Grazie.