Reef Tank lighting project

Hi guys,

I've recently got into marine fishkeeping and have started a small reef tank with small fish a some coral.
I went out looking for a lighting unit to suit the tank and I ended up getting one suitable for a larger tank at the right price - a large LED fitting for £30!!

I've decided I'm going to add dimmer functionality to it, and I've decided I might as well have it timer controlled at the same time.
Logical option was an Arduino.

I've got an Arduino Nano, a RTC module, and two LDD-500H Meanwell dimmable current-limited drivers.

I'm planning to drive my white LEDs using one LDD, and the blue LEDs from another.
I'm wanting to use PWM on the Arduino, and commands based on the Real Time Clock to adjust light intensity, can anyone help me get started on my code? I'm wanting to start fading the lights on gradually, peaking in the middle of the afternoon, and then fading them out. I haven't used an RTC before so any help with that would be greatly appreciated!

thanks,
Steve

P.S. The Arduino I'm using is an Arduino Nano, £3 from China! There was one for £1.50 but you had to buy a module to connect a USB and programme it for another £1.50 (although if you have a lot of projects it could save a few £!

I started this for my tank but never finished. Here is what I have so far. Delays need to be replaced with millis() or RTC.
The way it is written will fade the LED up to full power in an hour stay on for 6 hours then fade down to off.

int ledone = 9;  // the pin that the LED is attached to
int ledtwo = 10;
int ledthree = 11;
int brightness = 55;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin 9 to be an output:
  pinMode(ledone, OUTPUT);
   pinMode(ledtwo, OUTPUT);
    pinMode(ledthree, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  {

  // set the brightness of pin 9:
  analogWrite(ledone, brightness);
  analogWrite(ledtwo, brightness);
  analogWrite(ledthree, brightness);
  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

if (brightness == 255){
  delay(25200000);
}


  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 55 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(18000);                            
    }

-ruli00

Thanks for your reply!

Could you point me towards any kind of resources for using a Tiny RTC I2C module? Got it cheap on eBay and came with no information to help or anything!

Built the circuit last night, my 2x Mean Well RDD 500-H, Arduino Nano, and the wiring seems to be done; just need programming up and the power supplies / LED's connecting on either end!

Steve, what chip is on your RTC? DS1307, DS3231, something else?

tylernt:
Steve, what chip is on your RTC? DS1307, DS3231, something else?

It's this one
http://www.ebay.co.uk/itm/261454103577?_trksid=p2059210.m2749.l2649&ssPageName=STRK%3AMEBIDX%3AIT

Just a small update - I have got my circuit sorted, and have justed tested it using this code I took and modified slightly from the Learning section of the website. Brightness is stepped up 5/255 every 0.3 seconds. I set it between 20 and 255 as I was worried about rapidly switching the LED's on and off (but it's probably not worth worrying about...)

I'm going to set up the program using long delay times, and rigging it to a timer switch - Arduino will lose power at midnight, and get power back around 10am. It will then use delays to adjust brightness, and will run through my cycle until the timer removes power.

I'd like to replace this with:

RTC using pre-set times to adjust brightness / turn off

Manual Override switch - have a switch to turn off the timed PWM and use two potentiometers to adjust the white and blue channels.

Backlit LCD, showing Time on the top line and Blue % + White% - message displayed in override mode saying "MANUAL CONTROL" instead of showing the time.

/*
 Fade
 
 This example shows how to fade an LED on pin 3
 using the analogWrite() function.
 
 This example code is in the public domain.
 */

int led = 3;           // the pin that the LED is attached to
int brightness = 20;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  {
  // declare pin 3 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()  {
  // set the brightness of pin 3:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 20 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }    
  // wait for 300 milliseconds to see the dimming effect    
  delay(300);                            
}

This is my current code; bare in mind the arduino will be reset using a timer switch, that will turn everything off from midnight until 10am.

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 D3 White
 D4 Blue
 
 */

int whiteled = 3;
int blueled = 4;
// the pin that the LEDs are attached to

