Ciao, sto usando questo sketch per un timer luci Arduino-RTC-Timer/RTC_Light_Timer.pde at master · fouldsy/Arduino-RTC-Timer · GitHub
però si accendono solo quando alla data ora precisa al secondo, però se per caso va via la corrente, il timer non riaccende le luci se è passato quel dato momento.
Come posso fare a fargli fare ciò che voglio???
Ho modificato questi punti:
if (now.second() >= 0) { // in modo tale che si accenda anche dopo che siano passate le 0 sencondi
if (now.hour() >= startHour && now.minute() >= startMinute) // in modo da potersi accendere anche dopo l'orario
if (now.hour() >= endHour && now.minute() >= endMinute) {// idem
Praticamente ho cambiato da == a >=.
Però l' unico problema e che si accende a mezzanotte, scattate le 00:00 si accende la luce. Perché???
Qualcuno mi sa aiutare???
Mettiamo che hai impostato l' accensione alle 21 e lo spegnimento alle 22:
Alle 21 succedera' questo:
if 21 >= 21 ( condizione vera )
if 21 >= 22 (condizione falsa )
La lampada si accendera' e basta.
Alle 22 succedera' questo:
if 22 >= 21 ... ( condizione vera )
if 22 >= 22 ... (condizione vera )
La lampada si accendera' e subito dopo si spegnera.
Alle 23 succedera' questo:
if 23 >= 21 ... ( condizione vera )
if 23 >= 22 ... (condizione vera )
La lampada si accendera' e subito dopo si spegnera.
Alle 23:59:59 succedera' questo:
if 23:59:59 >= 21 ( condizione vera )
...passa una frazione di secondo...
if 00:00:00 >= 22 (condizione falsa)
La lampada si accendera' e basta.
Guardando il codice da cui hai preso spunto non rilevo incongruenze... Magari posta il codice da te modificato così da vedere se c'è qualche incongruenza...
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;
int ledPin = 2; // Set our LED pin
byte startHour = 20; // Set our start and end times
byte startMinute = 24; // Don't add leading zero to hour or minute - 7, not 07
byte endHour = 20; // Use 24h format for the hour, without leading zero
byte endMinute = 25;
byte validStart = 0; // Declare and set to 0 our start flag
byte poweredOn = 0; // Declare and set to 0 our current power flag
byte validEnd = 0; // Declare and set to 0 our end flag
void setup () {
pinMode(ledPin, OUTPUT); // Set our LED as an output pin
digitalWrite(ledPin, LOW); // Set the LED to LOW (off)
Wire.begin(); // Start our wire and real time clock
RTC.begin();
if (! RTC.isrunning()) { // Make sure RTC is running
Serial.println("RTC is NOT running!");
//RTC.adjust(DateTime(__DATE__, __TIME__)); // Uncomment to set the date and time
}
}
void loop () {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.second() >= 0) { // Only process if we have ticked over to new minute
if (poweredOn == 0) { // Check if lights currently powered on
checkStartTime(); // If they are not, check if it's time to turn them on
} else {
checkEndTime(); // Otherwise, check if it's time to turn them off
}
if (validStart == 1) { // If our timer is flagged to start, turn the lights on
turnLightsOn();
}
if (validEnd == 1) { // If our time is flagged to end, turn the lights off
turnLightsOff();
}
}
delay(1000);
}
byte checkStartTime() {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.hour() >= startHour && now.minute() >= startMinute) {
validStart = 1; // If our start hour and minute match the current time, set 'on' flags
poweredOn = 1;
} else {
validStart = 0; // Otherwise we don't need to power up the lights yet
}
return validStart; // Return the status for powering up
}
byte checkEndTime() {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.hour() >= endHour && now.minute() >= endMinute) {
validEnd = 1; // If our end hour and minute match the current time, set the 'off' flags
poweredOn = 0;
} else {
validEnd = 0; // Otherwise we don't need to power off the lights yet
}
return validEnd; // Return the status for powering off
}
void turnLightsOn() {
digitalWrite(ledPin, HIGH); // Turn on the LED
}
void turnLightsOff() {
digitalWrite(ledPin, LOW); // Turn off the LED
}
acik:
Mettiamo che hai impostato l' accensione alle 21 e lo spegnimento alle 22:
Alle 21 succedera' questo:
if 21 >= 21 ( condizione vera )
if 21 >= 22 (condizione falsa )
La lampada si accendera' e basta.
Alle 22 succedera' questo:
if 22 >= 21 ... ( condizione vera )
if 22 >= 22 ... (condizione vera )
La lampada si accendera' e subito dopo si spegnera.
Alle 23 succedera' questo:
if 23 >= 21 ... ( condizione vera )
if 23 >= 22 ... (condizione vera )
La lampada si accendera' e subito dopo si spegnera.
Alle 23:59:59 succedera' questo:
if 23:59:59 >= 21 ( condizione vera )
...passa una frazione di secondo...
if 00:00:00 >= 22 (condizione falsa)
La lampada si accendera' e basta.
OK, ho capito, ed allora come faccio a risolvere il problema??
sako84:
OK, ho capito, ed allora come faccio a risolvere il problema??
Trasponendo in codice i suggerimenti ricevuti
Il problema non è difficile, hai 2 orari - 1 di attacco ed 1 di stacco. Se l'orario è maggiore di quello di stacco e minore di quello di attacco, luci spente. Altrimenti luci accese. Tieni conto che hai la mezzanotte nel mezzo.
sako84:
OK, ho capito, ed allora come faccio a risolvere il problema??
Trasponendo in codice i suggerimenti ricevuti
Il problema non è difficile, hai 2 orari - 1 di attacco ed 1 di stacco. Se l'orario è maggiore di quello di stacco e minore di quello di attacco, luci spente. Altrimenti luci accese. Tieni conto che hai la mezzanotte nel mezzo.
Appunto come faccio se voglio che siano accese dalle 6:30 alle 18:30 e spente tutto il resto del tempo..
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 RTC;
int ledPin = 2; // Set our LED pin
byte startHour = 20; // Set our start and end times
byte startMinute = 24; // Don't add leading zero to hour or minute - 7, not 07
byte endHour = 20; // Use 24h format for the hour, without leading zero
byte endMinute = 25;
byte validStart = 0; // Declare and set to 0 our start flag
byte poweredOn = 0; // Declare and set to 0 our current power flag
byte validEnd = 0; // Declare and set to 0 our end flag
void setup () {
pinMode(ledPin, OUTPUT); // Set our LED as an output pin
digitalWrite(ledPin, LOW); // Set the LED to LOW (off)
Wire.begin(); // Start our wire and real time clock
RTC.begin();
if (! RTC.isrunning()) { // Make sure RTC is running
Serial.println("RTC is NOT running!");
//RTC.adjust(DateTime(__DATE__, __TIME__)); // Uncomment to set the date and time
}
}
void loop () {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.second() >= 0) { // Only process if we have ticked over to new minute
if (poweredOn == 0) { // Check if lights currently powered on
checkStartTime(); // If they are not, check if it's time to turn them on
} else {
checkEndTime(); // Otherwise, check if it's time to turn them off
}
if (validStart == 1) { // If our timer is flagged to start, turn the lights on
turnLightsOn();
}
if (validEnd == 1) { // If our time is flagged to end, turn the lights off
turnLightsOff();
}
}
delay(1000);
}
byte checkStartTime() {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.hour() < startHour && now.minute() < startMinute) {
validStart = 0; // Otherwise we don't need to power up the lights yet
} else {
validStart = 1; // If our start hour and minute match the current time, set 'on' flags
poweredOn = 1;
}
return validStart; // Return the status for powering up
}
byte checkEndTime() {
DateTime now = RTC.now(); // Read in what our current datestamp is from RTC
if (now.hour() >= endHour && now.minute() >= endMinute) {
validEnd = 1; // If our end hour and minute match the current time, set the 'off' flags
poweredOn = 0;
} else {
validEnd = 0; // Otherwise we don't need to power off the lights yet
}
return validEnd; // Return the status for powering off
}
void turnLightsOn() {
digitalWrite(ledPin, HIGH); // Turn on the LED
}
void turnLightsOff() {
digitalWrite(ledPin, LOW); // Turn off the LED
}
Così dovrebbe andare.
Però ho un altro quesito da porre.
Allora nell'ultima parte
void turnLightsOn() {
digitalWrite(ledPin, HIGH); // Turn on the LED
}
void turnLightsOff() {
digitalWrite(ledPin, LOW); // Turn off the LED
}
si dice cosa deve fare, ma se io voglio far accendere prima una luce e poi un altra dopo un tot di tempo come devo fare??
Avevo fatto così:
void turnLightsOn() {
digitalWrite(ledPin, HIGH); // Turn on the LED
delay (60000);
digitalWrite(ledPin2, HIGH);
delay (60000);
digitalWrite(ledPin3, HIGH);
}
void turnLightsOff() {
digitalWrite(ledPin, LOW); // Turn off the LED
}
Però mi vanno in loop, come posso fare per farlo fare una sola volta??