Buongiorno ho un mega 2560 quando carico lo sketch mi da dat e ora corretta ma se stacco corrente ritorna indietro a quando lo avevo compilato, seguendo la procedura cioè flasharlo con la riga adjust time e poi senza quella riga va alle 6.28 del 2106 cosa posso fare?, leggevo su internet che lo sketch bisogna che venga passato anche dall rtc con un pulsante intanto ecco mio sketch :
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <RTClib.h>
RTC_Millis RTC;
PCF8574_HD44780_I2C lcd(0x3F, 20, 4);
int oraAlba = 9;
int minAlba = 10;
long albas;
int durataAlbas = 720;
int oraTramonto = 17;
int minTramonto = 10;
long tramontos;
int durataTramontos = 720;
long nows;
int delayR = 0;
int delayG = 120;
int delayB = 240;
int delayUV = 360;
int delayW = 360;
int pinR = 2;
int pinG = 3;
int pinB = 4;
int pinW = 5;
int pinUV = 6;
//rele
float rele1 = 35;
float rele2 = 37;
int rele3 = 39;
float rele4 = 41;
int rele5 = 43;
char buffer1[20];
char buffer2[20];
float riscaldatore = 22.0; // allerta temperatura acqua bassa
float alertacqua = 26.0; // ventola tangenziale
float alertplafo = 44.0; // alerta plafoniera temperatura troppo alta
float tempSoglia = 38.0; //temperatura ventole plafo
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tmp_address;
float temp0=0;
float temp1=0;
int numberOfDevices;
#define VENTOLA 35
#define TANG 37
#define NOTTE 39
#define PUMP 41
#define PUMP1 43
#define RISC 45
void setup()
{
lcd.init(); // LCD Initialization
lcd.backlight( ); // Backlight ON
lcd.clear(); // Clear the display
numberOfDevices = 1;
Serial.begin(9600);
Wire.begin();
sensors.begin();
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinW, OUTPUT);
pinMode(pinUV, OUTPUT);
pinMode(rele1, OUTPUT);
pinMode(rele2, OUTPUT);
pinMode(rele3, OUTPUT);
pinMode(rele4, OUTPUT);
pinMode(rele5, OUTPUT);
pinMode(VENTOLA, OUTPUT);
pinMode(TANG, OUTPUT);
pinMode(RISC, OUTPUT);
pinMode(NOTTE, OUTPUT);
pinMode(PUMP, OUTPUT);
pinMode(PUMP1, OUTPUT);
digitalWrite( rele1, LOW );
digitalWrite( rele2, LOW );
digitalWrite( rele3, LOW );
digitalWrite( rele4, LOW );
digitalWrite(rele5, LOW);
///////////////////////////// SETTAGGIO ORA ////////////////////////////////////
/*
Come settare correttamente ora:
1) compilare lo sketch con quella riga , a) oppure b)
2) flashare l'Arduino con quello sketch, in modo che lo sketch regoli l'RTC
3) ricompilare lo sketch SENZA quella riga
4) riflashare l'Arduino con il nuovo sketch
Ora puoi fare tutti gli stacchi che vuoi e l'RTC andrà per conto suo correttamente.*/
RTC.adjust(DateTime(__DATE__, __TIME__)); // settaggio ora automatico b)
//RTC.begin(DateTime(__DATE__, __TIME__));
///////////////////////////////////////////////////////////////////////////////////
delay(3000);
}
void loop() {
// prendo ora attuale
DateTime now = RTC.now();
sprintf(buffer1, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
lcd.setCursor(0,0);
lcd.print( buffer1 );
sprintf(buffer2, "%02d/%02d/%d", now.day(), now.month(), now.year());
lcd.setCursor(0,1);
lcd.print( buffer2 );
Serial.println(buffer1);
Serial.println(buffer2);
// Richiesta temperatura
sensors.requestTemperatures(); // Comando per misurare la temp.
temp0 = sensors.getTempCByIndex(0);
lcd.setCursor(0,2); // Colonna 0, Riga 0
lcd.print("acqua:");
lcd.setCursor (6, 2);
lcd.print(temp0);
temp1 = sensors.getTempCByIndex(1);
lcd.setCursor (0,3); //Colonna 0, Riga 1
lcd.print("plafo:");
lcd.setCursor (6,3);
lcd.print(temp1);
//PLAFONIERA =TEMP1 vetole
if (temp1 > tempSoglia - 0.2 and - 0.6 ) {
digitalWrite(VENTOLA, LOW);
}
if (temp1 < tempSoglia + 0.2 and + 0.6) {
digitalWrite(VENTOLA, HIGH);
}
// ventola tangenziale
if (temp0 > alertacqua - 0.2 and - 0.6) {
digitalWrite(TANG, LOW);
}
if (temp0 < alertacqua + 0.2 and + 0.6) {
digitalWrite(TANG, HIGH);
}
// riscaldatore
if (temp0> riscaldatore + 1.0 and +4.0 ) {
digitalWrite (RISC, LOW);
}
if (temp0< riscaldatore - 1.0 and - 4.0 ){
digitalWrite (RISC, HIGH);
}
// Calcolo inizio alba in secondi
albas = oraAlba * 3600 + minAlba * 60;
// Calcolo inizio tramonto in secondi
tramontos = oraTramonto * 3600 + minTramonto * 60;
// Calcolo attuale in secondi
nows = now.hour() * 3600 + now.minute() * 60 + now.second();
// Notte
if (nows < albas || nows > tramontos + durataTramontos) {
digitalWrite(pinR, LOW);
digitalWrite(pinG, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinUV, LOW);
digitalWrite(pinW, LOW);
digitalWrite(NOTTE, HIGH);
digitalWrite(PUMP, LOW);
digitalWrite(PUMP1, HIGH);
}
// Alba
if (nows > albas && nows + delayR < albas + durataAlbas) {
analogWrite(pinR, 65 * (float)(((albas + durataAlbas) - nows) / (durataAlbas - delayR)));
}
if (nows > albas && nows + delayG < albas + durataAlbas) {
analogWrite(pinG, 65 * (float)(((albas + durataAlbas) - nows) / (durataAlbas - delayG)));
}
if (nows > albas && nows + delayB < albas + durataAlbas) {
analogWrite(pinB, 65 * (float)(((albas + durataAlbas) - nows) / (durataAlbas - delayB)));
}
if (nows > albas && nows + delayUV < albas + durataAlbas) {
analogWrite(pinUV, 65 * (float)(((albas + durataAlbas) - nows) / (durataAlbas - delayUV)));
}
if (nows > albas && nows + delayW < albas + durataAlbas) {
analogWrite(pinW, 65 * (float)(((albas + durataAlbas) - nows) / (durataAlbas - delayW)));
}
// Giorno
if (nows > albas + durataAlbas && nows < tramontos) {
digitalWrite(pinR, HIGH);
digitalWrite(pinG, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinUV, HIGH);
digitalWrite(pinW, HIGH);
digitalWrite(PUMP,HIGH);
digitalWrite (PUMP1,LOW);
digitalWrite (NOTTE, LOW);
}
if ( temp1 > alertplafo + 0.2 and + 0.6 ) {
digitalWrite(pinR, LOW);
digitalWrite(pinG, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinUV, LOW);
digitalWrite(pinW, LOW);
}
// Tramonto
if (nows > tramontos && nows + delayR < tramontos + durataTramontos) {
int v = 65 - (65 * (float)(((tramontos + durataTramontos) - nows) / (durataTramontos - delayR)));
analogWrite(pinR, v);
}
if (nows > tramontos && nows + delayG < tramontos + durataTramontos) {
int v = 65 - (65 * (float)(((tramontos + durataTramontos) - nows) / (durataTramontos - delayG)));
analogWrite(pinG, v);
}
if (nows > tramontos && nows + delayB < tramontos + durataTramontos) {
int v = 65 - (65 * (float)(((tramontos + durataTramontos) - nows) / (durataTramontos - delayB)));
analogWrite(pinB, v);
}
if (nows > tramontos && nows + delayUV < tramontos + durataTramontos) {
int v = 65 - (65 * (float)(((tramontos + durataTramontos) - nows) / (durataTramontos - delayUV)));
analogWrite(pinUV, v);
}
if (nows > tramontos && nows + delayW < tramontos + durataTramontos) {
int v = 65 - (65 * (float)(((tramontos + durataTramontos) - nows) / (durataTramontos - delayW)));
analogWrite(pinW, v);
}
}
[code]