// the setup routine runs once when you press reset:
void setup()  {
  // declare pin 3 to be an output:
  pinMode(whiteled, OUTPUT);
  pinMode(blueled, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()  {
  // set the brightness of pin 3:
  analogWrite(blueled, 10);    //10.00am
  delay(1200000);
  analogWrite(blueled, 15); //10.20am
  delay(1200000);
 analogWrite(blueled, 20); //10.40am
 delay(1200000);
analogWrite(blueled, 20);  //11.00am
 delay(1800000);
 analogWrite(blueled, 30); //11.30am
 analogWrite(whiteled, 15);
delay(1800000);
 analogWrite(blueled, 40); // 12.00pm
 analogWrite(whiteled, 25);
 delay(1800000);
 analogWrite(blueled, 50);  //1.00pm
 analogWrite(whiteled, 35);
 delay(3600000);
 analogWrite(blueled, 60); //1.30pm
 analogWrite(whiteled, 50);
 delay(7200000);
 analogWrite(blueled, 65);  //2.00pm
 analogWrite(whiteled, 70);
 delay(14400000);
 analogWrite(blueled, 55); //6.00pm
 analogWrite(whiteled, 40);
  delay(1800000);
 analogWrite(blueled, 50);  //6.30pm
 analogWrite(whiteled, 35);
 delay(1800000);
 analogWrite(blueled, 40); // 7.00pm
 analogWrite(whiteled, 25);
 delay(1800000);
 analogWrite(blueled, 35); // 7.30pm
 analogWrite(whiteled, 20);
  delay(1800000);
 analogWrite(blueled, 25); // 8.00pm
 analogWrite(whiteled, 20);
  delay(1800000);
 analogWrite(blueled, 20); // 8.30pm
 analogWrite(whiteled, 15);
  delay(1800000);
 analogWrite(blueled, 15); // 9.00pm
 analogWrite(whiteled, 0);
}

Revised Code
This version works correctly (Arduino Nano doesn't have PWM on D4 - oops!). Also moved code out of the loop so that when it reaches the night-time setting at 9.00pm it remains there indefinitely. This will be cut out by my timer switch at midnight, and when power is restored at 10.00am the light will start running the code from the start.

Oops didn't post my code.

Update: Light came on at 10am as per timer plug on the power supplies. Arduino came on and put the blue LED set on at 10/255. Lights gradually brightened and white set have begun to fade in about 10 minutes ago. It would appear my solution is working!

Forgot to point out, the LED unit is designed for a larger tank, which is why I'm running it at about 25% of it's full power. This will be tweaked as necessary as I'm currently adjusting how it's hung, and will soon be changing tanks (to a 30" long tank instead of a 12" cube).

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 D3 White
 D5 Blue
 
 */

int whiteled = 3;
int blueled = 5;
// the pin that the LEDs are attached to

// the setup routine runs once when you press reset:
void setup()  {
  // declare pin 3 to be an output:
  pinMode(whiteled, OUTPUT);
  pinMode(blueled, OUTPUT);
  
    // set the brightness of pin 3:
  analogWrite(blueled, 10);    //10.00am
  delay(1200000);
  analogWrite(blueled, 15); //10.20am
  delay(1200000);
 analogWrite(blueled, 20); //10.40am
 delay(1200000);
analogWrite(blueled, 20);  //11.00am
 delay(1800000);
 analogWrite(blueled, 30); //11.30am
 analogWrite(whiteled, 15);
delay(1800000);
 analogWrite(blueled, 40); // 12.00pm
 analogWrite(whiteled, 25);
 delay(1800000);
 analogWrite(blueled, 50);  //1.00pm
 analogWrite(whiteled, 35);
 delay(3600000);
 analogWrite(blueled, 60); //1.30pm
 analogWrite(whiteled, 50);
 delay(7200000);
 analogWrite(blueled, 65);  //2.00pm
 analogWrite(whiteled, 70);
 delay(14400000);
 analogWrite(blueled, 55); //6.00pm
 analogWrite(whiteled, 40);
  delay(1800000);
 analogWrite(blueled, 50);  //6.30pm
 analogWrite(whiteled, 35);
 delay(1800000);
 analogWrite(blueled, 40); // 7.00pm
 analogWrite(whiteled, 25);
 delay(1800000);
 analogWrite(blueled, 35); // 7.30pm
 analogWrite(whiteled, 20);
  delay(1800000);
 analogWrite(blueled, 25); // 8.00pm
 analogWrite(whiteled, 20);
  delay(1800000);
 analogWrite(blueled, 20); // 8.30pm
 analogWrite(whiteled, 15);
  delay(1800000);
 analogWrite(blueled, 15); // 9.00pm
 analogWrite(whiteled, 0);
}

// the loop routine runs over and over again forever:
void loop()  {

}

Just need to work out the RTC now to allow proper times to be used instead of the plug timer.

Also need to look into using an LCD shield with the Nano (I believe the 6 pin port is for this?) and also how I can add a manual mode with two Pots as dimmers instead of the preset values - I'm assuming an If statement? But then I'm unsure where I'd put this in when not running a loop?

You will need to completely rewrite your code if you want to use manual override with pots. All those delays will have to go. I'd suggest working on the RTC piece and then adding the override.

I agree with wildbill -- delay()s will have to go for manual pots and/or override and/or an LCD.

SteveZZR, since you want to "multitask", you're going to want a Finite State Machine or something very similar. Examples:

http://playground.arduino.cc/code/FiniteStateMachine
http://playground.arduino.cc/Code/SMlib

You certainly don't HAVE to use either of the above libraries, as a simple switch/case structure works fine too. The main concept you need to use is called non-blocking code. The following is very helpful:

Hi,

RTC Information, sample code HERE: