Looking for help with DIY Arduino Reef Controller

mikevangels

New member
Hi All,

About 3 years ago I bought all the stuff to put together a DIY LED light for my tank.... fast forward 3 years and I finally got around to putting it together!

The lights are all working now but I'm looking to put together a controller for them.

Has anyone built one of those arduino based controllers? I have an old atmega 1280 that I bought back in the day but I don't have much experience (read none) with coding.

I tried to copy some of the old sketches that I found on various threads but they didn't seem to work properly.

If anyone has any advice or knows of a good resource for a simple and more importantly current sketch for a simple ramp up/down program that would be cool.
 
What hardwate are you using? Do you have a real time clock? What LED drivers do you have?

With a RTC and pwm drivers, I can make a simple sketch gor you.

If you dont have the clock and you just want the lights tp ramp up when the arduino turns on, you can use some very simple sketches. Tell us more about what upu are trying to do.
 
Right now I have 2 channels of led lights... white and blue. Each of these channels is driven by a meanwell eln-60-48p driver.

Currently I have my Arduino board (ATmega1280), a RTC (DS1307 I think?), and a 4x20 LCD screen.

In a perfect world I'd like to be able to ramp up each channel of lights independently then have them ramp down.

For example, at 10am the blue channel would come on; ramp up for 4 hours; stay at full power for 2 hours, then ramp down for 4 hours and turn off.

Then at 11am the white channel would come on; ramp up for 3 hours; stay at full power for 2 hours; then ramp down for 3 hours and turn off.

On the LCD I'd just like to have it display the time, and what percentage the white channel and blue channel of LEDs is at.

I got the arduino hooked up on a breadboard the other day and got the lcd screen working with the typical "Hello World" text and I also got the time to display... it's the led's ramping up and down that I'm kinda lost on.

Any help would be greatly appreciated.
 
All that can be done easily.

this would be generic code for ramping of the lights

PWM.SetDaylight( PWMSlope(11,0,20,0,15,100,120,15) );
PWM.SetActinic( PWMSlope(10,0,20,0,15,100,240,15) );


im not sure how standard arduino code is vs my reef angel but this is basic screen code used by reef angel.


void DrawCustomMain()
{
// the graph is drawn/updated when we exit the main menu &
// when the parameters are saved
ReefAngel.LCD.Clear(BtnActiveColor,5,0,127,11);
ReefAngel.LCD.DrawText(DefaultBGColor,BtnActiveColor,30,3,"My Reef Angel");
ReefAngel.1;
pingSerial();
#if defined DisplayLEDPWM && ! defined RemoveAllLights
ReefAngel.LCD.DrawMonitor(15, 70, ReefAngel.Params,
ReefAngel.PWM.GetDaylightValue(), ReefAngel.PWM.GetActinicValue());
#else // defined DisplayLEDPWM && ! defined RemoveAllLights
ReefAngel.LCD.DrawMonitor(15, 70, ReefAngel.Params);
#endif // defined DisplayLEDPWM && ! defined RemoveAllLights
pingSerial();
byte TempRelay = ReefAngel.Relay.RelayData;
TempRelay &= ReefAngel.Relay.RelayMaskOff;
TempRelay |= ReefAngel.Relay.RelayMaskOn;
ReefAngel.LCD.DrawOutletBox(12, 103, TempRelay);
}

void DrawCustomGraph()
{
ReefAngel.LCD.DrawGraph(5, 15);
}
 
The Reef Angel code will only work if you install the Reef Angel libraries. You can give that a try first, but I won't be much help because I am not familiar with Reef Angel.

Can you post your working code that shows you writing to the LCD screen and reading and displaying the time? If so, I can try to write the ramping code. Do you want a linear ramp then hold (a trapezoid) or would something smoother be better (sinusoidal)? I currently use a sine wave because, by my best guess, that would be more natural.
 
Parabola ( is what reef angel calls it ) it is a constant ramp. ramps up, reaches max, and begins coming back down.

based on what he wanted he wanted the trapezoid style ramp.
 
This is a sketch that I got from one of the old Reefcentral threads....

I thought it would be a good base. After a few changes I got it to compile but the time doesn't seem to display properly (which I guess affects the % display) and I also don't think the whites and blues ramp up independently... it seems the white's are dependent on the blues?

Also, your comment about trapezoid vs sine wave was interesting... sine wave does seem more natural.


PHP:
*/


#define DS1307_I2C_ADDRESS 0x68   // address used for clock
#include "Wire.h"                 // library used
#include <LiquidCrystal.h>        // library used




/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                 L E D   D I M M I N G   P A R T                                                  * */
/* *                                 F A D E S   I N   A N D   O U T                                                  * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */



/* ********************************************************************************* */
/* *     									   * */
/* *     Note: time for ramp down is the same as time for ramp up.                 * */
/* *     									   * */
/* ********************************************************************************* */
int ontime = 10 ;                 // what time do you want the blue to start to ramp up?
int blueramptime = 60 ;           // how many minutes do you want the blue to ramp up?
int whiteramptime = 180 ;         // after the blue, how many minutes do you want the white to ramp up?
int photoperiod = 240 ;           // after the white, how many minutes do you want it to stay at max intensity?
int blue = 3;                     // which arduino pwm pin are you using for the blue?
int white = 11;                   // which arduino pwm pin are you using for the white?


int bluemin = 0 ;          
int whitemin = 0 ; 





/* ********************************************************************************* */
/* *                                                                               * */
/* *     the line below is needed if you are using meanwell ELN60-48P, for pwm     * */
/* *     these are the values in 10% increments                                    * */
/* *     [11] is not the arduino pin but the number of steps used from 0 to 255    * */
/* *     just remove // sign below for the blue and white                          * */
/* *     and add // sign above for the blue and white                              * */
/* *                                                                               * */
/* ********************************************************************************* */
int bluepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };   
int whitepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };   



// int pwm_one = 10;              // extra pwm pin for future use (spare)
// int pwm_one = 9;               // extra pwm pin for future use (spare)




/* ********************************************************************************* */
/* *                                                                               * */
/* *     originally arduino pins 8, 9, 4, 5, 6, 7 are used,                        * */
/* *     I have to use different arduino non pwm pins, so I can reserved the pwm   * */
/* *     pins for other use.                                                       * */
/* *                                                                               * */
/* ********************************************************************************* */
LiquidCrystal lcd(12, 13, 4, 5, 6, 7);   
                                         



/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                 R T C   C L O C K   D S 1 3 0 7                                                  * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */
byte decToBcd(byte val)           // Convert normal decimal numbers to binary coded decimal
{
  return ( (val/10*16) + (val%10) );
}


byte bcdToDec(byte val)           // Convert binary coded decimal to normal decimal numbers
{
  return ( (val/16*10) + (val%16) );
}

                                  // 1) Sets the date and time on the ds1307
                                  // 2) Starts the clock
                                  // 3) Sets hour mode to 24 hour clock
                                  // Assumes you're passing in valid numbers

