The Planted Tank Forum - View Single Post - Improved Arduino LED lighting code
View Single Post
post #127 of (permalink) Old 12-09-2012, 11:55 PM
Join Date: Dec 2012
Location: Canada
Posts: 2
Getting it to work with RGB

Ok I have been at this for a week now, I too was looking at getting my RGB lights running off of this simple but wonderful code.

But looking into it getting it to run on the Uno was the easy part but getting 3 CH timer is an other story entirely and I'm not programer I'm more of a band aid hacker lets say.

Well to explain it shortly the Arduino has 3 Timer, Timer0, Timer1, Timer2,
each one of these timers have 2 CH with a variety feature for each.
It polls an compares the millis() and Freq ch A, B. The timers and says hey you need to Turn on, or off, or Fade, ext.. but each timer has 2 Registers to keep track of the time.

And see this is a great feature and what makes this code great, Is its not taking up all your clock times(CPU) while trying to keep track of the time and do other things.

Like keeping track of Temps, or controlling your 3 speed pump or turing them on and off ect..

I tried to add one of the other timer from pin 11 but I don't have all the programming knowledge to get it to work properly, I mean it was working sort of.

So I'm taking the easy route and using the Arduino Mega 2560 it adds 3 more 16 Bit Timers.

Making it easy to modify the code to work withe RGB lights.
Plus if you really wanted too you could use both Timer1 for your main lights and Timer3 for your complementary RGB Lights.

I am plaining to adding lots more features I just need to mash all this code a have together like setting the time from the LCD Unit it self with 2 Buttons or a rotary encoder, Alarms to tell you its time to do Water changes or clean your filter.

If any one is interested when I get it all done I can post it up.

Cheers! Happy coding..
Oh here's the Timer3 file for the Mega I have it using Pin 5,3,2
And you need to add or change this code.
I'm still trying to debug the Error so if you find one or see something wrong let me know.

#include <TimerThree.h>
const int kChan0Pin = 5; // Channel 0 Pin
const int kChan1Pin = 2; // Channel 1 Pin
const int kChan2Pin = 3; // Channel 2 Pin
const int kDayState[] = { 1023, 1023, 1023 }; // daytime LED state
const int kNightState[] = { 0, 0, 0 }; // nighttime LED state
int state_chan1, state_chan2, state_chan3;
void fader(long start_time, const int start_state[], const int end_state[], int out[3]) {

  float per_second_delta_0 = (float) (end_state[0]-start_state[0])/kFadeDuration;
  float per_second_delta_1 = (float) (end_state[1]-start_state[1])/kFadeDuration;
  float per_second_delta_2 = (float) (end_state[2]-start_state[2])/kFadeDuration;
  long elapsed = ctr-start_time; // Calculate the elapsed Time

  out[0] = start_state[0] + per_second_delta_0 * elapsed;
  out[1] = start_state[1] + per_second_delta_1 * elapsed;
  out[2] = start_state[2] + per_second_delta_2 * elapsed;
void set_state(const int state[]) {
  if (state[0] >= 0 && state[0] <= 1023) {
    Timer3.setPwmDuty(kChan0Pin, state[0]);
    state_chan1 = state[0]; }
  if (state[1] >= 0 && state[1] <= 1023) {
    Timer3.setPwmDuty(kChan1Pin, state[1]);
    state_chan2 = state[1]; }
  if (state[2] >= 0 && state[2] <= 1023) {
    Timer3.setPwmDuty(kChan2Pin, state[2]);
    state_chan3 = state[2]; }
Attached Files
File Type: zip (9.0 KB, 112 views)

Last edited by chuckxx; 12-09-2012 at 11:59 PM. Reason: Opps forgot the file
chuckxx is offline  
For the best viewing experience please update your browser to Google Chrome