LED light timed cycle problems

I am making a project to have a set of LEDs fade in over a period of 6 hours, and then to fade back out again during a period of 6 hours. with a 12 hour period where they are off, then the cycle starts again.

I messed with the fading led sketch and got a bit of help from a programmer or 2 that i know to get the thing refined and to get the correct delay setting to make it work over the time period i desired.

for those who want to know its a prototype for a LED grow light setup i am making to give my plants a bit of extra light during the dark months over winter (i live in Norway in the polar circle, and we get 2 months of dark with no sun at all),so using some ultrabright LEDs i hope to help them out until the spring.

here is the code i have at present (running on 3 chip setups: one a lillipad bootloader on internal clock, one with dec bootloader on a 16 MHz crystal on a breadboard, and one with a 16 MHz osc on a Dcore board {thnx John Ryan})

// Daytime LED 


int value = 0;                            // variable to keep the actual value 
int ledpin1 = 11;                           // light connected to digital PWM pins 3 - 11
int ledpin2 = 10;
int ledpin3 = 9;
int ledpin4 = 6;
int ledpin5 = 5;
int ledpin6 = 3;

void setup() 
{ 
  // nothing for setup 
} 

#define set_pins(value) analogWrite(ledpin1, value);analogWrite(ledpin2, value);analogWrite(ledpin3, value);analogWrite(ledpin4, value); analogWrite(ledpin5, value); analogWrite(ledpin6, value); delay(84705);

void loop()
{
        for(;;) {
                for(value = 0 ; value <= 255; value+=1) { // fade in (from min to max in 255 steps)
                        set_pins(value);
                }
                for(value = 255; value >=0; value-=1) { // fade out (from max to min in 255 steps)
                        set_pins(value);
                }
                for(value = 0 ; value <= 255; value+=1) {
                        set_pins(0);
                }
                for(value = 255; value >=0; value-=1)   { // fade out (from max to min in 255 steps)
                        set_pins(0);
                }
        }
}

am currently running this code (cleaned up by a friend from the old setup i hashed up from the fading led sketch) on all 3 to see if the problem is hardware related.

You see the problem is that at some point during the cycle the LEDs fail to dim, or are in the dimming part of the cycle and go to full brightness, or are in the fading in part and just speed up to full, and then stay like that.

when i ran the prog with a 50 ms delay it ran fine for well over the time i was running the realtime one. no problems for what i could see, it faded in to full, faded out and was off for the same amount of time.