void setDateDs1307(byte second,   // 0-59
byte minute,                      // 0-59
byte hour,                        // 1-23
byte dayOfWeek,                   // 1-7
byte dayOfMonth,                  // 1-28/29/30/31
byte month,                       // 1-12
byte year)                        // 0-99
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
  Wire.send(decToBcd(minute));
  Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                  // bit 6 (also need to change readDateDs1307)
  Wire.send(decToBcd(dayOfWeek));
  Wire.send(decToBcd(dayOfMonth));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.endTransmission();
}

                                  // Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

  // A few of these need masks because certain bits are control bits
  *second = bcdToDec(Wire.receive() & 0x7f);
  *minute = bcdToDec(Wire.receive());
  *hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
  *dayOfWeek = bcdToDec(Wire.receive());
  *dayOfMonth = bcdToDec(Wire.receive());
  *month = bcdToDec(Wire.receive());
  *year = bcdToDec(Wire.receive());
}



/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                 D E F I N E  :  O N E S E C O N D                                                * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */

void onesecond() //function that runs once per second while program is running
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  lcd.setCursor(0, 0);
  if(hour>0)
  {
    if(hour<=12)
    {
      lcd.print(hour, DEC);
    }
    else
    {
      lcd.print(hour-12, DEC);
    }
  }
  else
  {
    lcd.print("12");
  }
  lcd.print(":");
  if (minute < 10) {
    lcd.print("0");
  }
  lcd.print(minute, DEC);
  lcd.print(":");
  if (second < 10) {
    lcd.print("0");
  }
  lcd.print(second, DEC);
  if(hour<12)
  {
    lcd.print("am");
  }
  else
  {
    lcd.print("pm");
  }
  lcd.print(" ");
  delay(1000);
}






/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                 S E T U P                                                                        * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */

void setup() {
  pinMode(relayPin1, OUTPUT);     // set the digital pin as output:
  pinMode(relayPin2, OUTPUT);     // set the digital pin as output:
  


/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                 S E T U P - D I S P L A Y                                                        * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  second = 56;
  minute = 57;
  hour = 23;
  dayOfWeek = 6;  		              // Sunday is 0
  dayOfMonth = 26;
  month = 2;
  year = 11;


/* ********************************************************************************* */
/* *                                                                               * */
/* *     this is where you set your time...                                        * */
/* *     1) change the second, minute, hour, etc above                             * */
/* *     2) remove // below                                                        * */
/* *     3) and load this sketch to your arduino                                   * */
/* *     4) after loading the sketch, put the // back again                        * */
/* *     5) and load this sketch again to your arduino, and save                   * */
/* *                                                                               * */
/* ********************************************************************************* */
  //setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);


  analogWrite(blue, bluemin);
  analogWrite(white, whitemin);
  lcd.begin(20, 4); // set up the LCD's number of rows and columns: 
 
  lcd.setCursor(0, 1);
  lcd.print("blue:");
  lcd.print(33*bluemin/85);
  lcd.setCursor(8, 1);
  lcd.print("white:");
  lcd.print(33*whitemin/85);  
}




/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                 L O O P                                                                          * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */




void loop()
{
  onesecond();
  relay2();
  relay1();




/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                 L O O P - D I M   F U N C T I O N                                                * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  int daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
      

  int bluerampup;
     if (daybyminute >= (ontime*60)) 
       bluerampup = (((ontime*60) + blueramptime) - daybyminute);
     else
       bluerampup = blueramptime;
       
  int whiterampup;
    if (daybyminute >= (ontime*60 + blueramptime)) 
       whiterampup = (((ontime*60) + blueramptime + whiteramptime) - daybyminute);
     else
       whiterampup = whiteramptime;

  int whiterampdown;
    if (((ontime * 60) + photoperiod + blueramptime + whiteramptime) <= daybyminute)
      whiterampdown = (((ontime*60) + photoperiod + blueramptime + 2*whiteramptime) - daybyminute);
    else
      whiterampdown = whiteramptime;
      
  int bluerampdown;
    if (((ontime * 60) + photoperiod + blueramptime + 2*whiteramptime) <= daybyminute)
      bluerampdown = (((ontime*60) + photoperiod + 2*blueramptime + 2*whiteramptime) - daybyminute);
    else
      bluerampdown = blueramptime;





/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                 L O O P - F A D E  I N                                                           * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */

 if (daybyminute >= (ontime*60))
  { 
    if (daybyminute <= ((ontime*60) + blueramptime + (whiteramptime))) //if time is in range of fade in, start fading in + (whiteramptime/10*9)
    {
      // fade blue LEDs in from min to max.
      for (int i = 1; i <= 10; i++) // setting ib value for 10% increment. Start with 0% 
      { 
        analogWrite(blue, bluepercent[i]); 
        lcd.setCursor(5, 1);
        lcd.print(i);
        lcd.print(" "); 
      
        int countdown = ((bluerampup*60)/10); // calculates seconds to next step
        while (countdown>0)
          {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      }      

      // fade white LEDs in from min to max.
      for (int i = 1; i <= 10; i++) // setting i value for 10% increment. Start with 0%
      { 
        analogWrite(white, whitepercent[i]); 
        lcd.setCursor(14, 1);
        lcd.print(i);
        lcd.print(" "); 

        int countdown = ((whiterampup*60)/10); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      } 
    }
  }

  
/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                 L O O P - M A X  V A L U E                                                       * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */

 if (daybyminute >= ((ontime * 60) + blueramptime + whiteramptime)) 
  { 
    if ( daybyminute < ((ontime * 60) + blueramptime + whiteramptime + photoperiod)) // if time is in range of photoperiod, turn lights on to maximum fade value
    {
      analogWrite(blue, 255);
        lcd.setCursor(5, 1);
        lcd.print(10);
        lcd.print(" ");
      analogWrite(white, 255); 
        lcd.setCursor(14, 1);
        lcd.print(10);
        lcd.print(" "); 
      
    } 
  }


/* ******************************************************************************************************************** */
/* *                                                                                                                  * */
/* *                                 L O O P - F A D E  O U T                                                         * */
/* *                                                                                                                  * */
/* ******************************************************************************************************************** */

  if (((ontime * 60) + photoperiod + blueramptime + whiteramptime) <= daybyminute)
  { 
    if (((ontime * 60) + photoperiod + 2*whiteramptime + 2*blueramptime) >= daybyminute)
    {
      // fade white LEDs out from max to min in increments of 1 point:
      for (int i = 10; i >= 0; i--) // setting i value for 10% increment. Start with 10%
      { 
        analogWrite(blue, 255);
        lcd.setCursor(5, 1);
        lcd.print(10);
        lcd.print(" "); 
        
        analogWrite(white, whitepercent[i]); 
        lcd.setCursor(14, 1);
        lcd.print(i);
        lcd.print(" ");  

        int countdown = ((whiterampdown*60)/10); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }

      } 

      // fade blue LEDs out from max to min in increments of 1 point:
      for (int i = 10; i >= 0; i--) // setting i value for 10% increment. Start with 10%
      { 
        analogWrite(blue, bluepercent[i]);
        lcd.setCursor(5, 1);
        lcd.print(i);
        lcd.print(" "); 

        int countdown = ((bluerampdown*60)/10); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
          relay2();
          relay1();
        }
      }
    }
  }


}  // END LOOP
 
