Go Down

Topic: time controlled High Power LED dimming (Read 5333 times) previous topic - next topic

Schlesa

Hi,

i am a newby and I have problems to realize a arduino 1.0 code for a aquarium LED Light.

I have 4 x 50W High Power LEDs (2x blue / 2x white) with 4x http://www.ebay.de/itm/130631160445?ssPageName=STRK:MEWAX:IT&_trksid=p3984.m1438.l2649#ht_600wt_1180 and KSQs
I have a Arduino Uno with a 2x16 LCD and a Mega and also the RTC clock module.

I have found some code example, but all of them do not function right.

Can everybody help me to realize this:

every LED should start to a different time and fade / dimm up to 75% within 2 hour.
After some hours every LED should fade / dimm down to 0% within 2 hour (also different time)

e.g.
Blue LED 1 start at 07:00 and will be on 75% at 09:00
Blue LED 2 start at 07:30 and will be on 75% at 09:30
White LED 3 start at 08:00 and will be on 75% at 10:00
White LED 4 start at 09:00 and will be on 75% at 11:00

White LED 4 fade / dimm down at 14:00 and will be off/0% at 16:00
White LED 3 fade / dimm down at 15:00 and will be off/0% at 17:00
Blue LED 2 fade / dimm down at 16:30 and will be off/0% at 18:30
Blue LED 1 fade / dimm down at 17:00 and will be off/0% at 19:00

Any Ideas for a code for Arduino 1.0 ?

johnwasser

Code: [Select]

const unsigned long HOUR = 60 * 60;
const unsigned long MINUTE = 60;
const int TARGET_BRIGHTNESS = (255 * 3) / 4;  // Target brightness is 3/4 of full

loop()
    {
    /////  Get time from RTC into RTCHour, RTCMinute, RTCSecond
    unsigned long time = RTCHour * HOUR + RTCMinute * MINUTE + RTCSecond;  // Time in seconds

    analogWrite(Blue1Pin    brightness(time, 7*HOUR,                 17*HOUR));
    analogWrite(Blue2Pin,   brightness(time, 7*HOUR+30*MINUTE, 16*HOUR+30*MINUTE));
    analogWrite(White3Pin, brightness(time, 8*HOUR,                 15*HOUR));
    analogWrite(White4Pin, brightness(time, 9*HOUR,                 14*HOUR));

   delay(1000);
    }

byte brightness(int Pin, unsigned long time, unsigned long fadeUpStart, unsigned long fadeDownStart)
    {
    //  Mid day, light is at maximum brightness
    if (time >= fadeUpStart + 2*HOUR  && time <= fadeDownStart)
        return TARGET_BRIGHTNESS;

   // Dawn:  fade up the light
    if (time >= fadeUpStart && time <= fadeUpStart + 2*HOUR)  // Fading up
           {
           unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
           return TARGET_BRIGHTNESS * seconds / (HOUR*2);  // Fade up based on portion of interval completed.
           }

   // Evening: Fade down the light
   if (time >= fadeDownStart && time <= fadeDownStart + 2*HOUR)  // Fading down
           {
           unsigned long seconds = (fadeDownStart + (HOUR*2)) - time;  // Number of seconds remaining in the fade time
           return TARGET_BRIGHTNESS * seconds / (HOUR*2);  // Fade down based on portion of interval left.
           }

    // The remaining times are night and the lights is off
    return 0;  // Shouldn't get here
    }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e


Schlesa

Hi again,

I have used the following code, but I can't compile, because:

Lichtsteuerung.cpp: In function 'void loop()':
Lichtsteuerung:33: error: 'RTC' was not declared in this scope
Lichtsteuerung:15: error: too few arguments to function 'byte brightness(int, long unsigned int, long unsigned int, long unsigned int)'
Lichtsteuerung:35: error: at this point in file
Lichtsteuerung:15: error: too few arguments to function 'byte brightness(int, long unsigned int, long unsigned int, long unsigned int)'
Lichtsteuerung:36: error: at this point in file
Lichtsteuerung:15: error: too few arguments to function 'byte brightness(int, long unsigned int, long unsigned int, long unsigned int)'
Lichtsteuerung:37: error: at this point in file
Lichtsteuerung:15: error: too few arguments to function 'byte brightness(int, long unsigned int, long unsigned int, long unsigned int)'
Lichtsteuerung:38: error: at this point in file

