The Planted Tank Forum

The Planted Tank Forum (http://www.plantedtank.net/forums/index.php)
-   DIY (http://www.plantedtank.net/forums/forumdisplay.php?f=20)
-   -   Another Arduino LED light schedule sketch (http://www.plantedtank.net/forums/showthread.php?t=213234)

benjaf 01-14-2013 11:19 AM

Another Arduino LED light schedule sketch
 
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:
http://codebender.cc/sketch:4232
EDIT:
New location of source:
https://github.com/benjaf/LightController

benjaf 01-14-2013 11:19 AM

[placeholder]

danielt 01-14-2013 02:00 PM

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.

benjaf 01-14-2013 02:13 PM

You're welcome!
Yes, an RTC module will be necessary, the arduino itself is nowhere near accurate enough to to provide useful time keeping for longer periods.

danielt 01-14-2013 04:17 PM

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.

benjaf 01-14-2013 05:13 PM

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.

Darkblade48 01-14-2013 06:59 PM

Quote:

Originally Posted by benjaf (Post 2240538)
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 ;)

benjaf 01-14-2013 08:16 PM

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!

benjaf 01-25-2013 01:57 PM

Just a minor update:
The project has been added to scapegoats's new wiki: opensourceaquatics.com
and the source has moved to Github: Light Controller

AquaPorn 05-15-2013 04:28 PM

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!

JustAGuy716 05-15-2013 09:49 PM

The 5 in the statement designates how many values each row has - start hr, start tm, end hr, end time, and pwm intensity. That's 5 things for each period.

AquaPorn 05-15-2013 10:20 PM

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.

AquaPorn 05-15-2013 10:42 PM

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.

JustAGuy716 05-16-2013 12:39 AM

Quote:

Originally Posted by AquaPorn (Post 3373322)
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)

AquaPorn 05-16-2013 04:46 AM

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.


All times are GMT. The time now is 07:48 AM.

Powered by vBulletin®
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.