I will take a look later tonight and see if I can suggest some changes.

A sine wave and parabola are similar over the interval we would be using, but the sine wave is the function you get from a spinning sphere. A parabola would only approximate that function-so why not just use the sine wave?
 
We are running different Wire libraries so I could not get your code to compile, but maybe this will work?


Code:
#define DS1307_I2C_ADDRESS 0x68   // address used for clock 
#include "Wire.h"                 // library used 
#include <LiquidCrystal.h>        // library used 




/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 L E D   D I M M I N G   P A R T                                                  * */ 
/* *                                 F A D E S   I N   A N D   O U T                                                  * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 



/* ********************************************************************************* */ 
/* *                                            * */ 
/* *     Note: time for ramp down is the same as time for ramp up.                 * */ 
/* *                                            * */ 
/* ********************************************************************************* */ 
int day_time = 1000 ;                 // what time do you want the lights to come on?
int night_time = 1700;                // what time do you want the lights to turn off?
int blue = 3;                     // which arduino pwm pin are you using for the blue? 
int white = 11;                   // which arduino pwm pin are you using for the white? 


int bluemin = 0 ;           
int whitemin = 0 ;  





/* ********************************************************************************* */ 
/* *                                                                               * */ 
/* *     the line below is needed if you are using meanwell ELN60-48P, for pwm     * */ 
/* *     these are the values in 10% increments                                    * */ 
/* *     [11] is not the arduino pin but the number of steps used from 0 to 255    * */ 
/* *     just remove // sign below for the blue and white                          * */ 
/* *     and add // sign above for the blue and white                              * */ 
/* *                                                                               * */ 
/* ********************************************************************************* */ 
int bluepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };    
int whitepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };    



// int pwm_one = 10;              // extra pwm pin for future use (spare) 
// int pwm_one = 9;               // extra pwm pin for future use (spare) 




/* ********************************************************************************* */ 
/* *                                                                               * */ 
/* *     originally arduino pins 8, 9, 4, 5, 6, 7 are used,                        * */ 
/* *     I have to use different arduino non pwm pins, so I can reserved the pwm   * */ 
/* *     pins for other use.                                                       * */ 
/* *                                                                               * */ 
/* ********************************************************************************* */ 
LiquidCrystal lcd(12, 13, 4, 5, 6, 7);    
                                          



/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 R T C   C L O C K   D S 1 3 0 7                                                  * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 
byte decToBcd(byte val)           // Convert normal decimal numbers to binary coded decimal 
{ 
  return ( (val/10*16) + (val%10) ); 
} 


byte bcdToDec(byte val)           // Convert binary coded decimal to normal decimal numbers 
{ 
  return ( (val/16*10) + (val%16) ); 
} 

                                  // 1) Sets the date and time on the ds1307 
                                  // 2) Starts the clock 
                                  // 3) Sets hour mode to 24 hour clock 
                                  // Assumes you're passing in valid numbers 

void setDateDs1307(byte second,   // 0-59 
byte minute,                      // 0-59 
byte hour,                        // 1-23 
byte dayOfWeek,                   // 1-7 
byte dayOfMonth,                  // 1-28/29/30/31 
byte month,                       // 1-12 
byte year)                        // 0-99 
{ 
  Wire.beginTransmission(DS1307_I2C_ADDRESS); 
  Wire.send(0); 
  Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock 
  Wire.send(decToBcd(minute)); 
  Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set 
                                  // bit 6 (also need to change readDateDs1307) 
  Wire.send(decToBcd(dayOfWeek)); 
  Wire.send(decToBcd(dayOfMonth)); 
  Wire.send(decToBcd(month)); 
  Wire.send(decToBcd(year)); 
  Wire.endTransmission(); 
} 

                                  // Gets the date and time from the ds1307 
void getDateDs1307(byte *second, 
byte *minute, 
byte *hour, 
byte *dayOfWeek, 
byte *dayOfMonth, 
byte *month, 
byte *year) 
{ 
  // Reset the register pointer 
  Wire.beginTransmission(DS1307_I2C_ADDRESS); 
  Wire.send(0); 
  Wire.endTransmission(); 

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7); 

  // A few of these need masks because certain bits are control bits 
  *second = bcdToDec(Wire.receive() & 0x7f); 
  *minute = bcdToDec(Wire.receive()); 
  *hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm 
  *dayOfWeek = bcdToDec(Wire.receive()); 
  *dayOfMonth = bcdToDec(Wire.receive()); 
  *month = bcdToDec(Wire.receive()); 
  *year = bcdToDec(Wire.receive()); 
   
} 



/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 D E F I N E  :  O N E S E C O N D                                                * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 

void onesecond() //function that runs once per second while program is running 
{ 
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); 
  lcd.setCursor(0, 0); 
  if(hour>0) 
  { 
    if(hour<=12) 
    { 
      lcd.print(hour, DEC); 
    } 
    else 
    { 
      lcd.print(hour-12, DEC); 
    } 
  } 
  else 
  { 
    lcd.print("12"); 
  } 
  lcd.print(":"); 
  if (minute < 10) { 
    lcd.print("0"); 
  } 
  lcd.print(minute, DEC); 
  lcd.print(":"); 
  if (second < 10) { 
    lcd.print("0"); 
  } 
  lcd.print(second, DEC); 
  if(hour<12) 
  { 
    lcd.print("am"); 
  } 
  else 
  { 
    lcd.print("pm"); 
  } 
  lcd.print(" "); 
  delay(1000); 
} 






/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 S E T U P                                                                        * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 

void setup() { 
  pinMode(relayPin1, OUTPUT);     // set the digital pin as output: 
  pinMode(relayPin2, OUTPUT);     // set the digital pin as output: 
   


/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 S E T U P - D I S P L A Y                                                        * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 
  Wire.begin(); 
  second = 56; 
  minute = 57; 
  hour = 23; 
  dayOfWeek = 6;                        // Sunday is 0 
  dayOfMonth = 26; 
  month = 2; 
  year = 11; 


/* ********************************************************************************* */ 
/* *                                                                               * */ 
/* *     this is where you set your time...                                        * */ 
/* *     1) change the second, minute, hour, etc above                             * */ 
/* *     2) remove // below                                                        * */ 
/* *     3) and load this sketch to your arduino                                   * */ 
/* *     4) after loading the sketch, put the // back again                        * */ 
/* *     5) and load this sketch again to your arduino, and save                   * */ 
/* *                                                                               * */ 
/* ********************************************************************************* */ 
  //setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year); 


  analogWrite(blue, bluemin); 
  analogWrite(white, whitemin); 
  lcd.begin(20, 4); // set up the LCD's number of rows and columns:  
  
  lcd.setCursor(0, 1); 
  lcd.print("blue:"); 
  lcd.print(33*bluemin/85); 
  lcd.setCursor(8, 1); 
  lcd.print("white:"); 
  lcd.print(33*whitemin/85);   
} 




/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 L O O P                                                                          * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 




