Loading...
Pages: [1]   Go Down
Author Topic: time controlled High Power LED dimming  (Read 1502 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 98
Posts: 6386
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
    }
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you! This would me help smiley-grin
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
    }
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 98
Posts: 6386
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I have try the new Version and compiling without failures.

Thx, I will upload this in the afternoon and will see what's happens  smiley-grin

Thank you very much for your help and the code!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-grin .

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:
#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 ...
« Last Edit: March 05, 2012, 02:03:07 pm by Schlesa » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 98
Posts: 6386
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need a separate print() for each value:
Code:
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?
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi again,

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

Code:
#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!
« Last Edit: March 18, 2012, 02:45:59 pm by Schlesa » Logged

Kropp, Germany
Offline Offline
Newbie
*
Karma: 0
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
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
Logged

Pages: [1]   Go Up
Print
 
Jump to: