in questi giorni mi sto cimentando a creare (con arduino :)) un orologio che sia in grado di calcolare gli orari di alba e tramonto, così da far accendere e spegnere delle luci in automatico.
Il codice per adesso è in grado di far dialogare arduino con un RTC esterno (il classico DS1307) e tramite una funzione che contiene qualche formula di conversione di calcolare gli orari di alba e tramonto, non essendo un esperto di astronomia, non sono certo dell'esattezza del procedimento ma, sta di fatto che con delle simulazioni i risultati ottenuti sono piuttosto soddisfacenti (manca però il calcolo dei crepuscoli).
Prima di passare alle fasi di introduzione delle condizioni che gestiranno un relè, sto cercando di verificare l'affidabilità del chip RTC e del codice che ho scritto fino a questo punto.
Ed ecco il codice:
prima parte:
#include <Math.h>
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h> // written by mattt on the Arduino forum and modified by D. Sjunnessonint command = 0;
int prevDay = 0;
int actDay = 0;float deg_rad = 57.2958; // conversione gradi radianti
float omega = 0.9863; //(= 360/365) [gradi/giorno] -> gradi per ogni giro / giorni_anno
double mezzo_di_medio = 12.1667; //corrisponde alle 12h10m -> 50m indietro rispetto a Greenwich
float lat = 45.47;
double alba, tramonto;int alba_tramonto_HM[4];
void setup()
{Serial.begin(57600);
omega = omega/deg_rad; //few conversionfor(int i=0; i<5; i++)
alba_tramonto_HM* = 0;*
}
void loop()
{
_ if(Serial.available()){_
_ command = Serial.read();_
- if (command == 84) { //If command = "T" Set Date*
- setRTC();*
- }*
- }*
- actDay = RTC.get(DS1307_DATE,false);*
- if( actDay != prevDay ){*
- calcAlba_Tramonto( mezzo_di_medio );*
- prevDay = actDay;*
- }*
Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
_ Serial.print(":");_
Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
_ Serial.print(":");_
Serial.print(RTC.get(DS1307_SEC,false));//read seconds
_ Serial.print(" "); // some space for a more happy life_
Serial.print(RTC.get(DS1307_DATE,false));//read date
_ Serial.print("/");_
Serial.print(RTC.get(DS1307_MTH,false));//read month
_ Serial.print("/");_
Serial.print(RTC.get(DS1307_YR,false)); //read year
_ Serial.println();_
Serial.println(calcDOY(RTC.get(DS1307_DATE,false), RTC.get(DS1307_MTH,false), RTC.get(DS1307_YR,false)));
_ Serial.print("alba: ");_
Serial.print(alba_tramonto_HM[0], DEC);
_ Serial.print("h ");_
Serial.print(alba_tramonto_HM[1], DEC);
_ Serial.println("m ");_
_ Serial.print("tramonto: ");_
Serial.print( alba_tramonto_HM[2], DEC);
_ Serial.print("h ");_
Serial.print( alba_tramonto_HM[3], DEC);
_ Serial.println("m ");_- double ora_dec = conv_sessagimali( RTC.get(DS1307_HR,true), RTC.get(DS1307_MIN,true) );*
- delay(1000);*
}
void setRTC()
{
_ byte second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result. _
_ byte minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));_
_ byte hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));_
_ byte dayOfWeek = (byte) (Serial.read() - 48);_
_ byte dayOfMonth = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));_
_ byte month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));_
_ byte year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));_- RTC.stop();*
- RTC.set(DS1307_SEC,second); //set the seconds*
- RTC.set(DS1307_MIN,minute); //set the minutes*
- RTC.set(DS1307_HR,hour); //set the hours*
- RTC.set(DS1307_DOW,dayOfWeek); //set the day of the week*
- RTC.set(DS1307_DATE,dayOfMonth); //set the date*
- RTC.set(DS1307_MTH,month); //set the month*
- RTC.set(DS1307_YR,year); //set the year*
- RTC.start();*
}
[/quote]
La data nell'RTC viene impostata tramite seriale inviando il comando "T(sec)(min)(ore)(giorno della settimana)(giorno)(mese)(anno)"
Per prima cosa non so se l'inizializzazione dell'RTC è fatta nel modo ottimale, perchè succede che a volte non riesco più a settare l'ora, riesco solo a farlo se faccio l'upload di uno sketch con l'impostazione contenuta nel Setup.