void loop() 
{ 
  onesecond(); 
  relay2(); 
  relay1(); 




/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 L O O P - D I M   F U N C T I O N                                                * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); 
  int mil_time = (hour * 100) + minute;  //create military time output [0000,2400)
  int daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes 
       

   /*LED Light declarations
  
   */
  int on_time_hours, on_time_minutes, off_time_minutes, off_time_hours, intensity;
  float pwmbrightnessch1, pwmbrightnessch2, minutes_into_cycle, total_time;
  
  
  
  
  //if daytime
  if(day_time <= mil_time && mil_time < night_time){
    
    /*LED Light calculations
     
    (z/100)(4*w(/y))(-x/y)(x-y) Quadratic
    
    pwmbrightnessch1 = ((intensity - 10) * .01)*(4.0 * 255.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch2 = (intensity * .01)*(4.0 * 255.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch3 = ((intensity + 30) * .01)*(4.0 * 255.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch4 = (intensity * .01)*(4.0 * 65535.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch5 = ((intensity - 10) * .01)*(4.0 * 65535.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    
    or
    
    (z/100)*w*sin(pi*x/y)
    
    
    w = total number of PWM steps
    x = number of minutes elapsed following the time the lights turned on today
    y = total number of minutes that the light will be on today
    z = light intensity max from 1-100
  
   */
    
    
    intensity = 80; //set base intentity as an integer percentage
    
  
    total_time = (night_time / 100 - day_time / 100) * 60.0 - day_time % 100 + night_time % 100;  //total number of minutes that the light will be on today
    minutes_into_cycle = (hours - day_time / 100.0) * 60.0 + minutes - day_time % 100;  //total number of minutes that the light has been on today
    
    const float pi = 3.142;
    pwmbrightnessch1 =  (intensity * .01)   * 255.0   * sin(pi * minutes_into_cycle/total_time);
    pwmbrightnessch2 =  (intensity * .01)   * 255.0   * sin(pi * minutes_into_cycle/total_time);

    
    
    if(pwmbrightnessch1 <= 0){pwmbrightnessch1 = 1;} //durring the day, set the minimum intensity
    if(pwmbrightnessch2 <= 0){pwmbrightnessch2 = 1;}

    
    if(pwmbrightnessch1 > 255){pwmbrightnessch1 = 255;} //durring the day, set the maximum intensity
    if(pwmbrightnessch2 > 255){pwmbrightnessch2 = 255;}   
    
      
   } //end if
   
   
    analogWrite(blue, pwmbrightnessch1);  //set PWM fan intensity
    analogWrite(white, pwmbrightnessch2);



        lcd.setCursor(5, 1); 
        lcd.print(pwmbrightnessch1); 
        lcd.print(" /255");



}  // END LOOP
 
So here is the code I have now. It seems to compile okay but for the time display I just get 33:165:85pm and it never updates or changes.





Code:
#include <LiquidCrystal.h>
#define DS1307_I2C_ADDRESS 0x68   // address used for clock 
#include "Wire.h"                 // library used 






/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 L E D   D I M M I N G   P A R T                                                  * */ 
/* *                                 F A D E S   I N   A N D   O U T                                                  * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 



/* ********************************************************************************* */ 
/* *                                            * */ 
/* *     Note: time for ramp down is the same as time for ramp up.                 * */ 
/* *                                            * */ 
/* ********************************************************************************* */ 
int day_time = 1000 ;                 // what time do you want the lights to come on?
int night_time = 2100;                // what time do you want the lights to turn off?
int blue = 8;                     // which arduino pwm pin are you using for the blue? 
int white = 9;                   // which arduino pwm pin are you using for the white? 


int bluemin = 0 ;           
int whitemin = 0 ;  





/* ********************************************************************************* */ 
/* *                                                                               * */ 
/* *     the line below is needed if you are using meanwell ELN60-48P, for pwm     * */ 
/* *     these are the values in 10% increments                                    * */ 
/* *     [11] is not the arduino pin but the number of steps used from 0 to 255    * */ 
/* *     just remove // sign below for the blue and white                          * */ 
/* *     and add // sign above for the blue and white                              * */ 
/* *                                                                               * */ 
/* ********************************************************************************* */ 
int bluepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };    
int whitepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };    



// int pwm_one = 10;              // extra pwm pin for future use (spare) 
// int pwm_one = 9;               // extra pwm pin for future use (spare) 




/* ********************************************************************************* */ 
/* *                                                                               * */ 
/* *     originally arduino pins 8, 9, 4, 5, 6, 7 are used,                        * */ 
/* *     I have to use different arduino non pwm pins, so I can reserved the pwm   * */ 
/* *     pins for other use.                                                       * */ 
/* *                                                                               * */ 
/* ********************************************************************************* */ 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);    
                                          



/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 R T C   C L O C K   D S 1 3 0 7                                                  * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 
byte decToBcd(byte val)           // Convert normal decimal numbers to binary coded decimal 
{ 
  return ( (val/10*16) + (val%10) ); 
} 


byte bcdToDec(byte val)           // Convert binary coded decimal to normal decimal numbers 
{ 
  return ( (val/16*10) + (val%16) ); 
} 

                                  // 1) Sets the date and time on the ds1307 
                                  // 2) Starts the clock 
                                  // 3) Sets hour mode to 24 hour clock 
                                  // Assumes you're passing in valid numbers 

void setDateDs1307(byte second,   // 0-59 
byte minute,                      // 0-59 
byte hour,                        // 1-23 
byte dayOfWeek,                   // 1-7 
byte dayOfMonth,                  // 1-28/29/30/31 
byte month,                       // 1-12 
byte year)                        // 0-99 
{ 
  Wire.beginTransmission(DS1307_I2C_ADDRESS); 
  Wire.write(0); 
  Wire.write(decToBcd(second));    // 0 to bit 7 starts the clock 
  Wire.write(decToBcd(minute)); 
  Wire.write(decToBcd(hour));      // If you want 12 hour am/pm you need to set 
                                  // bit 6 (also need to change readDateDs1307) 
  Wire.write(decToBcd(dayOfWeek)); 
  Wire.write(decToBcd(dayOfMonth)); 
  Wire.write(decToBcd(month)); 
  Wire.write(decToBcd(year)); 
  Wire.endTransmission(); 
} 

                                  // Gets the date and time from the ds1307 
void getDateDs1307(byte *second, 
byte *minute, 
byte *hour, 
byte *dayOfWeek, 
byte *dayOfMonth, 
byte *month, 
byte *year) 
{ 
  // Reset the register pointer 
  Wire.beginTransmission(DS1307_I2C_ADDRESS); 
  Wire.write(0); 
  Wire.endTransmission(); 

  Wire.requestFrom(DS1307_I2C_ADDRESS, 7); 

  // A few of these need masks because certain bits are control bits 
  *second = bcdToDec(Wire.read() & 0x7f); 
  *minute = bcdToDec(Wire.read()); 
  *hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm 
  *dayOfWeek = bcdToDec(Wire.read()); 
  *dayOfMonth = bcdToDec(Wire.read()); 
  *month = bcdToDec(Wire.read()); 
  *year = bcdToDec(Wire.read()); 
   
} 



/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 D E F I N E  :  O N E S E C O N D                                                * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 