any Idea how I can declare RTC and why there are to few arguments?

I have try different libraries and codes for reading Time from the RTC module, no change ...

What I have wrong in this code or is there anything missing?

Code: [Select]
#include "Wire.h"
#include "RTClib.h"
#define DS1307_ADDRESS 0x68  // This is the I2C address

// Arduino version compatibility Pre-Compiler Directives
#if defined(ARDUINO) && ARDUINO >= 100   // Arduino v1.0 and newer
  #define I2C_WRITE Wire.write
  #define I2C_READ Wire.read
  #include "Arduino.h"
#else                                   // Arduino Prior to v1.0
  #define I2C_WRITE Wire.send
  #define I2C_READ Wire.receive
  #include "WProgram.h"
#endif


const int Blue1Pin = 3;
const int Blue2Pin = 5;
const int White3Pin = 6;
const int White4Pin = 9;
const unsigned long HOUR = 60 * 60;
const unsigned long MINUTE = 60;
const int TARGET_BRIGHTNESS = (255 * 3) / 4;  // Target brightness is 3/4 of full

void getDateDs1307()
    {
Wire.requestFrom(DS1307_ADDRESS, 7);
}
void loop()
    {
     
getDateDs1307();
    /////  Get time from RTC into RTCHour, RTCMinute, RTCSecond
    unsigned long time = RTC.Hour * HOUR + RTC.Minute * MINUTE + RTC.Second;  // Time in seconds

    analogWrite(Blue1Pin,    brightness(time, 7*HOUR,                 17*HOUR));
    analogWrite(Blue2Pin,   brightness(time, 7*HOUR+30*MINUTE, 16*HOUR+30*MINUTE));
    analogWrite(White3Pin, brightness(time, 8*HOUR,                 15*HOUR));
    analogWrite(White4Pin, brightness(time, 9*HOUR,                 14*HOUR));

   delay(1000);
    }

byte brightness(int Pin, unsigned long time, unsigned long fadeUpStart, unsigned long fadeDownStart)
    {
    //  Mid day, light is at maximum brightness
    if (time >= fadeUpStart + 2*HOUR  && time <= fadeDownStart)
        return TARGET_BRIGHTNESS;

   // Dawn:  fade up the light
    if (time >= fadeUpStart && time <= fadeUpStart + 2*HOUR)  // Fading up
           {
           unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
           return TARGET_BRIGHTNESS * seconds / (HOUR*2);  // Fade up based on portion of interval completed.
           }

   // Evening: Fade down the light
   if (time >= fadeDownStart && time <= fadeDownStart + 2*HOUR)  // Fading down
           {
           unsigned long seconds = (fadeDownStart + (HOUR*2)) - time;  // Number of seconds remaining in the fade time
           return TARGET_BRIGHTNESS * seconds / (HOUR*2);  // Fade down based on portion of interval left.
           }

    // The remaining times are night and the lights is off
    return 0;  // Shouldn't get here
    }

johnwasser

Code: [Select]
The "too few arguments" error was because I forgot to remove "int Pin," from the declaration of brightness().

The "RTC" error was because you added '.' in the variable names that were to hold hour, minute, and second.  I still don't see anything in the code that gets the current time from your RTC chip.  You request 7 bytes from a I2C device and never read them.  I didn't include code to read the RTC because you weren't asking about that.

Try this version:
{code]
#include "Wire.h"
#define DS1307_ADDRESS 0x68  // This is the I2C address

const int Blue1Pin = 3;
const int Blue2Pin = 5;
const int White3Pin = 6;
const int White4Pin = 9;
const unsigned long HOUR = 60 * 60;
const unsigned long MINUTE = 60;
const int TARGET_BRIGHTNESS = (255 * 3) / 4;  // Target brightness is 3/4 of full

