Another Arduino LED light schedule sketch
Planted Tank Forums
Your Tanks Image Hosting *Tank Tracker * Plant Profiles Fish Profiles Planted Tank Guide Photo Gallery Articles

Go Back   The Planted Tank Forum > Specific Aspects of a Planted Tank > DIY


Reply
 
Thread Tools Display Modes
Old 01-14-2013, 12:19 PM   #1
benjaf
Algae Grower
 
benjaf's Avatar
 
PTrader: (0/0%)
Join Date: Mar 2012
Location: Denmark
Posts: 105
Default

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.

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

Last edited by benjaf; 05-22-2013 at 09:38 AM.. Reason: Code rewritten
benjaf is offline   Reply With Quote Quick reply to this message
Sponsored Links
Advertisement
 
Old 01-14-2013, 12:19 PM   #2
benjaf
Algae Grower
 
benjaf's Avatar
 
PTrader: (0/0%)
Join Date: Mar 2012
Location: Denmark
Posts: 105
Default

[placeholder]
__________________
benjaf is offline   Reply With Quote Quick reply to this message
Old 01-14-2013, 03:00 PM   #3
danielt
Planted Tank Obsessed
 
PTrader: (0/0%)
Join Date: Dec 2012
Location: In an aquarium nearby
Posts: 447
Default

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.
danielt is offline   Reply With Quote Quick reply to this message
Old 01-14-2013, 03:13 PM   #4
benjaf
Algae Grower
 
benjaf's Avatar
 
PTrader: (0/0%)
Join Date: Mar 2012
Location: Denmark
Posts: 105
Default

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.
__________________
benjaf is offline   Reply With Quote Quick reply to this message
Old 01-14-2013, 05:17 PM   #5
danielt
Planted Tank Obsessed
 
PTrader: (0/0%)
Join Date: Dec 2012
Location: In an aquarium nearby
Posts: 447
Default

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.
danielt is offline   Reply With Quote Quick reply to this message
Old 01-14-2013, 06:13 PM   #6
benjaf
Algae Grower
 
benjaf's Avatar
 
PTrader: (0/0%)
Join Date: Mar 2012
Location: Denmark
Posts: 105
Default

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.
__________________
benjaf is offline   Reply With Quote Quick reply to this message
Old 01-14-2013, 07:59 PM   #7
Darkblade48
Moderator
 
Darkblade48's Avatar
 
PTrader: (3/100%)
Join Date: Jan 2008
Location: Japan
Posts: 9,390
Default

Quote:
Originally Posted by benjaf View Post
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
__________________
Anthony

A Primer to Pressurized CO2 and A Primer to Planted Tanks
Eheim Pimp #362 - Eheim 2213 x2, Eheim 2028, Eheim 2217, Eheim surface skimmer and Eheim autofeeder.
Victor Pimp #33 - HPT272-125-350-4M
Darkblade48 is offline   Reply With Quote Quick reply to this message
Old 01-14-2013, 09:16 PM   #8
benjaf
Algae Grower
 
benjaf's Avatar
 
PTrader: (0/0%)
Join Date: Mar 2012
Location: Denmark
Posts: 105
Default

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 is offline   Reply With Quote Quick reply to this message
Old 01-25-2013, 02:57 PM   #9
benjaf
Algae Grower
 
benjaf's Avatar
 
PTrader: (0/0%)
Join Date: Mar 2012
Location: Denmark
Posts: 105
Default

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
__________________
benjaf is offline   Reply With Quote Quick reply to this message
Old 05-15-2013, 05:28 PM   #10
AquaPorn
Algae Grower
 
PTrader: (0/0%)
Join Date: May 2013
Location: Chicago Suburb
Posts: 26
Default

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!
AquaPorn is offline   Reply With Quote Quick reply to this message
Old 05-15-2013, 10:49 PM   #11
JustAGuy716
Algae Grower
 
PTrader: (0/0%)
Join Date: Aug 2012
Location: Madison WI
Posts: 42
Default

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.
JustAGuy716 is online now   Reply With Quote Quick reply to this message
Old 05-15-2013, 11:20 PM   #12
AquaPorn
Algae Grower
 
PTrader: (0/0%)
Join Date: May 2013
Location: Chicago Suburb
Posts: 26
Default

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 is offline   Reply With Quote Quick reply to this message
Old 05-15-2013, 11:42 PM   #13
AquaPorn
Algae Grower
 
PTrader: (0/0%)
Join Date: May 2013
Location: Chicago Suburb
Posts: 26
Default

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.
AquaPorn is offline   Reply With Quote Quick reply to this message
Old 05-16-2013, 01:39 AM   #14
JustAGuy716
Algae Grower
 
PTrader: (0/0%)
Join Date: Aug 2012
Location: Madison WI
Posts: 42
Default

Quote:
Originally Posted by AquaPorn View Post
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)

Last edited by JustAGuy716; 05-16-2013 at 01:46 AM.. Reason: Found additional possible cause
JustAGuy716 is online now   Reply With Quote Quick reply to this message
Old 05-16-2013, 05:46 AM   #15
AquaPorn
Algae Grower
 
PTrader: (0/0%)
Join Date: May 2013
Location: Chicago Suburb
Posts: 26
Default

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.

Last edited by AquaPorn; 05-16-2013 at 06:48 AM.. Reason: Forgot something
AquaPorn is offline   Reply With Quote Quick reply to this message
Reply

Tags
arduino, diy, led

Quick Reply
Message:
Options

Register Now

In order to be able to post messages on the The Planted Tank Forum forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in

Human Verification

In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.



Thread Tools
Display Modes

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:36 AM.


Powered by vBulletin®
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright Planted Tank LLC 2012