Pages: [1] 2   Go Down
Author Topic: LED light timed cycle problems  (Read 823 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 370
R-Doo-Inoo in the making :3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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})

Code:
// 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-grin about programming and the hardware.

thanx

Bng. smiley-wink
Logged

B-dui in creation.

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 370
R-Doo-Inoo in the making :3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i was using 0012

i dont think i used 0011 either wen i uploaded the 1st version of code smiley-sad

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
Logged

B-dui in creation.

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mikal

Quote

// 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);
}
 
« Last Edit: September 22, 2008, 02:18:28 pm by mikalhart » Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Mikal,

Quote
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.
« Last Edit: September 22, 2008, 02:09:32 pm by mem » Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi mem,

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

Mikal
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 370
R-Doo-Inoo in the making :3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

(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.
« Last Edit: September 22, 2008, 08:34:32 pm by The_Bongmaster » Logged

B-dui in creation.

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 370
R-Doo-Inoo in the making :3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

you mean like this?

Code:
// 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);
                }
        }
}

« Last Edit: September 22, 2008, 11:36:54 pm by The_Bongmaster » Logged

B-dui in creation.

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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
  }
}
« Last Edit: September 23, 2008, 12:10:49 am by mem » Logged

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 370
R-Doo-Inoo in the making :3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

B-dui in creation.

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31534
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 370
R-Doo-Inoo in the making :3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

B-dui in creation.

Norway
Offline Offline
Sr. Member
****
Karma: 0
Posts: 370
R-Doo-Inoo in the making :3
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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 smiley-razz)

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

B-dui in creation.

Pages: [1] 2   Go Up
Jump to: