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)
-   -   Improved Arduino LED lighting code (http://www.plantedtank.net/forums/showthread.php?t=147328)

sink 09-06-2011 11:49 PM

Improved Arduino LED lighting code
 
I wasn't happy with any existing Arduino code for LED control so I wrote my own. I think it's quite nice though it needs polishing and probably some features added. It does light timing, sunrise/sunset fading and all that jazz.

Some improvements over existing code:
  • Shorter and simpler
  • Accurately set clock from a script on your PC. You don't need to modify any code or upload any sketches!
  • Much, much smoother dimming, especially at the low end (due to a combination of higher resolution PWM output and every-second dimming adjustment instead of every-minute).
  • Higher resolution PWM also means you can dim your LEDs down to a lower intensity than before if your driver is capable. I've found I can dim my LEDs down far enough to be used as moonlights.
  • Flexible timing logic. You can do anything you want (with minor modifications) so long as it's on a 24hr cycle.
There are also some limitations:
  • Your Arduino needs to have either a DS3231(eg: ChronoDot) or DS1307 Real Time Clock chip connected via I2C (because I have one of those). These are probably the two most common RTC chips.
  • It only provides 2 PWM outputs (because I only have 2 LED strings). Of course you can modulate as many strings as you want from a single PWM signal but they'll modulate in unison.
  • It should work with the Arduino Duimilanove/Uno and probably any other micro-controller that uses the Atmega168/328 chip. In particular it will not work as-is with the Arduino Mega due to a difference in the onboard timers.
  • It depends on a few libraries but I've bundled them up in a download link below.
  • Only compiles with versions 0022/0023 of the Arduino IDE, not version 1.0 (yet!)

It works great for me. I'd love feedback, error reports, suggestions, free beers, etc. Post in the thread or PM me.

I hope this will be useful to people! :thumbsup:

Get the code:
Latest version
Libraries bundle

Installing libraries:
The libraries that don't come with the Arduino environment by default are bundled in the zipfile above. Just extract that zip into the libraries folder inside your sketchbook folder and then restart the Arduino IDE. If libraries doesn't exist, create it first.

Until I get around to writing documentation just post or PM installation questions/issues and I'll try to help.

Latest Version:
05/2014 -- O2surplus has made some significant improvements to this code, adding LCD and temp sensor support, please see his posts later on in this thread for the code.

jcgd 09-07-2011 12:34 AM

I would try it. But I'm not sure how to hook up my rtc. Could you e plain how or link me to a deceit walkthrough?

sink 09-07-2011 01:01 AM

Quote:

Originally Posted by jcgd (Post 1504566)
I would try it. But I'm not sure how to hook up my rtc. Could you e plain how or link me to a deceit walkthrough?

What does the RTC circuit that you have look like? Is it a breakout board? Mine is integrated into my driver because I built it myself, but the principle is the same either way. You just need to power and ground it somehow (5V and GND from the Arduino) and then connect the data pins (SCL and SDA) from the chip to the Arduino. On most Arduino boards (including mine) SDA (data line) is on analog pin 4, and SCL (clock line) is on analog pin 5.

Here's a tutorial that might be applicable Adafruit also sells an RTC breakout board kit with everything you need except wires for under $10. I don't have one but I modeled my RTC circuit after theirs and it works great, so might be an option if you want to add an RTC to an existing setup.

O2surplus 09-07-2011 03:53 PM

I'll give it a try! Took me nearly an hour to hunt down and install the extra libraries, but I got the code to compile without issue. I'll hook up the Dueminlanove this weekend and see what happens. Good job sink- Now if you could only show me how to integrate that coding into the controller that I built, that would be awesome!

sink 09-07-2011 05:31 PM

Quote:

Originally Posted by O2surplus (Post 1505217)
I'll give it a try! Took me nearly an hour to hunt down and install the extra libraries, but I got the code to compile without issue. I'll hook up the Dueminlanove this weekend and see what happens. Good job sink- Now if you could only show me how to integrate that coding into the controller that I built, that would be awesome!

You are welcome. Glad to know it compiles. Once past that step it should run fine. Please let me know what you think! As an aside, you'll probably want to change a couple of variables in the code to match your own needs regarding sunrise/sunset time and in particular how bright you want daylight to be because my default may be quite dim for others. The lines are all towards the beginning of the file and are commented pretty well as to what they do and possible values. I'll write up more detailed instructions when I have some time.

I'd love to make a bundle with the required libraries but I'm not sure if the licenses permit it. In the meantime I'll edit the original post with links to the needed libraries. Thanks for pointing out that task might not be trivial.

I'm willing to help with any driver integration you need. Do you mean the RTC or the code?

O2surplus 09-07-2011 06:15 PM

Sink,

I found the TimerOne and Time libraries at Arduino.cc, so I think you could zip them up together with your code and be good to go. There's one thing you need to fix though, your code cannot be unzipped and moved directly into the Arduino IDE. The IDE will not recognize the folder until it is renamed. I had to change the name of the folder to "akl_tank_control" and the file name to "akl_tank_control.pde" and then it would open and compile properly. This is detail that may cause trouble for people who are not familiar with some of the Arduino IDE's file naming requirements.

I'll have to shoot you a PM with the coding for my controller. It works great as is, but I like the fact that your code updates the PWM every second, instead of once per minute. I've got the hardware side of things nailed down but I could use your help to improve the software.

Here's a photo of the controller board- without the 16x2 LCD on top.

http://i861.photobucket.com/albums/a...a/IMG_1366.jpg

I've got enough boot loaded ATMEGA328's to build 30 more, and a newer design that supports the MeanWell ELN-P's as well as my Cat4101's.

Maybe a trade- Chips or a completed controller for software?

sink 09-07-2011 07:29 PM

Quote:

Originally Posted by O2surplus (Post 1505377)
I found the TimerOne and Time libraries at Arduino.cc, so I think you could zip them up together with your code and be good to go. There's one thing you need to fix though, your code cannot be unzipped and moved directly into the Arduino IDE. The IDE will not recognize the folder until it is renamed. I had to change the name of the folder to "akl_tank_control" and the file name to "akl_tank_control.pde" and then it would open and compile properly. This is detail that may cause trouble for people who are not familiar with some of the Arduino IDE's file naming requirements.

Clearly I too am unfamiliar with the IDE's file naming requirements :icon_smil. I just uploaded a version that should be better in that regard. I also bundled the libraries up and added a link above. Thanks much for the input.

Quote:

I'll have to shoot you a PM with the coding for my controller. It works great as is, but I like the fact that your code updates the PWM every second, instead of once per minute. I've got the hardware side of things nailed down but I could use your help to improve the software.

I've got enough boot loaded ATMEGA328's to build 30 more, and a newer design that supports the MeanWell ELN-P's as well as my Cat4101's
Driver looks great! I'd love to help on the software end.

Quote:

Maybe a trade- Chips or a completed controller for software?
Sounds very reasonable. I do love the barter system. I'll shoot you an email :icon_smil

O2surplus 09-09-2011 01:55 AM

Sink,

I loaded the code into my Duemilanove and connected it up to my Cat4101's, Looks good so far. I made some changes to the code to run the leds at max (1023) all day,and set the night time level to a minimum (20) for moon lighting at night. I think a pwm value of 20 works out to be a hair less than 2%. I should know by 10pm whether or not the leds will continue to run at such a low setting.
I did notice that there's something amiss with the time display in the serial monitor. The time displayed is 1 hour less than the correct time on my PC. Any idea what's causing that? Must be something to do with the DS1307 and daylight savings time?

sink 09-09-2011 02:34 AM

Quote:

Originally Posted by O2surplus (Post 1507173)
Sink,

I loaded the code into my Duemilanove and connected it up to my Cat4101's, Looks good so far. I made some changes to the code to run the leds at max (1023) all day,and set the night time level to a minimum (20) for moon lighting at night. I think a pwm value of 20 works out to be a hair less than 2%. I should know by 10pm whether or not the leds will continue to run at such a low setting.

Your LEDs should run all the way down to a setting of 1 without any problems. The CAT4101s OTOH don't seem to have particularly precise dimming below about 1% though it will still work. I've lowered the default PWM frequency from 500Hz to 150Hz in an effort to improve that so I'm curious to see how your results are.

Quote:

I did notice that there's something amiss with the time display in the serial monitor. The time displayed is 1 hour less than the correct time on my PC. Any idea what's causing that? Must be something to do with the DS1307 and daylight savings time?
That's an interesting one. The DS1307 is completely naive of timezones so the issue would be with the time your computer is putting out somehow not compensating for DST. I presume you set the clock using the timerset.pde sketch?

O2surplus 09-09-2011 02:48 AM

Quote:

Originally Posted by sink (Post 1507224)
That's an interesting one. The DS1307 is completely naive of timezones so the issue would be with the time your computer is putting out somehow not compensating for DST. I presume you set the clock using the timerset.pde sketch?

Oh- I forgot about that separate sketch. My DS1307 must still be running from the last time that I set it. That says a lot since it's been out in a drawer collecting dust for the last six months,LOL. I guess the back up battery must have kept it alive.:thumbsup:
I'll have to try that sketch. I just adjusted the start and stop time in the main sketch to compensate for now. hehe!

sink 09-09-2011 02:58 AM

Quote:

Originally Posted by O2surplus (Post 1507249)
Oh- I forgot about that separate sketch. My DS1307 must still be running from the last time that I set it. That says a lot since it's been out in a drawer collecting dust for the last six months,LOL. I guess the back up battery must have kept it alive.:thumbsup:
I'll have to try that sketch. I just adjusted the start and stop time in the main sketch to compensate for now. hehe!

I figured that was the case :icon_smil. My fault for not getting around to writing proper documentation yet. As an aside, the timeset.pde isn't specific to the lighting control code in any way. It's just a convenient way to easily set the RTC time. It'll work for any Arduino/DS1307 pair.

On battery power the DS1307 shuts down all functions except for keeping the clock running so it can run for 5+ years on a single coin cell. Pretty nifty.

Tbakes 09-09-2011 04:28 PM

Very cool - I'd like to upgrade my controller with the new code once it is worked out!

O2surplus 09-09-2011 04:54 PM

Quote:

Originally Posted by Tbakes (Post 1507788)
Very cool - I'd like to upgrade my controller with the new code once it is worked out!


Well, we'll have to see what "coding magic" "Sink" can pull off! LOL Then I'll show you how to reprogram the controller. He's got a day job- so let's not rush him.LOL

O2surplus 09-10-2011 06:38 AM

Sink,

I uploaded the TimeSet code.....Worked perfectly. I also uploaded the TankControl code again after lowering the minimum PWM state to "5" and stting the fade duration to 1 hour. All I can say is "WOW!" The dimming is completely linear, seamless, and totally natural looking. That has got to be the best Dimming Simulation that I've ever seen! I now have 90 little "moon lights" over my tank. Too cool! Thanks so much for creating and posting the codes!:biggrin::proud:

jcgd 09-10-2011 06:49 AM

Wow, you are getting me excited to download this to my arduino. I need to get that RTC installed.


All times are GMT. The time now is 10:59 AM.

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