void onesecond() //function that runs once per second while program is running 
{ 
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); 
  lcd.setCursor(0, 3); 
  if(hour>0) 
  { 
    if(hour<=12) 
    { 
      lcd.print(hour, DEC); 
    } 
    else 
    { 
      lcd.print(hour-12, DEC); 
    } 
  } 
  else 
  { 
    lcd.print("12"); 
  } 
  lcd.print(":"); 
  if (minute < 10) { 
    lcd.print("0"); 
  } 
  lcd.print(minute, DEC); 
  lcd.print(":"); 
  if (second < 10) { 
    lcd.print("0"); 
  } 
  lcd.print(second, DEC); 
  if(hour<12) 
  { 
    lcd.print("am"); 
  } 
  else 
  { 
    lcd.print("pm"); 
  } 
  lcd.print(" "); 
  delay(1000); 
} 






/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 S E T U P                                                                        * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 

void setup() { 
 


/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 S E T U P - D I S P L A Y                                                        * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 
  Wire.begin(); 
  second = 56; 
  minute = 14; 
  hour = 20; 
  dayOfWeek = 3;                        // Sunday is 0 
  dayOfMonth = 29; 
  month = 10; 
  year = 14; 


/* ********************************************************************************* */ 
/* *                                                                               * */ 
/* *     this is where you set your time...                                        * */ 
/* *     1) change the second, minute, hour, etc above                             * */ 
/* *     2) remove // below                                                        * */ 
/* *     3) and load this sketch to your arduino                                   * */ 
/* *     4) after loading the sketch, put the // back again                        * */ 
/* *     5) and load this sketch again to your arduino, and save                   * */ 
/* *                                                                               * */ 
/* ********************************************************************************* */ 
  //setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year); 


  analogWrite(blue, bluemin); 
  analogWrite(white, whitemin); 
  lcd.begin(20, 4); // set up the LCD's number of rows and columns:  
  
  lcd.setCursor(0, 1); 
  lcd.print("Blue:"); 
  lcd.print(33*bluemin/85); 
  lcd.setCursor(0, 2); 
  lcd.print("White:"); 
  lcd.print(33*whitemin/85);   
} 




/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 L O O P                                                                          * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 




void loop() 
{ 
  onesecond(); 
 




/* ******************************************************************************************************************** */ 
/* *                                                                                                                  * */ 
/* *                                 L O O P - D I M   F U N C T I O N                                                * */ 
/* *                                                                                                                  * */ 
/* ******************************************************************************************************************** */ 
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; 
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); 
  int mil_time = (hour * 100) + minute;  //create military time output [0000,2400)
  int daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes 
       

   /*LED Light declarations
  
   */
  int on_time_hours, on_time_minutes, off_time_minutes, off_time_hours, intensity;
  float pwmbrightnessch1, pwmbrightnessch2, minutes_into_cycle, total_time;
  
  
  
  
  //if daytime
  if(day_time <= mil_time && mil_time < night_time){
    
    /*LED Light calculations
     
    (z/100)(4*w(/y))(-x/y)(x-y) Quadratic
    
    pwmbrightnessch1 = ((intensity - 10) * .01)*(4.0 * 255.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch2 = (intensity * .01)*(4.0 * 255.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch3 = ((intensity + 30) * .01)*(4.0 * 255.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch4 = (intensity * .01)*(4.0 * 65535.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch5 = ((intensity - 10) * .01)*(4.0 * 65535.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    
    
    
    or
    
    (z/100)*w*sin(pi*x/y)
    
    
    w = total number of PWM steps
    x = number of minutes elapsed following the time the lights turned on today
    y = total number of minutes that the light will be on today
    z = light intensity max from 1-100
  
   */
    
    
    intensity = 80; //set base intentity as an integer percentage
    
  
    total_time = (night_time / 100 - day_time / 100) * 60.0 - day_time % 100 + night_time % 100;  //total number of minutes that the light will be on today
    minutes_into_cycle = (hour - day_time / 100.0) * 60.0 + minute - day_time % 100;  //total number of minutes that the light has been on today
    
    const float pi = 3.142;
    pwmbrightnessch1 =  (intensity * .01)   * 255.0   * sin(pi * minutes_into_cycle/total_time);
    pwmbrightnessch2 =  (intensity * .01)   * 255.0   * sin(pi * minutes_into_cycle/total_time);

    
    
    if(pwmbrightnessch1 <= 0){pwmbrightnessch1 = 1;} //durring the day, set the minimum intensity
    if(pwmbrightnessch2 <= 0){pwmbrightnessch2 = 1;}

    
    if(pwmbrightnessch1 > 255){pwmbrightnessch1 = 255;} //durring the day, set the maximum intensity
    if(pwmbrightnessch2 > 255){pwmbrightnessch2 = 255;}   
    
      
   } //end if
   
   
    analogWrite(blue, pwmbrightnessch1);  //set PWM fan intensity
    analogWrite(white, pwmbrightnessch2);



     



}  // END LOOP
 
Did you uncomment this line and set the time?

//setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

For example:

setDateDs1307(0, 0, 8, 1, 29, 10, 2014);
 
Try printing mil_time and see what you get. I use the "time" library, so I don't call time the same way you do. Here is my entire code: (please ignore redundancies, poor variable declarations and general sloppiness)

