Reef Pico LED intefacing, 3 watt LED's and RTC

Hello lovely people!

after a pretty long break i recently picked up a new Arduino mega for a project i sort of started 6 months ago.

long story short, i started never finished, life took over and it was a job to pick back up, now that i've jsut finished my Uni exams for a few months i thought i'd have another stab.

firstly the first project can be found here:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1269962097/0

the idea was to build my own little reef computer for a pico tank that i set up, unfortunatly july 10 i had to dismantle my pico as it wasnt getting the attention it needed plus going away on holiday etc ment it was for the best. i restarted the pico last month and with it restarted my interest in Arduinos, so i ordered my bits and bobs and would like to have another stab at the project.

i deciced to buy a second Mega so i could work on different aspects of the project before compiling it with my old (which is a mess of breadboards, wires, probes etc)

so without wasting any more time

Ladys and gentle men if you so choose to help.

I want to write a piece of software which utilizes the buckpucks of my DIY led light and fade my led's in via PWM and my RTC chip.
once ive got the basics sorted, i would like to incorporate a moon light and possibly have some fun with "random" weather patterns etc.

i've had a bit of a dabble this everning just "relearning the ropes" as it were with coding, the last object orientated programing i did was months a go, and with my poor memory unfortunatly some has slipped ::slight_smile:

So this is the stage im at so far...

....

yup thats it nothing!

ok not quite,

I hooked up my DIY RTC chip to my arduino and pulled over some test coding and used the sereal monitor to check it all works.

this was only ment to be a 10-15 minute job before starting some more productive coding but for some reason my RTC chip isnt working correctly or atleast my coding.

for some reason my "seconds" wont write properly.

when i call the serial monitor up it always starts from 0. edit while typing this entry i realised i needed to hash out the RTC start program after it had been flashed to the RTC chip now we can move onto the fun bit

heres the given test code:

#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h> 

void setup()
{
  Serial.begin(9600);

  RTC.stop();
  RTC.set(DS1307_DOW,5);     
  RTC.set(DS1307_DATE,21); 
  RTC.set(DS1307_MTH,1);    
  RTC.set(DS1307_YR,11); 
  RTC.set(DS1307_SEC,34);       
  RTC.set(DS1307_MIN,46);    
  RTC.set(DS1307_HR,20);         
  RTC.start();

}

void loop()
{

  Serial.print(RTC.get(DS1307_HR,true)); //read the hour and also update all the values by pushing in true
  Serial.print(":");
  Serial.print(RTC.get(DS1307_MIN,false));//read minutes without update (false)
  Serial.print(":");
  Serial.print(RTC.get(DS1307_SEC,false));//read seconds
  Serial.print("      ");                 // some space for a more happy life
  Serial.print(RTC.get(DS1307_DATE,false));//read date
  Serial.print("/");
  Serial.print(RTC.get(DS1307_MTH,false));//read month
  Serial.print("/");
  Serial.print(RTC.get(DS1307_YR,false)); //read year 
  Serial.println();
  delay(1000);
}

How would you guys go about implimenting some form of coding where say

8:00AM = start fading first chain of led's in
8.30AM = start fading in second chain of led's in
8.00PM = start fading second chain of led's out
8.30PM = start fading second chain of leds out.

id like each chain to fade in roughly from 0-100% over the space of say 1hour

would you guys use something like a multi argument IF statement?