the LEDs are ultrabright ones of 3.2V or 3.5V (blue and white, i would like to have red but don't have any), not sure what resistors to use so any suggestion would be good.
And they are using all the default pwm pins (3,5,6,9,10,11)

i hope this is enough info and would really appreciate any input from those of u who know more than i do :smiley: about programming and the hardware.

thanx

Bng. :wink:

If the problem occurs every 9 ½ hours or so it may be due to problems with the delay function on versions 0011 and earlier. Try it with arduino version 0012 that has just been released.

You can read more about this issue searching on keywords millis() rollover

i was using 0012

i dont think i used 0011 either wen i uploaded the 1st version of code :frowning:

not sure whats going on really im wondering if its hardware realted some kind of feedback on the pwm pins messing it up maybe? i dunno :S

Perhaps test it with a variant the just does digital writes to turn the LEDs on and off. If that works ok then it's a pwm problem.

#define set_pins(value) digitalWrite(ledpin1, value);digitalWrite(ledpin2, value) . . .

for(value = 0 ; value <= 255; value+=1) {
set_pins(value > 127); // sets 0 for 0-127, 1 for 127-255

Bong--

I'm not sure whether this could be the cause of your erratic behavior, but I note that you did not configure the led pins as OUTPUT pins:

pinMode(ledpin1, OUTPUT);

(They default to INPUT according to the documentation.)

Also, I notice a slight inaccuracy in your delay value (84705), which is six hours divided by 255. Your loops actually execute 256 times, so the correct value should be 84375. Here's a version which will probably work better, although keep in mind that the Aduino clock is not terribly accurate, so by the end of that long Norwegian winter you may find that your plants have drifted a bit.
:slight_smile:

Mikal

// Daytime LED
int pins[] = {11, 10, 9, 6, 5, 3};
#define NUMPINS 6

void setup()
{
for (int i=0; i<NUMPINS; ++i)
pinMode(pins*, OUTPUT);*
}
void set_pins(int value)
{

  • for (int i=0; i<NUMPINS; ++i)*
    _ analogWrite(pins*, value);_
    _
    }_
    void loop()
    _
    {_
    _
    // First six hours: fade in*_
    * for (int value = 0 ; value <= 255; value++) { // fade in (from min to max in 256 steps)*
    * set_pins(value);
    _ delay(6UL * 60 * 60 * 1000 / 256); // Delay 1/256 of 6 hours = 84375ms_
    _
    }_
    _
    // Next six hours: fade out*_
    * for (int value = 255; value >= 0; value--) { // fade out (from max to min in 256 steps)*
    * set_pins(value);
    _ delay(6UL * 60 * 60 * 1000 / 256); // Delay 1/256 of 6 hours_
    _
    }_
    _
    // Next twelve hours: off*_
    * set_pins(0);
    _ delay(12UL * 60 * 60 * 1000);_
    _
    }_
    _
    [/quote]*_

Hi Mikal,

I'm not sure whether this could be the cause of your erratic behavior, but I note that you did not configure the led pins as OUTPUT pins:
pinMode(ledpin1, OUTPUT);
(They default to INPUT according to the documentation.)

You dont need to call pinMode() to set the pin as an output before calling analogWrite(), that is done in the analogWrite function.

But pinmode does need to be called in setup for my test code using digitalWrite to work.

Hi mem,

You dont need to call pinMode() to set the pin as an output before calling analogWrite(), that is done in the analogWrite function.

Rats. I thought I might be on to something. Thanks.
:slight_smile:

Mikal

hey thnx for the input i'll try things and will get back to u :slight_smile:

(edit)
@ mem:

I put in your code suggestion and am wondering at what point does the LEDs switch on? im guessing at the 3 hour mark? or is it supposed to be on as soon as the prog starts?if its the latter than nothing is happening, no lights. if its the 3 hour thing then i'll leave it running and watch.

I have also burned mikalhart's code to one of the other chips to see if it runs better than the one i had b4.

im guessing at the 3 hour mark?

Right, as soon as "value > 127" becomes true. But don't forget that with digitalWrite, you MUST set pinMode(pin, OUTPUT);

Mikal

you mean like this?

// Daytime LED 


int value = 0;                            // variable to keep the actual value 
int ledpin1 = 11;                           // light connected to digital PWM pins 3 - 11
int ledpin2 = 10;
int ledpin3 = 9;
int ledpin4 = 6;
int ledpin5 = 5;
int ledpin6 = 3;

void setup() 
{ 
      pinMode(ledpin1, OUTPUT);  // nothing for setup 
      pinMode(ledpin2, OUTPUT);
      pinMode(ledpin3, OUTPUT);
      pinMode(ledpin4, OUTPUT);
      pinMode(ledpin5, OUTPUT);
      pinMode(ledpin6, OUTPUT);
     
} 

#define set_pins(value) digitalWrite(ledpin1, value);digitalWrite(ledpin2, value);digitalWrite(ledpin3, value);digitalWrite(ledpin4, value); digitalWrite(ledpin5, value); digitalWrite(ledpin6, value); delay(84705);

void loop()
{
        for(;;) {
                for(value = 0 ; value <= 255; value+=1) {
                        set_pins(value > 127);          // sets 0 for 0-127, 1 for 127-255
                }
                for(value = 255 ; value <= 0; value+=1) {
                        set_pins(value > 127);          // sets 0 for 0-127, 1 for 127-255
                }
                for(value = 0 ; value <= 255; value+=1) {
                        set_pins(0);
                }
                for(value = 255 ; value <= 0; value+=1)   { // fade out (from max to min in 255 steps)
                        set_pins(0);
                }
        }
}

Bong, yes, that sets the pins to outputs for the digitalWrite. But it may be easier to test using Mikal's version. Try it first as he posted. If it still has the problems, try replacing the set_pins function with this test version:

void set_pins(int value)
{
  for (int i=0; i<NUMPINS; ++i){
//   analogWrite(pins[i], value);  // use this for analog writes
   digitalWrite(pins[i], value > 127); // use this for testing ony, pin is set when value greater than 127
  }
}

thnx. i have his version on test now as well as ure digital suggestion. :slight_smile:

i hope to use the fading method tho so i get more realistic daylight effect

its a slow thing tho cos it takes hours to test it :smiley:

I'm not sure why you want to fade them. Do the plants jump with surprise if you turn the light on suddenly? :wink:

no but the gentle cycle of it would also benefit us seeing as we don't get the sun either in them months :wink:

tests still underway. got some bigger LEDs (red white and blue) so there will be more spread of light :slight_smile:

the program seems to be running fine, its been going on and off as it should, (not sure about the times tho i forgot to log it :P)

Im wondering if it would be possible to use some kind of external timepiece (a clock or an old watch with quartz timing) to keep a more precise cycle on it..

Tests seem to have gone well :slight_smile: there doesn't appear to be much drift n the timing and the lights have been coming on and going off at around the same time every day and night almost to the minute :smiley:

thanks for all your help :slight_smile: with luck I'll have some nice chillies from all this :3

i may have some adaptions to the basic program to try out soon :slight_smile:
like advance and go-bak buttons to set the time roughly (with possible indicator leds to show what time of day its at).
who knows.