Time controlled High Power LED

Hi!

I just found a code to controll my LED with the Arduino. I changed and added things for my own needs. The editor wanted it to fade from 0 - 255 within two hours, stay at full brightness over the day and fade it down to zero at the evening. What I am not understandig is, where the hell is written down, that the fading process takes two hours? I wanted to test, if it works within, let's say 10 minutes. Don't want to wait all day long, just to see the dimming process :wink:

Another thing is, that I don't really understand what this line does:

lcd.print("R: ");
    if(((brightness(time,     8*HOUR+30*MINUTE, 21*HOUR+30*MINUTE))*100/255) < 10) {
    lcd.print("0");
  }
lcd.print((brightness(time,   8*HOUR+30*MINUTE, 21*HOUR+30*MINUTE))*100/255);
lcd.print("% ");

If time, 8HOUR+30MINUTE, 21HOUR+30MINUTE))*100/255 is smaller then 10 (which it is even if I take 24h!), LCD shall print Zero?

You may have an idea for me. My Code is the following:

#define Tmin 20
#define Tmax 55
#include <Wire.h>
#include <LiquidCrystal.h>

#include <PWM.h>
int32_t frequency = 25000;

#define DS1307_ADDRESS 0x68  // This is the I2C address
LiquidCrystal lcd(12, 11, 6, 5, 4, 3);

const int whitePin = 10;
const int bluePin = 2;
const int greenPin = 7;
const int redPin = 8;
const int uvPin = 1;
const unsigned long HOUR = 60 * 60;
const unsigned long MINUTE = 60;
const int TARGET_BRIGHTNESS = 255;

int i, temp1;
int fan = 9;
int a, value;


void setup()
{  
          pinMode(fan, OUTPUT);
          pinMode(7, INPUT);
  lcd.begin(20, 4);
  
  Wire.begin();
}

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



void loop()
{
  InitTimersSafe();
bool succes = SetPinFrequencySafe(9, frequency);
  
temp1 = ((5.0*analogRead(A3)*100.0)/1024);
fan = map(temp1, Tmin, Tmax, 0, 255);    // Tmin->0% // Tmax->100%
  if (fan<25)   fan = 0;
  if (fan>255) fan = 255;

    analogWrite(9, fan);               // PWM Geschwindigkeit setzen



  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); //****************************************** 24h Zeit

//******************************************************************************************** Zeit auf 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("Uhr");
  unsigned long time = RTCHour * HOUR + RTCMinute * MINUTE + RTCSecond;  // Time in seconds
  
// write value within the timeframe to PWM 
  analogWrite(whitePin,   brightness(time, 10*HOUR,          20*HOUR));
  analogWrite(bluePin,    brightness(time, 8*HOUR,           22*HOUR));
  analogWrite(greenPin,   brightness(time, 8*HOUR+30*MINUTE, 21*HOUR+30*MINUTE));
  analogWrite(redPin,     brightness(time, 8*HOUR+30*MINUTE, 21*HOUR+30*MINUTE));
  analogWrite(uvPin,      brightness(time, 8*HOUR,           22*HOUR));


//********************************************************************************************************************************Weiss

lcd.setCursor(0,1); //greenPin
      lcd.print("W: ");
    if(((brightness(time,     10*HOUR, 20*HOUR))*100/255) < 10) {
      lcd.print("0");
  }
lcd.print((brightness(time,   10*HOUR, 20*HOUR))*100/255);
lcd.print("% ");

//********************************************************************************************************************************  Blau
lcd.print("B: ");
    if(((brightness(time,     8*HOUR, 22*HOUR))*100/255) < 10) {
    lcd.print("0");
  }
lcd.print((brightness(time,   8*HOUR, 22*HOUR))*100/255);
lcd.print("% ");
//********************************************************************************************************************************  Grün
lcd.setCursor(0,2); 
lcd.print("G: ");
    if(((brightness(time,     8*HOUR+30*MINUTE, 21*HOUR+30*MINUTE))*100/255) < 10) {
    lcd.print("0");
  }
lcd.print((brightness(time,   8*HOUR+30*MINUTE, 21*HOUR+30*MINUTE))*100/255);
lcd.print("% ");
//********************************************************************************************************************************  UV
lcd.print("R: ");
    if(((brightness(time,     8*HOUR+30*MINUTE, 21*HOUR+30*MINUTE))*100/255) < 10) {
    lcd.print("0");
  }
lcd.print((brightness(time,   8*HOUR+30*MINUTE, 21*HOUR+30*MINUTE))*100/255);
lcd.print("% ");
//********************************************************************************************************************************  Rot
/*erial.println("U: ");
    if(((brightness(time,         8*HOUR, 22*HOUR))*100/255) < 10) {
    Serial.println("0");
  }
Serial.println((brightness(time,   8*HOUR, 22*HOUR))*100/255);
Serial.println("%");
*/
//********************************************************************************************************************************

    lcd.setCursor (0,3);
    lcd.print("T: ");
    lcd.print(temp1);
    lcd.print('\xDF');
    lcd.print("C ");
    lcd.setCursor(9,3);
    lcd.print("Fan: ");

    lcd.print(fan/2.55);
    lcd.print("%  ");

}

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
}

Greetings

Here:

  analogWrite(whitePin,   brightness(time, 10*HOUR,          20*HOUR));
  analogWrite(bluePin,    brightness(time, 8*HOUR,           22*HOUR));
  analogWrite(greenPin,   brightness(time, 8*HOUR+30*MINUTE, 21*HOUR+30*MINUTE));
  analogWrite(redPin,     brightness(time, 8*HOUR+30*MINUTE, 21*HOUR+30*MINUTE));
  analogWrite(uvPin,      brightness(time, 8*HOUR,           22*HOUR));

Looks like the whitePin goes on 2 hours after the bluePin, and goes off 2 hours before the bluePin, for example.

It does not, because I changed the sketch. The Guy who coded it, started at 8:30 and 9:30 I think... so it's not the distance between those two colours. I can't find the sketch in this forum, but I'll look for it :slight_smile:

Kind regards

Jan