#include <OneWire.h>
#include <Wire.h>
#include "RTClib.h" // Incluye la librería RTClib
#define DS1307_I2C_ADDRESS 0x68
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
boolean HoraRetrasa;
void setup()
{
Serial.begin(9600);
Wire.begin();
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
pinMode(9, OUTPUT); // ajudicamos esa salida a la serie (tip 141)
/* Solo queremos poner en hora el reloj una vez y luego quitar la llamada de setDateDs1307. Activar esta parte y luego quitarla */
/*
second = 00;
minute = 50;
hour = 17;
dayOfWeek = 5;
dayOfMonth = 28;
month = 3;
year = 14;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
*/
}
void loop()
{
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); //Obtenemos la hora
Pot(minute, hour); // declaramos fase,tiempos , hora y minutos para la fase
if (minute != minutoMemory) //Si es un nuevo minuto comprobamos cambio de hora
{
ActualizaMinuto();
}
Luces(minute, hour);
}
void ActualizaMinuto() //Cambiar hora verano-invierno
{
//Adelanta la hora a partir del dia 25 de Marzo, busca el primer domingo
//y cuando se han las 2 de la noche adelanta el reloj una hora
if (month==3)
{
if (dayOfMonth >= 26)
{
if (dayOfWeek = 7)
{
if (hour==2)
{
hour = 3;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}
}
}
}
//Retrasa la hora a partir del dia 25 de Octubre, busca el primer domingo
//y cuando se han las 2 de la noche retrasa el reloj una hora
if (month==10)
{
if (dayOfMonth >= 26)
{
if (dayOfWeek = 7)
{
if ((hour==2)&&(HoraRetrasa==false))
{
hour = 1;
HoraRetrasa=true;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}
}
}
}
if (hour==3)
{
HoraRetrasa=false;
}
minutoMemory=minute;
}
void Luces(int minutos, int horas)
{
///////////tiempos de progamacion//////////
int PotLED;
int InicioAmanecerLED=720; //12:00
int FinAmanecerLED=780; //13:00 //
int InicioAnochecerLED=1320; //22:00//
int FinAnochecerLED=1380; //23:00//
int MaxLED=125; //Maximo que vamos a encender los LED sobre 255
int Tiempo;
Tiempo=horas*60+minutos;
if (Tiempo < 60)
{
Tiempo=Tiempo+1440;
}
//Noche
if (Tiempo < InicioAmanecerLED )
{
PotLED=0;
Serial.println(PotLED);
}
//Amanecer
if (Tiempo >= InicioAmanecerLED && Tiempo <= FinAmanecerLED)
{
PotLED=map(Tiempo,InicioAmanecerLED,FinAmanecerLED,0,MaxLED);
Serial.println(PotLED);
}
//Dia
if ((Tiempo > FinAmanecerLED) && (Tiempo < InicioAnochecerLED))
{
PotLED=MaxLED;
Serial.println(PotLED);
}
//Anochecer
if (Tiempo >= InicioAnochecerLED && Tiempo <= FinAnochecerLED)
{
PotLED=map(Tiempo,InicioAnochecerLed,FinAnochecerLED,MaxLED,0);
Serial.println(PotLED);
}
//Control
if(PotLED < 0)
{
PotLED=0;
}
if(PotLED >MaxLED)
{
PotLED=MaxLED;
}
analogWrite(9, PotLED);
delay(1000);
}
byte bcdToDec(byte val) // Convierte BCD (binario decimal codificado) a números normales decimales
{
return ( (val/16*10) + (val%16) );
}
void setDateDs1307 (byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(decToBcd(second)); // 0 a bit 7 inicia el reloj
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour));
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}
// Establece la fecha y el tiempo del ds1307
void getDateDs1307 (byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Resetea el registro puntero
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// Alguno de estos necesitan enmascarar porque ciertos bits son bits de control
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f);
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}