Code:
#include <OneWire.h>
#include <LCDi2cW.h>
#include <Arduino.h>
#include <Wire.h>
#include <RTClib.h>
#include <PWM.h> //used to change the default PWM frequency and resolution
//#include <avr/wdt.h> //watchdog timer
#include <Time.h>
RTC_DS1307 RTC; //intiialize real time clock from RTClib.h
/*
Arduino Reef Controller

Analog Pin 0 = 
Analog Pin 1 = 
Analog Pin 2 = Relay 6 Refugium Light
Analog Pin 3 = Relay 5 Skimmer
Analog Pin 4 = SDA for I2C
Analog Pin 5 = SCL for I2C


Digital Pin 0 = Temp Sensor
Digital Pin 1 = Relay 1 Heater
Digital Pin 2 = Relay 2 Day Light
Digital Pin 3 = timer 2 8bit PWM Light - Blue stripe - Channel 1 - 3 Hyper Violet and 2 CREE XT-E Royal Blue (Actinic channel) (18v @ 700mA)
Digital Pin 4 = Relay 3 Return Pump
Digital Pin 5 = timer 0 8bit PWM Light - Green - Channel 2 - Royal Blue (base blue spectrum) (16.5V @ 1500mA)
Digital Pin 6 = timer 0 8bit PWM Light - Orange Stripe - Channel 3 - 3 XT-E Netural White and 2 XT-E Warm White (base white color) (16.5V @ 1500mA)
Digital Pin 7 = Relay 4 Fan
Digital Pin 8 = Alarm
Digital Pin 9 = timer 1 16bit PWM Light - Brown Stripe - Channel 4 - Turquoise / CREE XPE2 Blue (enhanced color spectrum) (17.2V @ 700mA)
Digital Pin 10 = timer 1 16bitPWM Light - Brown - Channel 5 - - 3 Hyper Violet and 2 CREE XT-E Royal Blue (Actinic channel) (18v @ 700mA)
Digital Pin 11 = timer 2 8bit PWM fan - Orange
Digital Pin 12 = Relay 7 Power Head #2
Digital Pin 13 = Relay 8 Powerhead in sump




Keypad controlls:
    A-normal mode             100
    B-feed mode               101
    C-Reset LCD Screen        102
    D-Reset temp history      103


*/
  LCDi2cW lcd = LCDi2cW(4,20,0x4C,1);  //[# of lines on LCD],[address of serial LCD controller on I2C]
  OneWire ds(0);  // Initialize DS18B20 Temp Sensor on pin 2
  
  
  
  //******************  Elements and functions for graphing history*****************************************************************
    
  int temp_hist[34];      //define temperature history array for bar graph  
    
  byte custChar[8] = {   //initialize custom character 0 is off and 1 is on.
    B00000,
    B00000,
    B00000,
    B00000,
    B00000,
    B00000,
    B00000,
    B00000};
  
  int setpoint (int row, int col) {  //sets a point in the custom character
    row = 7 - row;
    //col = 4 - col;
    bitSet (custChar[row], col);  
    return 1;
  }
  
  /* Unused
  int clearpoint (int row, int col) {  //clears a previously drawn point in the custom character
    row = 7 - row;
    col = 4 - col;
    bitClear (custChar[row], col);
    return 1;
  }
  
  */
  
  void ResetcustChar () {    //resets the custom character
    int counti = 0;
    while (counti < 8) {
      custChar[counti] = B00000;
      counti++;
    }
  }
    
    int graphHeight (int temp_hist) {  //sets the height of each bar in the graph
     int bar_height;
     if(temp_hist <= 7800){bar_height = -1;}
     if(temp_hist  > 7850){bar_height = 0;}
     if(temp_hist  > 7900){bar_height = 1;}
     if(temp_hist  > 7950){bar_height = 2;}
     if(temp_hist  > 8000){bar_height = 3;}
     if(temp_hist  > 8050){bar_height = 4;}
     if(temp_hist  > 8100){bar_height = 5;}
     if(temp_hist  > 8150){bar_height = 6;}
     if(temp_hist  > 8200){bar_height = 7;}
     if(temp_hist  > 8250){bar_height = 8;}
  
      return bar_height;
    }
  //**************************************************************************************************************************************
  
void setup(void) {
  
  InitTimersSafe();  //initialize timers
  //SetPinFrequencySafe(led, 100); //sets the frequency in Hz for the specified pin
  Timer1_SetTop(65535);  //set the resolution of timer 1
  
  for (int i = 0; i < 35; i = i + 1){ //set all values in temp history array to zero
           temp_hist[i] = 0;
         }
    
  Wire.begin(); //initialize the I2C bus
  RTC.begin(); //initialize the RTC
  // RTC.adjust(DateTime(__DATE__, __TIME__));  //set the time to the computer's current date and time
  lcd.init(); //initialize LCD
  
  //Digital Relay Pins
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(7,OUTPUT); 
  pinMode(12,OUTPUT); 
  pinMode(13,OUTPUT); 
  pinMode(A2,OUTPUT); 
  pinMode(A3,OUTPUT);
  
  //PWM
  pinMode(3, OUTPUT);  //Light Channel 1
  pinMode(5, OUTPUT);  //Light Channel 2
  pinMode(6, OUTPUT);  //Light Channel 3
  pinMode(9, OUTPUT);  //Light Channel 4
  pinMode(10, OUTPUT);  //Light Channel 5
  pinMode(11, OUTPUT);  //Fan
  
    int i;
    for (i = 0; i < 35; i = i + 1) {
      temp_hist[i] = 0;
    }
    digitalWrite(2, HIGH);   // turn on power supply for lights
    digitalWrite(7, HIGH);   // turn on chiller
    digitalWrite(4, HIGH);   // turn on Return Pump
    digitalWrite(13, HIGH);  // turn on Powerhead in sump
    digitalWrite(A3, HIGH);  // turn on Powerhead in tank
    
    
//    wdt_enable (WDTO_4S);  // reset watchdog after four seconds, if no "pat the dog" received
}  //end setup

int now_temp = 0;  //sets the current temp to zero
int High = 0; //sets an initial value for high temp (set low so it will be reset)
int Low = 9999;  //sets an initial value for low temp (set high so it will be reset)
int mode = 100; //sets initial keypad mode to normal operation
time_t feedtime = 0; //sets initial feedtime to zero (used for feed mode)
time_t manualtime = 0; //sets initial manualtime to zero (used for manual mode)
time_t start_up_time = 0; //sets initial start up time to zero (used for feed mode)
int bar_height = 0; //sets initial bar height to zero (used for graphing temp)

int day_time = 1200; //time for daytime lights to come on military time
int night_time = 2130;  //time for daytime lights to go off military time

void loop(void){  
  
  
  /* Check for keypad input *************************************************************************/
  if(lcd.keypad() != -1){
  mode = lcd.keypad();
  }  
  
  if (mode == 102){
    lcd.clear();
  }
  
  /*   Get Time *************************************************************************************/
  DateTime now = RTC.now();
    
  int minutes, hours, seconds, mil_time, years, months, days;
  time_t unix_epoch;
  hours = now.hour();  //This is in military time  [0,23]
  minutes = now.minute();
  seconds = now.second();
  years = now.year();
  months = now.month();
  days = now.day();
  unix_epoch = now.unixtime();
  mil_time = (hours * 100) + minutes;  //create military time output [0000,2400)
  
  
  /*   Get Temperature ******************************************************************************/
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      ds.reset_search();
      return;
  }
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end

  delay(750);              // maybe 750ms is enough, maybe not
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
  }

  int HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;
  LowByte = data[0];
  HighByte = data[1];
  TReading = (HighByte << 8) + LowByte;
  SignBit = TReading & 0x8000;  // test most sig bit
  if (SignBit) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
  Tc_100 = (Tc_100 * 9/5) + 3200;  //Convert to fahrenheit, comment this out to display in celcius
  Whole = Tc_100 / 100;  // separate off the whole and fractional portions
  Fract = Tc_100 % 100;
  
  
  
  /*   Use time and temp to control lights, heater, and fan in normal mode*****************************************/
  
  /*LED Light declarations
  
   */
  int on_time_hours, on_time_minutes, off_time_minutes, off_time_hours, intensity, intfan;
  float pwmbrightnessch1, pwmbrightnessch2, pwmbrightnessch3, pwmbrightnessch4, pwmbrightnessch5, minutes_into_cycle, total_time;
  
  
  
  
  //if daytime
  if(day_time <= mil_time && mil_time < night_time){
    
    /*LED Light calculations
     
    (z/100)(4*w(/y))(-x/y)(x-y) Quadratic
    
    pwmbrightnessch1 = ((intensity - 10) * .01)*(4.0 * 255.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch2 = (intensity * .01)*(4.0 * 255.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch3 = ((intensity + 30) * .01)*(4.0 * 255.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch4 = (intensity * .01)*(4.0 * 65535.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    pwmbrightnessch5 = ((intensity - 10) * .01)*(4.0 * 65535.0 / total_time)*(-1 * minutes_into_cycle/total_time)*(minutes_into_cycle - total_time);
    
    or
    
    (z/100)*w*sin(pi*x/y)
    
    
    w = total number of PWM steps
    x = number of minutes elapsed following the time the lights turned on today
    y = total number of minutes that the light will be on today
    z = light intensity max from 1-100
  
   */
    

    
    
    intensity = 80; //set base intentity as an integer percentage
    
  
    total_time = (night_time / 100 - day_time / 100) * 60.0 - day_time % 100 + night_time % 100;  //total number of minutes that the light will be on today
    minutes_into_cycle = (hours - day_time / 100.0) * 60.0 + minutes - day_time % 100;  //total number of minutes that the light has been on today
    
    const float pi = 3.142;
    pwmbrightnessch1 = (intensity) * .01   * 255.0   * sin(pi * minutes_into_cycle/total_time);
    pwmbrightnessch2 =  (intensity * .01)         * 255.0   * sin(pi * minutes_into_cycle/total_time);
    pwmbrightnessch3 = ((intensity +  20) * .01)  * 255.0   * sin(pi * minutes_into_cycle/total_time);
    pwmbrightnessch4 =  (intensity * .01)         * 65535.0 * sin(pi * minutes_into_cycle/total_time);
    pwmbrightnessch5 = (intensity * .01)   * 65535.0 * sin(pi * minutes_into_cycle/total_time);
    
    
    if(pwmbrightnessch1 <= 0){pwmbrightnessch1 = 1;} //durring the day, set the minimum intensity
    if(pwmbrightnessch2 <= 0){pwmbrightnessch2 = 1;}
    if(pwmbrightnessch3 < 0){pwmbrightnessch3 = 0;}
    if(pwmbrightnessch4 < 0){pwmbrightnessch4 = 0;}
    if(pwmbrightnessch5 < 0){pwmbrightnessch5 = 0;}
    
    if(pwmbrightnessch1 > 255){pwmbrightnessch1 = 255;} //durring the day, set the maximum intensity
    if(pwmbrightnessch2 > 255){pwmbrightnessch2 = 255;}
    if(pwmbrightnessch3 > 255){pwmbrightnessch3 = 255;}
    if(pwmbrightnessch4 > 65535){pwmbrightnessch4 = 65535;}
    if(pwmbrightnessch5 > 65535){pwmbrightnessch5 = 65535;}
    
    //set fan intensity - min 150then linear up to 255 max
    if(pwmbrightnessch3 <= 50){
      intfan = 150;
    }
    if(pwmbrightnessch3 > 50){
      intfan = 100 + pwmbrightnessch3;
    }
    if(pwmbrightnessch3 > 155){
      intfan = 255;
    }
    pwmWrite(11, intfan);
    
    
    
      
   } //end if
    
  else {   
    
    float total_night_time = (11 - night_time / 100) * 60 + night_time % 100 + 420; //total number of minutes that the light will be on at night; night time lasts from the time the light turns off until 7am
    float minutes_into_night = minutes + (hours - night_time / 100) * 60;;
    if(mil_time < night_time){
     minutes_into_night = minutes_into_night + minutes + hours * 60;
    }  
    
    int days_in_month;
    switch (months) {
    case 1:
      days_in_month = 31;
      break;
    case 2:
      days_in_month = 28;
      break;
    case 3:
      days_in_month = 31;
      break;
    case 4:
      days_in_month = 30;
      break;
    case 5:
      days_in_month = 31;
      break;
    case 6:
      days_in_month = 30;
      break;
    case 7:
      days_in_month = 31;
      break;
    case 8:
      days_in_month = 31;
      break;
    case 9:
      days_in_month = 30;
      break;
    case 10:
      days_in_month = 31;
      break;
    case 11:
      days_in_month = 30;
      break;
    case 12:
      days_in_month = 31;
      break;
    }
    
    const float pi = 3.142;   
    float night_intensity = sin(pi * days/ days_in_month);    
  
    pwmbrightnessch1 = 0;  
    pwmbrightnessch2 = 0;
    pwmbrightnessch3 = 0;
    pwmbrightnessch4 = (night_intensity * .0005) * 65535.0 * sin(pi * minutes_into_night/total_night_time + days);
    pwmbrightnessch5 = (night_intensity * .001) * 65535.0 * sin(pi * minutes_into_night/total_night_time + days);
    if(pwmbrightnessch4 < 0){pwmbrightnessch4 = -pwmbrightnessch4;}
    if(pwmbrightnessch5 < 0){pwmbrightnessch5 = -pwmbrightnessch5;}
   
   
    pwmWrite(11, 0);  //set fan intensity
    digitalWrite(2, HIGH); //turn on led light power
           
   }  //end else
   if(mil_time < 1100){
        digitalWrite(A2, HIGH); //refugium light on from midnight to until 10AM
      }
      else{
        digitalWrite(A2, LOW);
    }
   
    
    //if temp is < 78.5 deg 
      if(Tc_100 < 7850){
        digitalWrite(7, LOW);  //chiller
        digitalWrite(8, HIGH); //alarm
        digitalWrite(1, HIGH); //heater
      }
      //if 78.5 <= temp < 80.0 
      if(Tc_100 >= 7850 && Tc_100 < 8000){
        digitalWrite(8, LOW); //alarm
        digitalWrite(1, HIGH); //heater
      }
      //if 81.0 <= temp < 82.0
      if(Tc_100 >= 8100 && Tc_100 < 8200){
        digitalWrite(7, HIGH);  //chiller
        digitalWrite(1, LOW); //heater
        digitalWrite(8, LOW); //alarm
      }
      //if 82.0 <= temp < 82.5
      if(Tc_100 >= 8200 && Tc_100 < 8250){
      
      }
      //if 83.0 <= temp
      if(Tc_100 >= 8300){
        digitalWrite(8, HIGH); //alarm
      }
    
   //set the pwm values for each led channel 
   
   pwmWrite(3, pwmbrightnessch1);     //need to use analogWrite because this timer is not initialized
   analogWrite(5, pwmbrightnessch2);  //need to use analogWrite because this timer is not initialized
   analogWrite(6, pwmbrightnessch3);  //need to use pwmwWrite because this timer is initialized
   pwmWriteHR(9, pwmbrightnessch4);   //need to use pwmwWrite because this timer is set to high resolution
   pwmWriteHR(10, pwmbrightnessch5);  //need to use pwmwWrite because this timer is set to high resolution
   
   /*
   pwmWrite(3, 200);     //need to use analogWrite because this timer is not initialized
   analogWrite(5, 200);  //need to use analogWrite because this timer is not initialized
   analogWrite(6, 200);  //need to use pwmwWrite because this timer is initialized
   pwmWriteHR(9, 200);   //need to use pwmwWrite because this timer is set to high resolution
   pwmWriteHR(10, 200);  //need to use pwmwWrite because this timer is set to high resolution
   */
  
  /*    Powerheads  & Skimmer  ***************************************************************************/
  
      if (mode == 101){
      feedtime = unix_epoch; 
      }
      
      if (mode == 100){
      feedtime = 0; 
      }
      /*
      if (unix_epoch - feedtime < 600){
       digitalWrite(A3, LOW);   // Powerhead #1 
      }
      else{
      digitalWrite(A3, HIGH);   // Powerhead #1
      }
      */
      
      
      if (unix_epoch - feedtime < 1000){
       digitalWrite(4, LOW);   // Return Pump 
      }
      else{
      digitalWrite(4, HIGH);   // Return Pump
      }
      
      
      /* Unused
      if (unix_epoch - feedtime < 1800){
       digitalWrite(A2, LOW);   // Powerhead #2 
      }
      else{
      digitalWrite(A2, HIGH);   // Powerhead #2
      }
      
      */
      
      if (unix_epoch - feedtime < 2400){
       digitalWrite(12, LOW);   // Skimmer 
      }
      else{
      digitalWrite(12, HIGH);   // Skimmer
      }
   
  

  /*   Display all information on LCD *******************************************************************/

    //Display current Temp.
    lcd.setCursor(3,8);
    if (SignBit) // If its negative
    {lcd.print("-");}
    lcd.print("Now = ");
    lcd.print(Whole);
    lcd.print(".");
    if (Fract < 10){lcd.print("0");}
    lcd.print(Fract);
    lcd.write(0xDF);
    //lcd.print("F  ");
 
     int j;
     
    // Reset temp_hist array if mode chosen  
    /*
    if(mode == 103){
    for (j = 0; j < 35; j = j + 1){         
         temp_hist[j] = 0;
         }
    }
   */
   
     //Display High Temp (values in the temp_hist array)
    lcd.setCursor(1,0);
    for (j = 0; j < 35; j = j + 1){         
            if(temp_hist[j] > High){High = temp_hist[j];}
         }
    Whole = (High / 100);  // separate off the whole and fractional portions
    Fract = (High % 100);
    lcd.print("High = ");
    lcd.print(Whole, DEC);
    lcd.print(".");
    if (Fract < 10){lcd.print("0");}
    lcd.print(Fract, DEC);
    lcd.write(0xDF);
    lcd.print(" ");
 
    //Display Low Temp (values in the temp_hist array)
    lcd.setCursor(2,0);
      for (j = 0; j < 35; j = j + 1){         
            if(temp_hist[j] < Low && temp_hist[j] >5000){Low = temp_hist[j];}
         }
    Whole = (Low / 100);  // separate off the whole and fractional portions
    Fract = (Low % 100);
    lcd.print("Low  = ");
    lcd.print(Whole, DEC);
    lcd.print(".");
    if (Fract < 10){lcd.print("0");}
    lcd.print(Fract, DEC);
    lcd.write(0xDF);
    lcd.print(" ");
   
   //Display what relays are on******************************************************************************************
 /*
  lcd.setCursor(2,0); 
  if(digitalRead(13) == HIGH){lcd.print("S1 ");}
    else{lcd.print("S0 ");}
  if(digitalRead(6) == HIGH){lcd.print("H1 ");}
    else{lcd.print("H0 ");}
  if(digitalRead(7) == HIGH){lcd.print("L1 ");}
    else{lcd.print("L0 ");}  
  if(digitalRead(8) == HIGH){lcd.print("R1 ");}
    else{lcd.print("R0 ");}  
  if(digitalRead(9) == HIGH){lcd.print("F1 ");}
    else{lcd.print("F0 ");}    
  if(digitalRead(10) == HIGH){lcd.print("P1 ");}
    else{lcd.print("P0 ");}
    */
   
 // Print feed time remaining if in feed mode
 
   if (unix_epoch - feedtime < 2400){
   lcd.setCursor(0,0);  
   lcd.print(2400 - (unix_epoch - feedtime));
   lcd.print("     ");
   }
   else{
   lcd.setCursor(0,0);  
   lcd.print("  ");
   }
     
   
 // Print custom graph  
     if(start_up_time == 0){start_up_time = unix_epoch; temp_hist[0] = Tc_100;}  //first time through, set stat_time to current time
     if((unix_epoch - start_up_time) > 3600){   //if a hour has passed, move all of the array values right one space
         for (i = 34; i > 0; i = i - 1){         
           temp_hist[i] = temp_hist[(i-1)];
         }
        start_up_time = unix_epoch;             // set the starup time to the current time
        temp_hist[0] = Tc_100;                  //set the first value to the new temp value
     }
     
      ResetcustChar ();
      for (i = 0; i < 5; i = i + 1){       //set new point for data points 0-4    
           setpoint (graphHeight(temp_hist[i]), i);  //set column to new value
        } 
      lcd.load_custom_character(1, custChar);
      lcd.setCursor(3,6);
      lcd.write(1); 
      
      ResetcustChar ();
      for (i = 5; i < 10; i = i + 1){       //set new point for data points 0-4    
           setpoint (graphHeight(temp_hist[i]), i-5);  //set column to new value
        } 
      lcd.load_custom_character(2, custChar);
      lcd.setCursor(3,5);
      lcd.write(2);
      
      ResetcustChar ();
      for (i = 10; i < 15; i = i + 1){       //set new point for data points 0-4    
           setpoint (graphHeight(temp_hist[i]), i-10);  //set column to new value
        } 
      lcd.load_custom_character(3, custChar);
      lcd.setCursor(3,4);
      lcd.write(3);
      
      ResetcustChar ();
      for (i = 15; i < 20; i = i + 1){       //set new point for data points 0-4    
           setpoint (graphHeight(temp_hist[i]), i-15);  //set column to new value
        } 
      lcd.load_custom_character(4, custChar);
      lcd.setCursor(3,3);
      lcd.write(4);
      
      ResetcustChar ();
      for (i = 20; i < 25; i = i + 1){       //set new point for data points 0-4    
           setpoint (graphHeight(temp_hist[i]), i-20);  //set column to new value
        } 
      lcd.load_custom_character(5, custChar);
      lcd.setCursor(3,2);
      lcd.write(5);
      
      ResetcustChar ();
      for (i = 25; i < 30; i = i + 1){       //set new point for data points 0-4    
           setpoint (graphHeight(temp_hist[i]), i-25);  //set column to new value
        } 
      lcd.load_custom_character(6, custChar);
      lcd.setCursor(3,1);
      lcd.write(6);
      
      ResetcustChar ();
      for (i = 30; i < 35; i = i + 1){       //set new point for data points 0-4    
           setpoint (graphHeight(temp_hist[i]), i-30);  //set column to new value
        } 
      lcd.load_custom_character(7, custChar);
      lcd.setCursor(3,0);
      lcd.write(7);
      
      

   
  // Print Time 
   lcd.setCursor(0,12);
   if(now.hour() > 12){
     if((now.hour() - 12) < 10){lcd.print(" ");}
     lcd.print(now.hour() - 12);
     lcd.print(":");
     if(now.minute() < 10){lcd.print("0");}
     lcd.print(now.minute());
     lcd.print("PM");
   }
   else{
     if(now.hour() < 10){lcd.print(" ");}
     lcd.print(now.hour());
     lcd.print(":");
     if(now.minute() < 10){lcd.print("0");}
     lcd.print(now.minute());
     lcd.print("AM");
   }
   
   //Manual Mode ********************************************************************************
//Excluded to meet reefcentral character limits   



} //end main loop
 
Back
Top