The Planted Tank Forum banner

Another Arduino LED light schedule sketch

Tags
arduino diy led
32K views 109 replies 23 participants last post by  boxhead1990 
#1 · (Edited)
Here is the Arduino code I use to control the schedule of my LED lights.

Back story:
I've been redoing my 14G low-tech and wanted to try out LED, so I ordered a 10W ebay-special and planned to hook it up to an Arduino I had lying around. While waiting for it to arrive, I looked around for examples of how others had handled the software side of things. None of the code I found seemed to fit my needs.
I realize a lot of people have done the same, but I wanted something that allowed for individual schedules for any number of channels, including siesta and different length sunrise/sunset.

So in the end, I wrote my own code to control the lights.

Some things that need to be addressed before looking at the code:

1: This is not a complete sketch to control aquarium lights!
While it would work on a setup identical to mine, it wouldn't on much else. The code is meant as a helping hand for those that are not overly comfortable with programming, and find handling more complicated schedules difficult. I have removed all but the essentials in this sketch.

2: This has nothing to do with your hardware!
Ok, that may not be strictly true. But my point is: There are a lot of great guides out there regarding the hardware side of things, I will only be dealing with the software. This is the part that I have found relatively little of on these forums, whereas the hardware gurus seem very active. I provide a way to control the PWM signal, the rest is up to you.

Now, let's get started! :)

Features:
- Any number of individual channels (limited by you Arduino's number of PWM pins)
- Pretty much any light schedule that repeats every 24 hours
- Suitable for RGB lights
- Reasonably smooth fading w. a resolution of 1/255 updated every second

Hardware requirements:
- Arduino (Uno in my case)
- RTC module (DS1307 is used in the example)
- PWM outputs wired CORRECTLY to your LED drivers

Other requirements:
- Some (not necessarily a lot) proficiency with Arduino coding

There are a lot of comments in the code, and I hope it is not too difficult to understand.

UPDATE:
I have made a considerable rewrite, making it much easier to add more channels.
Concept explained:

A lot of this is explained in the code, but I'll try to summarize it here.
To define a schedule for a channel, you set a series of 'Points'. This should be considered a chart, where the line represents the intensity of light over a 24 hour period.
Intensity is a value in the range 0-255. 0 = OFF, 255 = Fully ON

To make lights turn on in the morning with a 30 minute 'sunrise', you would add 2 Points:
08:00 - Intensity = 0
08:30 - Intensity = 255

Explanation:
From 08:00 to 08:30 channel is going from 0-255

It's pretty simple, really.

:sleep: Enough rambling, here is the code. Let me know if you have questions or find it useful!

Link to source:
[STRIKE]http://codebender.cc/sketch:4232[/STRIKE]
EDIT:
New location of source:
https://github.com/benjaf/LightController
 
See less See more
#7 ·
Well it could work for a short period of time if you account for the time drift of your device.
The main issue with this approach is that everything is reset if you lose power. Then there is the rollover of the counter, can't quite remember how often that is.
Every 50 days. Also, as you mentioned, the drift with the Arduino counter can be significant (a few minutes per day) especially if the temperature changes.

Thanks for the code ;)
 
#10 ·
Hi,

I was hoping you can help me a bit with your code. I came across this thread searching for sunrise/sunset code for my marine build.

I am new to Arduino as well as coding, but I usually can dissect it and figure out what does what.

I used this code with the only deviation being I have 9 CHANNELS and 3 MAXPERIODS.

The code compiles, however the results are not what I expect. I vaguely understand most of the code except for:

int lightMatrix [CHANNELS][MAXPERIODS][5] =

Channels index the array 9
Maxperiods index the array 3

5 = ? what does the 5 index in this statement?

I think that this is where my issue might be.

Thanks for the help, and the code!
 
#13 ·
Just a thought that might be my answer...

The function is only finding 2 channels when I have 9. Now, I did not change the code and left it as it was originally written...which was for a 2 channel set-up.

I am now thinking I will need to expand upon the function in order to find all channels and periods.
Does that sound like I may be on the right track??

Thanks for anything.
 
#14 · (Edited)
Did you change the #define lines for CHANNELS & MAXPERIODS?

It also looks like you need to update the int lightvalue[CHANNELS]= to

int lightvalue[CHANNELS] = { 0, 0, 0, 0, 0 };

The same type of things need to be done whenever the phrase ...and so on shows up in the code. Without that change, the pins themselves won't be initialized (for pinMode statements) or updated (for analogWrite statements)
 
#15 · (Edited)
Thanks for taking the time man...

Yes I've had that set up from the beginning.

#define CHANNELS 9
#define MAXPERIODS 3

Also changed

int lightValue [CHANNELS] = {0,0,0,0,0,0,0,0,0};

So, those are in place as well as my 9 channels in the lightMatrix.
The code is commented well, and easy enough to adjust to my set up.
That's why I'm stumped as to why it's not working. It compiles fine. Just not getting desired results.

I think I have narrowed it down to the void function. The Intensity value is not being picked up by the lightValue array.
Still hunting...
I removed the pinMode call in setup...I don't think it is necessary as the code uses analogWrite and the LEDs are on pwm pins, but I can try adding that back.

I can also try changing "#define" to "const"...one drawback with using #define is that it replaces anything with the word CHANNELS with the 9 value. That might be my bug, but I doubt it.
Gut is still telling me I have an issue in that void function somewhere. It's just not finding what period it is.
 
#16 · (Edited)
Glad to hear someone is using, or at least attempting to use, this!
If you can send me a copy of your code, I'd be glad to look it over. The original has not been tested with more than 2 channels, so bugs are by no means impossible.

By the way:
I have been doing some re-factoring to the code to make it more user friendly, but have not had a chance to test it yet.
 
#18 ·
benjaf,

i just registered to say THANK YOU.
I have been hacking on my ardunio for a while now and everything was working fine....
Menu and submenus, Light saving on display (switch of 1 minute after last button pressed), RTC, temperature reading.
but i had problems with the light controls.

first i tried to use TimeAlarms to set alarms when to trigger a new end value and then count down or up every 5 seconds... could have worked good, but i also have some manual settings in my menu.
for example all light off or at 25%, 50%, 75%, 100% or automatic (automatic was using the events of TimeAlarm)

it was working, but after going from manual mode to automatic it was missing the events and did not calculate the current value for the light.

you solved my biggest issue with my arduino! :proud:

with kind regards
dookie
 
#19 ·
Oh,

just a quick question:
Do you have any idea why my lamp is on 0 now? time 22:33 with following channel config:
Channels[0].Pin = 13;
Channels[0].AddPoint(6, 0, 1);
Channels[0].AddPoint(8, 0, 1);
Channels[0].AddPoint(8, 30, 255);
Channels[0].AddPoint(11, 30, 255);
Channels[0].AddPoint(12, 0, 0);
Channels[0].AddPoint(17, 0, 0);
Channels[0].AddPoint(17, 30, 255);
Channels[0].AddPoint(22, 0, 255);
Channels[0].AddPoint(22, 30, 1);
Channels[0].AddPoint(0, 59, 1);
Channels[0].AddPoint(1, 00, 0);

The reason why i sometimes use 1 and 0 as intensity is because when it is 0 i set another digital output to switch off power completely. so with 1 it is supposed to be moon light.

Regards
Dookie
 
#20 ·
First of all, you're welcome!
The reason your schedule does not work, is that you can't define something after 24:00. If the schedule does not end in 24, it will assume lights are off after last 'valid' entry.
What you have to do is move the last 2 entries to the top like this:
(0, 0, 1)
(0, 59, 1)
(1, 0, 0)
(5, 59, 0)
and add (24, 0, 1) to the end. That should to the trick! :)
 
#22 ·
Hi benjaf, great job here.

Looking to modify my Arduino sketch to contain more light periods. I want to split the photoperiod in two.

Can I assume that your code here does that?

Channels[0].AddPoint(8, 0, 0);
Channels[0].AddPoint(8, 30, 255);
Channels[0].AddPoint(11, 0, 0);
Channels[0].AddPoint(11, 10, 0);
Channels[0].AddPoint(13, 0, 0);
Channels[0].AddPoint(13, 10, 255);
Channels[0].AddPoint(20, 0, 255);
Channels[0].AddPoint(20, 30, 0);
From my understanding it goes from 0% (0) to 100% (255) from 8:00 to 8:30 and then it's at 0% again at 11. Does that mean that it fades down from 100% at 8:30 to 0% at 11:00? So the fade period there is 30 minutes up and then 2.5 hours down? Or does the light just switch straight off at 11:00?


Why do you also include another 0% marker at 11:10? Is this just redundancy to make sure it's totally off?

Good work though. Simpler than mine and quite nice.
 
#24 ·
Yep I understand now. The fade up for the "sunrise" is 30 minutes. Then a quick fade down of 10 minutes for a "midday rest". Fade up of 10 minutes "midday wake" then a final 30 minute fade down for a "sunset".

Nicely done.

Now one thing you may have noticed that I can't really seem to crack is that the fade looks a bit bright early on.

It turns out that human eyes perceive light in the logarithmic scale, yet our fades are linear. As such things get to full bright at around 25% of the fade cycle and stay that way until 100%

You may have noticed this. That there's not really a "dawn breaking" effect. It's more dawn for a few minutes then fully bright for the rest. That's because our eyes perceive the full brightness at around 25% and from then on it's still full brightness.

Here are the correct values for a true logarithmic fade from 100% to 0% for a 1 byte (255) range:

Code:
255, 180, 128, 90, 64, 45, 32, 23, 16, 12, 8, 6, 4, 3, 2, 1,0
If you implement these values instead of linearly fading from 100% to 0% (via some sort of lookup array) then you will have a nice fade over time.

I tried to do a calculation that would work out the proper PWM value given a minimum and maximum range (so instead of 100% to 0% if I wanted to fade from 80% to 10%) but I couldn't get one worked out (my maths is poor).

See what you think of the logarithmic fade instead of linear. It's a more gradual effect and I'm sure it's not an issue for the plants. It just means the dimmer values are held a bit longer and so the lights don't look full bright too quickly.
 
#25 ·
So I think I have finalised adding the code to process the linear vs logarithmic scale to my project and thought it could help you and others out.

Here are my results:


With the following values (in case anyone wants to use an array lookup).
Percentage on left, PWM on right:
HTML:
1	0
2	0
3	0
4	0
5	0
6	0
7	0
8	1
9	1
10	1
11	1
12	1
13	1
14	1
15	1
16	1
17	2
18	2
19	2
20	2
21	2
22	2
23	3
24	3
25	3
26	3
27	3
28	4
29	4
30	4
31	4
32	5
33	5
34	5
35	6
36	6
37	7
38	7
39	8
40	8
41	9
42	9
43	10
44	10
45	11
46	12
47	12
48	13
49	14
50	15
51	16
52	17
53	18
54	19
55	20
56	21
57	22
58	23
59	25
60	26
61	28
62	30
63	31
64	33
65	35
66	37
67	39
68	42
69	44
70	47
71	49
72	52
73	55
74	58
75	62
76	65
77	69
78	73
79	77
80	82
81	87
82	92
83	97
84	102
85	108
86	115
87	121
88	128
89	136
90	143
91	152
92	160
93	169
94	179
95	190
96	200
97	212
98	224
99	237
100	250
I sourced this a while ago from THIS SITE

The formula is as follows. It accepts an integer input (0% - 100%) and will output a byte (0 - 255) which can be written directly to the PWM ports.
Code:
byte linearPWM(int percentage)
{
	// coefficients
	double a = 9.7758463166360387E-01;
	double b = 5.5498961535023345E-02;
	return (byte)(floor((a * exp(b * percentage) + 0.5)) -1);
}
 
#26 ·
I have actually been considering adding this, or at least a slightly simplified version of it for some time.
I have been doing some more rewrites to the project in order to simplify usage even more - and will probably add 'perceived linear' fading as an option for each channel. This code is not yet done, but testing is under way. Thanks for doing some of the numbers!
 
#27 · (Edited)
Hi....it's me again. I never got back to thank you for this code, or to let you know I got it up and running without any issues across 9 channels with 3 periods.

I've been running it as my default lighting schedule since the end of May. It's still the older version, but I don't have any issues with it. And I really don't plan to change it.

My question this time is about the timing/intensity matrix.

Is it possible to use a variable within it? Specifically the Intensity?

Current Example:

{
{0, 0, 9, 45, 0},
{10, 45, 16, 55, 90}, ////Wht_Ch1
{17, 55, 24, 0, 0}
},{

I am trying to pass a variable(without success) from a touch "dimming/blending" screen so I will be able to change it without changing the sketch every time.

Something like this:

{
{0, 0, 9, 45, 0},
{10, 45, 16, 55, White_Max}, ////Wht_Ch1
{17, 55, 24, 0, 0}
},{

I figured it would be as simple as passing the variable value into the matrix like that, but it has not. I've tried various ways and variables with out success.

Will the schedule only accept a number between 0 and 255?

Thanks for any help or ideas!

EDIT: here is a photo to try and make clear what I am talking about.

This is a control screen where I can currently dim and blend each of my LED colors.
I would like to be able to set maximum value to the lighting schedule from here.

 
#28 · (Edited)
You're welcome, happy to see someone is still using it :)
Nice interface by the way!
Anyway, on to what you are trying to do:

Passing a variable that way just passes the value of the variable at that given time.
To change the schedule after initialization you have to change the value directly in the matrix like this:

lightMatrix[Channel][Period][INTENSITY] = Variable;

Assuming you want to change period 2 in the first channel:
Code:
lightMatrix[0][1][INTENSITY] = White_Max;
Remember that array indexes start at 0, otherwise results will be rather odd...

EDIT: This must be done every time the variable is changed! (wasn't sure this came across in the original post)

The downside of doing what you do now - any changes will be lost at reset unless you have made some considerable changes to the sketch.
This will be remedied by an upcoming API edition (currently testing) but it will most likely be released as a separate sketch since it has very little in common with the current version..
 
#29 ·
Thanks so much for the quick response, and compliment.

Makes perfect sense to me now. You know...sometimes I get in a rut thinking a specific solution that I can't get by that, and I just need a slap in the head to see things another way.

I'm going to give it a go.

I'm also thinking that I might be able to solve the reset/power loss issue by saving the max lighting value in EEPROM or SD card when the user pushes "enter" on my dimming screen. But, I'll cross that bridge when I get to it.

Another bridge down the road is setting the start time. The total period would be fixed(easier math). Now that I see how I have to designate the variable I hope I can bang that code out too.

Thanks again!!
 
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top