Voy a usar el código de medina de este post https://todomarino.com/comunidad/sh...olador-de-D4n1&p=925442&viewfull=1#post925442 ya que no tengo ni idea de programar desde 0, solo he cambiado un par de cosas para que funcione en mi montaje, como la libreria de la pantalla, algunas funciones para que ande en la version 1 del ide y poco mas. El caso es que no funciona el dimeo, están siempre los leds al 100% aunque el porcentaje que aparece en la pantalla sea el correcto, el montaje doy por hecho que esta bien ya que al reiniciar arduino la pantalla se apaga y se enciende a si que supongo que sera algo del código. Uso arduino leonardo
Código:
// may reef vo.2//
#include <OneWire.h>
#include <LiquidCrystal_I2C.h>
#include <DallasTemperature.h>
#include <Wire.h> //
#define DS1307_I2C_ADDRESS 0x68
LiquidCrystal_I2C lcd(0x27,16,2);
OneWire oneWire(6);
DallasTemperature sensores(&oneWire);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
int Retraso=5000;
//////////
//VARIOS//
//////////
// Convierte números normales decimales a BCD (binario decimal codificado)
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convierte BCD (binario decimal codificado) a números normales decimales
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// Parar el DS1307, pero esto tiene el efecto secundario de segundos que se ponen a 0
// probablemente solo quiera usar esto para pruebas.
/*
void stopDs1307()
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(0x80);
Wire.endTransmission();
}
*/
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
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.write(0);
Wire.write(decToBcd(second)); // 0 a bit 7 inicia el reloj
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // Si usted quiere 12 hora am/pm usted tiene que poner el
// bit 6 (tambien tiene que cambiar readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(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.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// Alguno de estos necesitan enmascarar porque ciertos bits son bits de control
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
// set up the LCD's number of columns and rows:
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
lcd.begin(20,4);
sensores.begin();
// Cambie estos valores a los que usted quiere poner su reloj.
// Usted probablemente solo quiere poner su reloj una vez y luego quitar
// la llamada de setDateDs1307.
// si desea poner en hora, active esta parte y luego vuelva a quitarla
/*
second = 00;
minute = 32;
hour = 14;
dayOfWeek = 5;
dayOfMonth = 12;
month = 8;
year = 11;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
*/
}
void loop() {
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); //Obtenemos la hora
sensores.requestTemperatures();
Luces(minute, hour);
lcd.noBlink();
if (minute < 2) Retraso=1000;
else if (hour < 5) Retraso=10000;
else Retraso=3000;
}
void Luces(int minutos, int horas) {
int LucesAzules;
int InicioAmanecerAzul=660;//480 equivale a las 8 mañana /conversion seria 60x8=480 la misma tabla para los demas parametros //
int FinAmanecerAzul=780;//10:00 //
int InicioAnochecerAzul=1380;//23:00//
int FinAnochecerAzul=1440;//24:00//
int FinLuzLuna=120;//2:00//
int MaxAzul=250;
int PorcentAzules;
int LucesMixtas;
int InicioAmanecerMixtas=750;//10:30//
int FinAmanecerMixtas=810;//11:30//
int InicioAnochecerMixtas=1290;//21:30//
int FinAnochecerMixtas=1350;//22:30//
int MaxMixtas=250;
int PorcentMixtas;
int LucesBlancas;
int InicioAmanecerBlancas=870;//12:30//
int FinAmanecerBlancas=930;//13:30//
int InicioAnochecerBlancas=1140;//19:00//
int FinAnochecerBlancas=1200;//20:00//
int MaxBlancas=250;
int PorcentBlancas;
int Tiempo;
Tiempo=horas*60+minutos;
// if (Tiempo < 60)Tiempo=Tiempo+1440;
// Tiempo=Tiempo-60; //Horario de Invierno
//################################# AZULES ###################################
//Luna
if (Tiempo <= FinLuzLuna) LucesAzules=(FinAnochecerAzul-Tiempo);
//Noche
if (Tiempo < InicioAmanecerAzul && Tiempo > FinLuzLuna) LucesAzules=0;
//Amanecer
if (Tiempo >= InicioAmanecerAzul && Tiempo <= FinAmanecerAzul) LucesAzules=(Tiempo-InicioAmanecerAzul)*2;
//Dia
if ((Tiempo > FinAmanecerAzul) && (Tiempo < InicioAnochecerAzul)) LucesAzules=MaxAzul;
//Anochecer
if (Tiempo >= InicioAnochecerAzul && Tiempo <= FinAnochecerAzul) LucesAzules=(FinAnochecerAzul-Tiempo)*2;
//Control
if( LucesAzules <0) LucesAzules=0;
if( LucesAzules >250) LucesAzules=250;
//################################# MIXTAS ###################################
//Noche
if (Tiempo < InicioAmanecerMixtas) LucesMixtas=0;
//Amanecer
if (Tiempo >= InicioAmanecerMixtas && Tiempo <= FinAmanecerMixtas) LucesMixtas=(Tiempo-InicioAmanecerMixtas)*3;
//Dia
if (Tiempo > FinAmanecerMixtas && Tiempo < InicioAnochecerMixtas) LucesMixtas=MaxMixtas;
//Anochecer
if (Tiempo >= InicioAnochecerMixtas && Tiempo <= FinAnochecerMixtas) LucesMixtas=(FinAnochecerMixtas-Tiempo)*3;
//Control
if( LucesMixtas <0) LucesMixtas=0;
if( LucesMixtas >250) LucesMixtas=250;
//################################# BLANCAS ###################################
//Noche
if (Tiempo < InicioAmanecerBlancas) LucesBlancas=0;
//Amanecer
if (Tiempo >= InicioAmanecerBlancas && Tiempo <= FinAmanecerBlancas) LucesBlancas=(Tiempo-InicioAmanecerBlancas)*4;
//Dia
if (Tiempo > FinAmanecerBlancas && Tiempo < InicioAnochecerBlancas) LucesBlancas=MaxBlancas;
//Anochecer
if (Tiempo >= InicioAnochecerBlancas && Tiempo <= FinAnochecerBlancas) LucesBlancas=(FinAnochecerBlancas-Tiempo)*4;
//Control
if( LucesBlancas <0) LucesBlancas=0;
if( LucesBlancas >250) LucesBlancas=250;
analogWrite(8, 255-LucesAzules );
analogWrite(9, 255-LucesMixtas );
analogWrite(10, 255-LucesBlancas);
PorcentAzules =Porcent(LucesAzules ,255)*2;
PorcentMixtas =Porcent(LucesMixtas ,255)*2;
PorcentBlancas=Porcent(LucesBlancas,255)*2;
float T1(sensores.getTempCByIndex(0));
lcd.noBlink();
lcd.setCursor(2,0);
lcd.print("*REEF*");
lcd.setCursor(11,0);
lcd.print("LUCES:");
lcd.setCursor(0,1);
lcd.print("CLOK:");
if (hour < 10) lcd.print("0");
lcd.print(hour, DEC);
lcd.print(":");
if (minute < 10) lcd.print("0");
lcd.print(minute, DEC);
lcd.setCursor(10,1);
lcd.print(" AZUL:");
if (PorcentAzules < 10);
lcd.print(PorcentAzules);
lcd.print("% ");
lcd.setCursor(10,2);
lcd.print(" DUO:");
if (PorcentMixtas < 10);
lcd.print(PorcentMixtas);
lcd.print("% ");
lcd.setCursor(0,3);
lcd.print("TEM");
lcd.print(":");
lcd.print(T1);
lcd.print("C");
lcd.print(" ");
lcd.print(" ");
lcd.print("BLANCO:");
if (PorcentBlancas < 10) ;
lcd.print(PorcentBlancas);
lcd.print("% ");
if (LucesAzules+LucesMixtas+LucesBlancas > 1)
delay(Retraso);
}
int Porcent(int Valor, int Maximo)
{
int result;
result=Valor*50;
result=result/Maximo;
return result;
}