void setup()
{
  Wire.begin();
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void loop()
{
  /////  Get time from RTC into RTCHour, RTCMinute, RTCSecond

   // Set the register pointer to 0 (Second)
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write((byte)0);
  Wire.endTransmission();

  //  Read Second, Minute, and Hour
  Wire.requestFrom(DS1307_ADDRESS, 3);

  int RTCSecond = bcdToDec(Wire.read());
  int RTCMinute = bcdToDec(Wire.read());
  int RTCHour = bcdToDec(Wire.read() & 0b111111); //24 hour time
 

  unsigned long time = RTCHour * HOUR + RTCMinute * MINUTE + RTCSecond;  // Time in seconds

  analogWrite(Blue1Pin,   brightness(time, 7*HOUR,                 17*HOUR));
  analogWrite(Blue2Pin,   brightness(time, 7*HOUR+30*MINUTE, 16*HOUR+30*MINUTE));
  analogWrite(White3Pin, brightness(time, 8*HOUR,                 15*HOUR));
  analogWrite(White4Pin, brightness(time, 9*HOUR,                 14*HOUR));

  delay(1000);
}

byte brightness(unsigned long time, unsigned long fadeUpStart, unsigned long fadeDownStart)
{
  //  Mid day, light is at maximum brightness
  if (time >= fadeUpStart + 2*HOUR  && time <= fadeDownStart)
    return TARGET_BRIGHTNESS;

  // Dawn:  fade up the light
  if (time >= fadeUpStart && time <= fadeUpStart + 2*HOUR)  // Fading up
  {
    unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
    return TARGET_BRIGHTNESS * seconds / (HOUR*2);  // Fade up based on portion of interval completed.
  }

  // Evening: Fade down the light
  if (time >= fadeDownStart && time <= fadeDownStart + 2*HOUR)  // Fading down
  {
    unsigned long seconds = (fadeDownStart + (HOUR*2)) - time;  // Number of seconds remaining in the fade time
    return TARGET_BRIGHTNESS * seconds / (HOUR*2);  // Fade down based on portion of interval left.
  }

  // The remaining times are night and the lights is off
  return 0;  // Shouldn't get here
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Schlesa

wow, 2nd time very fast response.
Thank you! Also for the comments, so I think I have understand the functions & Problems :)

I have try the new Version and compiling without failures.

Thx, I will upload this in the afternoon and will see what's happens  :D

Thank you very much for your help and the code!

Schlesa

#6
Mar 05, 2012, 07:52 pm Last Edit: Mar 05, 2012, 08:03 pm by Schlesa Reason: 1
I have tested it now with the Arduino Uno and 4 small Low Power LEDs for testing and it is function like I need.
For Test, I have set the time frame and dimming time to some minutes and so I have seen the function, I have ask for.

Thank you for your Help and effort for a newbie  :D .

If I want to output the actual status of brightness to an LCD, I must implement such kind of code for one LED for example?:

Code: [Select]

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {

 lcd.begin(16, 2);

}

void loop() {
 
 lcd.setCursor(0, 1);
 lcd.write(Blue1Pin, brightness());  // can I use this or wrong variable / syntax?
 lcd.print(Blue1Pin, brightness());  // or ist that the right comand?
}


or have I imagined to easy  :smiley-red:

I am not sure if I read the right variable for the actual status of the LED brigtness ...

johnwasser

You need a separate print() for each value:
Code: [Select]

void loop() {
 lcd.setCursor(0, 1);
 lcd.print("Blue1Pin: ");  // can I use this or wrong variable / syntax?
 lcd.print(brightness());  // or ist that the right comand?
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Schlesa

#8
Mar 18, 2012, 08:03 pm Last Edit: Mar 18, 2012, 08:45 pm by Schlesa Reason: 1
Hi again,

I have test and try and now the code is complete.
Here if anybody want to know:

Code: [Select]

#include <Wire.h>
#include <LiquidCrystal.h>
#define DS1307_ADDRESS 0x68  // This is the I2C address

// SainSmart LCD 16x2
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

// Arduino Mega 2560
const int Blue1Pin = 2;
const int Blue2Pin = 3;
const int White3Pin = 11;
const int White4Pin = 12;
const int Night1Pin = 13;
const unsigned long HOUR = 60 * 60;
const unsigned long MINUTE = 60;
const int TARGET_BRIGHTNESS = (255 * 3) / 4;  // Target brightness is 3/4 of full

void setup()
{  
 lcd.begin(16, 2);
   
 Wire.begin();
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
 return ( (val/16*10) + (val%16) );
}

void loop()
{
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("***");
 lcd.setCursor(4,0);

 /////  Get time from RTC into RTCHour, RTCMinute, RTCSecond

  // Set the register pointer to 0 (Second)
 Wire.beginTransmission(DS1307_ADDRESS);
 Wire.write((byte)0);
 Wire.endTransmission();

 //  Read Second, Minute, and Hour
 Wire.requestFrom(DS1307_ADDRESS, 3);

 int RTCSecond = bcdToDec(Wire.read());
 int RTCMinute = bcdToDec(Wire.read());
 int RTCHour = bcdToDec(Wire.read() & 0b111111); //24 hour time

// write Time to LCD XX:XX:XX
 if(RTCHour < 10) {
   lcd.print("0");
 }
lcd.print(RTCHour);
lcd.print(":");
 if(RTCMinute < 10) {
   lcd.print("0");
 }
lcd.print(RTCMinute);
lcd.print(":");
 if(RTCSecond < 10) {
   lcd.print("0");
 }
lcd.print(RTCSecond);
lcd.setCursor(13,0);
lcd.print("***");
 unsigned long time = RTCHour * HOUR + RTCMinute * MINUTE + RTCSecond;  // Time in seconds
 
// write value within the timeframe to PWM
 analogWrite(Blue1Pin,   brightness(time, 7*HOUR,                 19*HOUR));
 analogWrite(Blue2Pin,   brightness(time, 7*HOUR+30*MINUTE,                 19*HOUR+30*MINUTE));
 analogWrite(White3Pin, brightness(time, 8*HOUR+30*MINUTE,                 16*HOUR+30*MINUTE));
 analogWrite(White4Pin, brightness(time, 9*HOUR+30*MINUTE,                 17*HOUR+30*MINUTE));
 analogWrite(Night1Pin, TARGET_BRIGHTNESS);

// write actual value in % to LCD (max. 255 = 100%)
lcd.setCursor(0,1); //White3Pin
if(((brightness(time,  8*HOUR+30*MINUTE,                 16*HOUR+30*MINUTE))*100/255) < 10) {
   lcd.print("0");
 }
lcd.print((brightness(time,  8*HOUR+30*MINUTE,                 16*HOUR+30*MINUTE))*100/255);
lcd.print("%");

lcd.setCursor(4,1); //Blue1Pin
if(((brightness(time, 7*HOUR,                 19*HOUR))*100/255) < 10) {
   lcd.print("0");
 }
lcd.print((brightness(time, 7*HOUR,                 19*HOUR))*100/255);
lcd.print("%");

lcd.setCursor(8,1); //Blue2Pin
if(((brightness(time, 7*HOUR+30*MINUTE,                 19*HOUR+30*MINUTE))*100/255) < 10) {
   lcd.print("0");
 }
lcd.print((brightness(time, 7*HOUR+30*MINUTE,                 19*HOUR+30*MINUTE))*100/255);
lcd.print("%");

lcd.setCursor(12,1); //White4Pin
if(((brightness(time, 9*HOUR+30*MINUTE,                 17*HOUR+30*MINUTE))*100/255) < 10) {
   lcd.print("0");
 }
lcd.print((brightness(time, 9*HOUR+30*MINUTE,                 17*HOUR+30*MINUTE))*100/255);
lcd.print("%");

 delay(1000);
}

byte brightness(unsigned long time, unsigned long fadeUpStart, unsigned long fadeDownStart)
{
 //  Mid day, light is at maximum brightness
 if (time >= fadeUpStart + HOUR  && time <= fadeDownStart)
   return TARGET_BRIGHTNESS;

 // Dawn:  fade up the light
 if (time >= fadeUpStart && time <= fadeUpStart + HOUR)  // Fading up
 {
   unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
   return TARGET_BRIGHTNESS * seconds / (HOUR);  // Fade up based on portion of interval completed.
 }

 // Evening: Fade down the light
 if (time >= fadeDownStart && time <= fadeDownStart + HOUR)  // Fading down
 {
   unsigned long seconds = (fadeDownStart + (HOUR)) - time;  // Number of seconds remaining in the fade time
   return TARGET_BRIGHTNESS * seconds / (HOUR);  // Fade down based on portion of interval left.
 }

 // The remaining times are night and the lights is off
 return 0;  // Shouldn't get here
}


Please feel free to write me, if I can something change ...

Again, thank you "johnwasser" for your help with the main code!

Knippi

Hello,
I have read this post and it's almost exactly what I was looking for a long time.
I'm from Germany and unfortunately my English is really bad. So I write it with a translation program. I would like to display the light levels (brightness) in (%) on my display. But I have another display, as will be controlled in the sketch. I've rewritten the first sketch, so I light levels (brightness) in (%) in the serial monitor displays and get it working.
Unfortunately I can not get the commands in exchange Serial.print lcd.print and everything works.
I have the following graphic display http://tronixstuff.wordpress.com/2011/03/12/the-dfrobot-lcd4884-lcd-shield/ and I'm glad that I get message with a RTC (DS1307) the date and time .
Looks like this:
Code: [Select]
#include <Wire.h>
#include <RTClib.h>

#include "LCD4884.h"y


RTC_DS1307 RTC;
DateTime now;

const char LOGFORMAT[] = "%02u.%02u.%04u     %02u:%02u:%02u";

void setup(){
//Serial.begin(9600);
    Wire.begin();
    RTC.begin();
lcd.LCD_init();
    lcd.LCD_clear();
    lcd.backlight(ON);

}

void loop()  {
    lcd.LCD_write_string(0, 0,"LED-Steuerung", MENU_HIGHLIGHT);
    lcd.LCD_write_string(8, 2,"c by Knippi", MENU_NORMAL);
    now=RTC.now();
   
  static char dataString[0];
    sprintf(dataString, LOGFORMAT, now.day(), now.month(), now.year(),now.hour(),now.minute(),now.second() );
       
   lcd.LCD_write_string(10, 4,dataString, MENU_NORMAL);


}


I've tried a lot and I think it must be in this part of the sketch to be installed, but how and what values???
Code: [Select]
static char dataString[0];
    sprintf(dataString, LOGFORMAT, now.day(), now.month(), now.year(),now.hour(),now.minute(),now.second() );
       
   lcd.LCD_write_string(10, 4,dataString, MENU_NORMAL);


I hope it has an idea.
Thank you ever.
Jens

kenlai32

I know its an old post.

But just wondering why does the screen flicker when using these code?


johnwasser


just wondering why does the screen flicker when using these code?


lcd.clear() at the top of loop(). 

The .clear() should only be called when you are changing ALL or ALMOST ALL of the text on the screen.  For minor changes, overwrite the parts of the screen that have actually changed.

Remove the call to .clear() and if unwanted characters are left on the screen, print blanks over them.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

davie87

I am using the below code but i am not having lights turn on and off them self.

blue = 5

white = 6

i have been trying to change the times to make the lights dim down/up or turn on/off however the lights can at times just appear semi on maybe 20% brightness.

As i being really daft and missing something out?

I believe i have an rtc and believe it is running at the right time (not sure how to prove this)

below is the link to the add on card that has been made for the arduino (not by me~)
http://forum.arduino.cc/index.php?topic=189740.new;topicseen

Code: [Select]

#include "Wire.h"
#define DS1307_ADDRESS 0x68  // This is the I2C address

const int BluePin = 6;

const int WhitePin = 5;

const unsigned long HOUR = 60 * 60;
const unsigned long MINUTE = 60;
const int TARGET_BRIGHTNESS = (255 * 3) / 4;  // Target brightness is 3/4 of full

void setup()
{
 Wire.begin();
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
 return ( (val/16*10) + (val%16) );
}

void loop()
{
 /////  Get time from RTC into RTCHour, RTCMinute, RTCSecond

  // Set the register pointer to 0 (Second)
 Wire.beginTransmission(DS1307_ADDRESS);
 Wire.write((byte)0);
 Wire.endTransmission();

 //  Read Second, Minute, and Hour
 Wire.requestFrom(DS1307_ADDRESS, 3);

 int RTCSecond = bcdToDec(Wire.read());
 int RTCMinute = bcdToDec(Wire.read());
 int RTCHour = bcdToDec(Wire.read() & 0b111111); //24 hour time
 

 unsigned long time = RTCHour * HOUR + RTCMinute * MINUTE + RTCSecond;  // Time in seconds

 
 analogWrite(BluePin,   brightness(time, 19*HOUR+14*MINUTE, 1*HOUR ));
 analogWrite(WhitePin, brightness(time, 20*HOUR,                 21*HOUR));


 delay(1000);
}

byte brightness(unsigned long time, unsigned long fadeUpStart, unsigned long fadeDownStart)
{
 //  Mid day, light is at maximum brightness
 if (time >= fadeUpStart + 2*HOUR  && time <= fadeDownStart)
   return TARGET_BRIGHTNESS;

 // Dawn:  fade up the light
 if (time >= fadeUpStart && time <= fadeUpStart + 2*HOUR)  // Fading up
 {
   unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time
   return TARGET_BRIGHTNESS * seconds / (HOUR*2);  // Fade up based on portion of interval completed.
 }

 // Evening: Fade down the light
 if (time >= fadeDownStart && time <= fadeDownStart + 2*HOUR)  // Fading down
 {
   unsigned long seconds = (fadeDownStart + (HOUR*2)) - time;  // Number of seconds remaining in the fade time
   return TARGET_BRIGHTNESS * seconds / (HOUR*2);  // Fade down based on portion of interval left.
 }

 // The remaining times are night and the lights is off
 return 0;  // Shouldn't get here
}

Damiher

hello! their contribution served me so much!
Thanks for exposing your idea, I was doing some changes I needed in my opinion and I raised a doubt as to the operation, because the program only runs if connected to PC USB, I never happened ... I'm locked, and do not know what could be.

Code: [Select]

#include "Wire.h"
#define DS1307_ADDRESS 0x68  // Esta es la dirección I2C

const int Blue1Pin = 3;
const int Blue2Pin = 5;
const int White3Pin = 6;
const int White4Pin = 9;
const unsigned long HOUR = 60 * 60;
const unsigned long MINUTE = 60;
const int ROJO = (255 * 0.2);  // variacion brillo final, 1/5 de 255
const int AZUL = (255 * 1);  // variacion brillo final, sin variacion 255
const int VIOLETA = (255 * 0.9);  // variacion brillo final, 9/10 de 255
const int BLANCO = (255 * 0.75);  // variacion brillo final, 1/2 de 255


void setup()
{
 Wire.begin();
}

byte bcdToDec(byte val)  {
// Convertir decimal codificado en binario de números decimales normales
 return ( (val/16*10) + (val%16) );
}

void loop()
{
 /////  Obtener el tiempo de RTC en RTCHour, RTCMinute, RTCSecond

  // Establecer el puntero del registro a 0 (Segundos)
 Wire.beginTransmission(DS1307_ADDRESS);
 Wire.write((byte)0);
 Wire.endTransmission();

 //  Read Second, Minute, and Hour
 Wire.requestFrom(DS1307_ADDRESS, 3);

 int RTCSecond = bcdToDec(Wire.read());
 int RTCMinute = bcdToDec(Wire.read());
 int RTCHour = bcdToDec(Wire.read() & 0b111111); //Tiempo en 24 horas
 

 unsigned long time = RTCHour * HOUR + RTCMinute * MINUTE + RTCSecond;  // Tiempo en segundos

 analogWrite(Blue1Pin,   brightness(time, 14*HOUR+30*MINUTE,      23*HOUR,ROJO));
 analogWrite(Blue2Pin,   brightness(time, 14*HOUR+30*MINUTE,      23*HOUR,AZUL));
 analogWrite(White3Pin, brightness(time, 14*HOUR,                 23*HOUR+30*MINUTE,VIOLETA));
 analogWrite(White4Pin, brightness(time, 15*HOUR,                 22*HOUR+30*MINUTE,BLANCO));

 delay(1000 );
}

byte brightness(unsigned long time, unsigned long fadeUpStart, unsigned long fadeDownStart, int BRILLO)
{
 //  Medio Dia, la luz esta al brillo máximo
 if (time >= fadeUpStart + 0.5*HOUR  && time <= fadeDownStart)
   return BRILLO;

 // Amanecer:  fade up the light
 if (time >= fadeUpStart && time <= fadeUpStart + 0.5*HOUR)  // Fading up
 {
   unsigned long seconds = time - fadeUpStart;  // Número de segundos en el tiempo de fade
   return BRILLO * seconds / (HOUR*0.5);  // Fade up basado en parte de intervalo finalizado
 }

 // Tarde: Fade down the light
 if (time >= fadeDownStart && time <= fadeDownStart + 0.5*HOUR)  // Fading down
 {
   unsigned long seconds = (fadeDownStart + (HOUR*0.5)) - time;  // Número de segundos restantes en el tiempo de fade
   return BRILLO * seconds / (HOUR*0.5);  // Fade down basado en parte de intervalo restante.
 }

 // Los tiempos restantes son la noche y las luces están apagados
 return 0;  // No debe llegar hasta aquí
 }

Go Up