if((DS1307_HR == 8)&&(DS1307_MIN = 0){
....
}

i know this consept is far from new nor is the application, yes i have googled and come across a little but i just wanted to compile all my thoughts, code etc into my own thread.

Many thanks in advanced

Luke

P.s.

if anyones interested:

my 4ft x 2ftx 2ft Mixed reef
http://www.ocean-wonders.co.uk/messageboard/showthread.php/7553-Phoenix-s-Reef-V3.0/page18

and my LED driven Pico which this arduino project is for:
http://www.ocean-wonders.co.uk/messageboard/showthread.php/6439-Phoenix-s-Little-Suprise/page16

8:00AM = start fading first chain of led's in
8.30AM = start fading in second chain of led's in
8.00PM = start fading second (first?) chain of led's out
8.30PM = start fading second chain of leds out.

Sounds like an interesting project. I'll just give a few little basic timing values to maybe get you started. Having a RTC makes it a pretty simple task I think. I will assume you will be using the analogWrite() function to PWM your led brightness.

As the analogWrite() function only has a 0-255 step capacity for dimming of 0-100% brightness, and you wish to fade up or down over a 12 hour period, that means incrementing or decrementing the PWM value every 3 mins, that is close enough I think, (3X256) / 60 = 12.8 hours cycle.

So your sketch needs to keep reading the RTC until it sees a 'start' time and then starts a pwm analogWrite command starting with 0 and incrementing the pwm value every three mins as read from the RTC. You can have o two time frames going if you wished to have some overlap time between the two light assemblies, other wise it's just keep reading the RTC and see when it's time to make a move.

Lefty

Hello Lefty, thank you for the reply and the information.

firstly to fix the quote

8:00AM = start fading first blue chain of led's in
8.30AM = start fading in second white chain of led's in
8.00PM = start fading second white chain of led's out
8.30PM = start fading first blue chain of leds out.

and regarding the time frame you are correct but i mislead you with my description.

i want the leds to fade in from 0-100% over the space fo say 1Hour
then to fade back, but its essentially the same as what you described.

i kinda came up with a solution this mornig in the shower :-?

i was thinking

if i had some code something along the lines of this i'll jsut do it in speak not code

At 8am set boolean blueOn to true
At 8.30am set boolean whiteOn to true
at midday set boolean blueOn and whiteOn to false
At 8pm set boolean whiteOff to true
At 8.30pm set boolean blueOff to true
At 3.00 Am set boolean whiteOff and BlueOFF to false

then while the arduino is looping call an imade fade object

that has various If statements saying if boolean XYZ is true do this

then the system will jsut fade at its own pace until completed with the parameters being reset at midday and 3am respectively.

What do you guys think? i mean i havent had a chance to play yet but i will do now/within the next hour or so after ive finished tidying up the house >:( ::slight_smile:

although i havent had a chance to test this code particullarly what do you guys think of the coding syntax/ideas etc the use of if loops, booleans etc

#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>

boolean whiteFadeOn = false;
boolean whiteFadeOff = false;
boolean blueFadeOn = false;
boolean blueFadeOff = false;
int whiteLedPin = 9;
int blueLedPin = 10;
int userDefMaxWhite = 100; //0-100 user defines the max intensity of the leds
int userDefMaxBlue =50; //0-100 user defines the max intensity of the leds
int fadeMaxWhite;
int fadeMaxBlue;

void setup(){
 // rtcStart();
}

void loop(){
  maxCalc();
  ledFade();
  rtcCatch();  
}

void maxCalc(){
  fadeMaxWhite = userDefMaxWhite*2.55;//converts user defined percentage (0-100) into a PWM value (0-255)
  fadeMaxBlue = userDefMaxBlue*2.55;//converts user defined percentage (0-100) into a PWM value (0-255)
}

void ledFade(){   
  if(whiteFadeOn = true){
    for(int fadeValue = fadeMaxWhite ; fadeValue <= 255; fadeValue +=1) { 
    analogWrite(whiteLedPin, fadeValue);             
    delay(60000); 
    }
  }
  
  if(blueFadeOn = true){
    for(int fadeValue = fadeMaxBlue ; fadeValue <= 255; fadeValue +=1) { 
    analogWrite(blueLedPin, fadeValue);             
    delay(60000); 
    }
  }
  
  if(whiteFadeOff = true){
    for(int fadeValue = fadeMaxWhite  ; fadeValue >= 0; fadeValue -=1) { 
    analogWrite(whiteLedPin, fadeValue);          
    delay(60000);                            
    }
  }
  
  if(blueFadeOff = true){
    for(int fadeValue = fadeMaxWhite  ; fadeValue >= 0; fadeValue -=1) { 
    analogWrite(blueLedPin, fadeValue);          
    delay(60000);                            
    }
  }
}

void rtcCatch(){
  if((DS1307_HR == 8)&&(DS1307_MIN == 0)){
    blueFadeOn = true;  
  }
  
  if((DS1307_HR ==8)&&(DS1307_MIN == 30)){
    whiteFadeOn = true;  
  }

  if((DS1307_HR ==13)&&(DS1307_MIN == 0)){
    whiteFadeOn = false;  
    blueFadeOn = false;
  }

  if((DS1307_HR ==20)&&(DS1307_MIN == 0)){
    whiteFadeOff = true;  
  }

  if((DS1307_HR ==20)&&(DS1307_MIN == 30)){
    blueFadeOff = true;  
  }

  if((DS1307_HR ==3)&&(DS1307_MIN == 0)){
    whiteFadeOff = false; 
    blueFadeOff = false; 
  }  
}

void rtcStart(){
  RTC.stop();
  RTC.set(DS1307_SEC,1);        //set the seconds
  RTC.set(DS1307_MIN,11);     //set the minutes
  RTC.set(DS1307_HR,13);       //set the hours
  RTC.set(DS1307_DOW,4);       //set the day of the week
  RTC.set(DS1307_DATE,22);       //set the date
  RTC.set(DS1307_MTH,1);        //set the month
  RTC.set(DS1307_YR,11);         //set the year
  RTC.start();   
}

i also plan to clean the code up a bit and put it into 2 objects purely so i can help break the code into segments!

cleaned up and now on version 1.01. ive added the abillity for the user to define the led intensity as a percentage from 0-100% currently by code which is then converted from a percentage into a PWM value.

(in the finished verion or the next version i will add a screen and buttons that enable the user to set the intensity on the fly, might do that next as ive got various lcds and an lcd shield)

also another point if I multiply an Int by a float its possible to get the occational float value, will that automatically be parsed into an int? i.e. rounded up. im assuming so .... need to plug it in and see what happens.

edit also realised a bit of code error in the fade code, so im just looking at that too.

huzzzar!

i've managed to fix the majority of the code, infact most of that code above needed re-writing/ tweeking.

im just testing it a bit more then i'll start looking into intergrating an LCD and buttons.

once ive got the fade to work correctly (im sure its just some of my syntax) i'll post the latest code.

edited later:

Well here we are at the moment, i've managed to write the correct code which fades leds in and out at user defined times.

The next stage is to incroperate an LCD and buttons, so i can write code for a menu system that enables the user to set the Maximum led intensity values (fadeMaxWhite and fadeMaxBlue) and then write code that enables the user to define what time they would like the various lights to come on and off at.

Then from there id like to rig a Moonlight, with a 1 watt led maybe. or possibly play with LED matrix's for random weather patterns maybe.

//Marine Reef Pico Led lighting controller Version 1.03 
//created by Harlequin 22-01-11.
//Based on various code librarys avalable on the public domain along with coding compiled by the author.

#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>

int whiteFadeOn = 0;
int whiteFadeOff = 0;
int blueFadeOn = 0;
int blueFadeOff = 0;

int whiteLedPin = 9; // White Led chain linked to Digital pin9
int blueLedPin = 10; // Blue Led chain linked to Digital pin10

int blueFadeValue = 0;
int whiteFadeValue = 0;

int userDefMaxWhite = 100; //0-100 user defines the max intensity of the leds
int userDefMaxBlue = 100; //0-100 user defines the max intensity of the leds
int fadeMaxWhite; //variable used when converthing userDefMaxWhite into a PWM value
int fadeMaxBlue; //variable used when converthing userDefMaxBlue into a PWM value

int hour;//variable used for the RTC timed event.
int minute;//variable used for the RTC timed event.

int counter = 0;//counter for PWN

void setup(){
  Serial.begin(9600); //not needed in end product
  rtcStart(); 
}

void loop(){
  debug(); //not needed in end product
  maxCalc();
  ledFade();
  rtcCatch();  
}

void ledFade(){  
  if(counter < 60){
    counter++;
  }
  else{
    
    if(whiteFadeOn == 1){
      if(whiteFadeValue <= fadeMaxWhite) { 
        analogWrite(whiteLedPin, whiteFadeValue);       
        whiteFadeValue = whiteFadeValue+4; // 255/4 = 63.75 i.e. 0-100% takes 63.75 minutes roughly to complete if it takes 1 minutes to complete 1 cycle.
      }
    }

    if(blueFadeOn == 1){
      if(blueFadeValue <= fadeMaxBlue) { 
        analogWrite(blueLedPin, blueFadeValue);       
        blueFadeValue = blueFadeValue+4;
     }
    }  
  
    if(blueFadeOff == 1){
      if(blueFadeValue >= 0) { 
        analogWrite(blueLedPin, blueFadeValue);       
        blueFadeValue = blueFadeValue-4;
      }
    }
  
    if(whiteFadeOff == 1){
      if(whiteFadeValue >= 0) { 
        analogWrite(whiteLedPin, whiteFadeValue);       
        whiteFadeValue = whiteFadeValue-4;
     }
   }
   counter =0;
}
}








void maxCalc(){
  fadeMaxWhite = userDefMaxWhite*2.55;//converts user defined percentage (0-100) into a PWM value (0-255)
  fadeMaxBlue = userDefMaxBlue*2.55;//converts user defined percentage (0-100) into a PWM value (0-255)
}

void rtcCatch(){
  hour = RTC.get(DS1307_HR,false);
  minute = RTC.get(DS1307_MIN,false);
  
  if((hour == 8)&&(minute == 0)){//sets for 08:00
    blueFadeOn = 1;  
  }
  
  if((hour ==8)&&(minute == 2)){//sets for 08:02
    whiteFadeOn = 1;  
  }

  if((hour ==15)&&(minute == 0)){//sets for 15:00
    whiteFadeOn = 0;  
    blueFadeOn = 0;
  }

  if((hour ==20)&&(minute == 0)){//sets for 20:00
    whiteFadeOff = 1;  
  }

  if((hour ==20)&&(minute == 30)){//sets for 20:30
    blueFadeOff = 1;  
  }

  if((hour ==3)&&(minute == 0)){
    whiteFadeOff = 0; 
    blueFadeOff = 0;
     
  }  
}

void debug(){ //not needed in end product
  Serial.print(RTC.get(DS1307_HR,true)); Serial.print(":"); Serial.print(RTC.get(DS1307_MIN,false));Serial.print(":");Serial.print(RTC.get(DS1307_SEC,false));
  Serial.print("      Counter:");    
  Serial.print(counter); 
  Serial.print("      "); 
  Serial.print(RTC.get(DS1307_DATE,false));Serial.print("/"); Serial.print(RTC.get(DS1307_MTH,false));Serial.print("/");Serial.print(RTC.get(DS1307_YR,false));Serial.print(" "); Serial.print(RTC.get(DS1307_DOW,false));
  Serial.print(" Blue LED state:");Serial.print(blueFadeOn);Serial.print(blueFadeOff);Serial.print("   PWM Value:");Serial.print(blueFadeValue);
  Serial.print("      ");
  Serial.print("White LED state:");Serial.print(whiteFadeOn);Serial.print(whiteFadeOff);Serial.print("   PWM Value");Serial.print(whiteFadeValue);
  Serial.println(); 
  delay(1000);
}

void rtcStart(){
  RTC.stop();
  RTC.set(DS1307_SEC,01);        //set the seconds
  RTC.set(DS1307_MIN,59);     //set the minutes
  RTC.set(DS1307_HR,7);       //set the hours
  RTC.set(DS1307_DOW,6);       //set the day of the week
  RTC.set(DS1307_DATE,22);       //set the date
  RTC.set(DS1307_MTH,1);        //set the month
  RTC.set(DS1307_YR,11);         //set the year
  RTC.start();   
}

Well i've managed to wire up a basic 1602 LCD screen that 16 char by 2.
so im now just meshing the LCD screen and my LED code. I also want to write some code that uses some basic push buttons as input buttons, then i'll write my menu code.

Its kinda nice how quick some of this coding is coming back to me, plus had some fun decyfering the datasheet for this lcd, doing a bit of soldering etc.

Harle