The Planted Tank Forum banner

1 - 20 of 109 Posts

·
Registered
Joined
·
113 Posts
Discussion Starter · #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
 

·
Registered
Joined
·
447 Posts
Thank you kind sir! Do I need the RTC module for this? I have the Arduino Mega and plan on writing myself a light timer for my salt water aquarium. Didn't looked at the code yet.
 

·
Registered
Joined
·
447 Posts
I thought so. My plan was to use the uptime counter and count the time interval passed to determine how long should I keep the light on. I realize it is a poor way of doing this. I'll try to get a hold on a RTC module and tryout your code.
 

·
Registered
Joined
·
113 Posts
Discussion Starter · #6 ·
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.
 

·
Super Moderator
Joined
·
11,987 Posts
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 ;)
 

·
Registered
Joined
·
113 Posts
Discussion Starter · #8 ·
You're welcome! Might as well share in case someone else can use it!

@danielt: If you make code to emulate a 24H clock and set start time to be the time of compilation, you just have to rebuild and upload to the arduino to set time once in a while. Could work as long as the power is stable!
 

·
Registered
Joined
·
26 Posts
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!
 

·
Registered
Joined
·
26 Posts
Shoot....that's what I thought it was for. I guess I have to keep looking for the bug.

when I have the 5 designated only channels 8 & 9 will light when all nine should be.

back to the drawing board.
 

·
Registered
Joined
·
26 Posts
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.
 

·
Registered
Joined
·
42 Posts
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.
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)
 

·
Registered
Joined
·
26 Posts
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.
 

·
Registered
Joined
·
113 Posts
Discussion Starter · #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.
 

·
Registered
Joined
·
3 Posts
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
 

·
Registered
Joined
·
3 Posts
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
 

·
Registered
Joined
·
113 Posts
Discussion Starter · #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! :)
 
1 - 20 of 109 Posts
Top