Show Your Arduino Controller Sketch......

sandman3467

Member
Well lets get one centralized topic going of our own personal sketch we've slaved over to share with the rest of the world....
At present my Ver. 1.06 uses a 20x4 LCD for a display.Am running a Mega1280 currently that's set up to control 2 channels for Main LEDs,1 for moon phases,1 backlight dimming,1 fuge/sump,2 powerheads (wavemaker),1 heater,1 Heatsink fan,1 ATO. For readings I have 2 temps: Water and Heatsink,pH and ORP,
Here is the LCD Layout

~ = Space
* = Character
^ = Degree

12:23AM~**~B:10~W:10
W:79.00^F~~L:85.00^F
ORP:350mV~pH:8.20~~
Fuge~Heat~Fan~ATO:~1

But all of my current sketches can be viewed HERE with pics of the LCD.

Here's mine:

Code:
/*******************************************************************************
 *
 * Purpose: Aquarium Controller
 *
 * 
 * Arduino Version 022
 * 
 *
 *
 * Initial version 1.0   5.12.2011
 *
 *  Changes                   Date             Version   Details
 *
 * Original                   Unknown            1.00    Originally written by Christian, cptbjorn@gmail.com
 * 20x4 LCD & Temp Sensors    Unknown            1.01    Added by Tom, droidninjas@gmail.com
 * Heater & Fan Realys        5.20.2011          1.02    Addition of heater and fan relays controlled by set temp values.
 * Moon Phases                5.23.2011          1.03    Coding for Moonlights to simulate moon phases.
 * pH,ORP                     6.9.2011           1.04    Addition of pH,ORP were added and screen redesigned.
 * ATO                        6.14.2001          1.05    ATO with count added
 * Dimmable Display           7.3.2011           1.06    Addition of Display dimmed after lights out.
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *******************************************************************************/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>                 // Comunication I2C 
#define DS1307_I2C_ADDRESS 0x68 //set rtc
#include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins
#define ONE_WIRE_BUS 52 //Define the pin of the DS18B20

/************************************************* H A R D W A R E **********************************************************************/
int fan = 49;              // Fan 
int ph_1 =  32;            // PowerHead 1
int ph_2 =  33;            // PowerHead 2
int heater = 31;           // Heater
int fuge = 30;             // fuge light
int moon = 2;              // moon light 
int ato_input = 5;         // ATO Float Sensor
int ph_probe = 1;          // pH probe on Analog 1
int orp_probe = 2;         // ORP probe on Analog 2
int ato = 4;               // ATO Pump
int alarm = 35;            // Alarm
int backlight = 3;
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  R E L A Y   P A R T  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*|||||||||||||||||||||||||||||||||||||||||||||||||||  S I M P L E   O N   A N D   O F F   F E A T U R E ||||||||||||||||||||||||||||||||||||||||||||*/
int ledState1 = LOW;             
int ledState2 = HIGH; 
long previousMillis1 = 0;        
long previousMillis2 = 0;
long interval1 = 1000;          // interval at which to blink (milliseconds) for RELAY1
long interval2 = 1000;		 // interval at which to blink (milliseconds) for RELAY2
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  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  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
int blueramptime = 60 ;    // time for blue LEDs to dim on and off in minutes
int whiteramptime = 120 ;  // time for white LEDs to dim on and off in minutes
int bluemin = 0 ;          // minimmum dimming value of blue LEDs, range of 0-255
int bluemax = 255 ;        // maximum dimming value of blue LEDs, range of 0-255
int whitemin = 0 ;         // minimum dimming value of white LEDs, range of 0-255
int whitemax = 255 ;       // maximum dimming value of white LEDs, range of 0-255
int photoperiod = 360 ;    // amount of time array is on at full power in minutes
int ontime = 7 ;           // time of day (hour, 24h clock) to begin photoperiod fade in
int blue = 10;             // blue LEDs connected to digital pin 3 (pwm)
int white = 11;            // white LEDs connected to digital pin 11 (pwm)

//int bluepercent[11] = { 0, 1, 2, 5, 8 ,12, 18, 27, 44, 80, 255 };   // this line is needed if you are using meanwell ELN60-48D
//int whitepercent[11] = { 0, 1, 2, 5, 8 ,12, 18, 27, 44, 80, 255 };   // these are the values in 10% increments

int bluepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };   // this line is needed if you are using meanwell ELN60-48P
int whitepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };   // these are the values in 10% increments

/************************************************M O O N P H A S E S ********************************************************************/
int iBlueIntensity;	   //declare the integer of blue intensity
float fBlueIntensity;	   //declare the floating point version of blue intensity

/*************************************************************************** A T O *******************************************************/
int ato_time_day = 3; // Number of seconds the ATO runs each time during day time.
int ato_time_night = 3; // Number of seconds the ATO runs each time during night time.
int ato_time = 3; // Number of seconds for the ATO to run each time the switch is on.
int ato_count = 0; // Counts how many time ATO is active (ato_count X 50 ml = ml/day)
int ato_hour = 0;


#define NUMREADINGS_ph 60
int readings_ph[NUMREADINGS_ph]; // The readings from the analog input
#define NUMREADINGS_orp 60
int readings_orp[NUMREADINGS_orp]; // The readings from the analog input

int index_ph = 0; // The index of the current pH reading
int index_orp = 0; // The index of the current ORP reading

int total_orp = 0;
int total_ph = 0;

int average_orp = 0;
int average_ph = 0;
int orp_val = 350;        // Comment out after done testing
int orp_Low, orp_High, ph_val, ph_Low, ph_High, Whole, Fract;

  
LiquidCrystal lcd(22,23,24,25,26,27); //My diy circuit layout use this if you copy my board

//  Set up the custom fish icon
byte newChar[8] = {
	B00000,
	B00000,
	B10001,
	B11011,
	B11111,
	B11111,
	B11001,
	B10000
};

byte newChar1[8] = {
	B00001,
	B00010,
	B11101,
	B11010,
	B11111,
	B11110,
	B11100,
	B00000
};
byte newChar2[8] = {
	B00000,
	B00000,
	B10001,
	B11011,
	B11111,
	B11111,
	B10011,
	B00001
};
byte newChar3[8] = {
	B10000,
	B01000,
	B10111,
	B01011,
	B11111,
	B01111,
	B00111,
	B00000
};
byte newChar4[8] = {    //Moon
	B00111,
	B01110,
	B11100,
	B11000,
	B11000,
	B11100,
	B01110,
	B00111
};
byte newChar5[8] = {    //Stars
	B01010,
	B10000,
	B00010,
	B01001,
	B00100,
	B10000,
	B00100,
	B10001
};
byte newChar6[8] = {    //Left sun
	B01010,
	B00100,
	B00001,
	B11011,
	B00011,
	B00001,
	B01000,
	B10010
};
byte newChar7[8] = {    //Right sun
	B10010,
	B00100,
	B10000,
	B11011,
	B11000,
	B10000,
	B00010,
	B01001
};

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  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  :  L U N A R P H A S E |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

int moonPhase(int moonYear, int moonMonth, int moonDay)
{
  int dayFromYear, dayFromMonth;
  double julianDay;
  int phase;

  if (moonMonth < 3)			//keep the month before march
  {
    moonYear--;			//take away a year
    moonMonth += 12;		//add an extra 12 months (the year taken away from before)
  }
  ++moonMonth;
  dayFromYear = 365.25 * moonYear; //get days from current year
  dayFromMonth = 30.6 * moonMonth; //get number of days from the current month
  julianDay = dayFromYear + dayFromMonth + moonDay - 694039.09; //add them all  
  julianDay /= 29.53;		//divide by the length of lunar cycle
  phase = julianDay;		//take integer part
  julianDay -= phase;		//get rid of the int part
  phase = julianDay*8 + 0.5;	//get it between 0-8 and round it by adding .5
  phase = phase & 7;		//get a number between 1-7
  return phase; 		//1 == new moon, 4 == full moon
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  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);
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  B A C K  L I G H T  O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void backlighton()
{
  analogWrite(backlight, 255);      
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  B A C K  L I G H T   O F F |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void backlightoff()
{
  analogWrite(backlight, 50);    
}

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  F U G E  L I G H T  O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void FugeOn()
{
  digitalWrite(fuge, HIGH);  
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  F U G E  L I G H T   O F F |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void FugeOff()
{
  digitalWrite(fuge, LOW);
}

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  R E L A Y 1 O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void relay1On()  //FUNCTION TO TURN ON AND OFF RELAY 1.
{ 
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis1 > interval1) 
  { 
    previousMillis1 = currentMillis;   
    if (ledState1 == LOW)
      ledState1 = HIGH;
    else
      ledState1 = LOW;
    digitalWrite(ph_1, ledState1);
  }
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  R E L A Y 2 O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void relay2On()
{
  unsigned long currentMillis2 = millis();

  if(currentMillis2 - previousMillis2 > interval2) 
  {
    previousMillis2 = currentMillis2;   
    if (ledState2 == LOW)
      ledState2 = HIGH;
    else
      ledState2 = LOW;
    digitalWrite(ph_2, ledState2);
  }
}

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


void setup() {
  
  pinMode(ph_1, OUTPUT);        // set 
  pinMode(ph_2, OUTPUT);        // set 
  pinMode(heater, OUTPUT);      //Set Heater Relay
  pinMode(fan, OUTPUT);      // Set analog pin 0 as a output
  pinMode(fuge, OUTPUT);      // Set analog pin 1 as a output
  pinMode(moon, OUTPUT);      // Set analog pin 3 as a output
  pinMode(ato_input, INPUT); // digital pin for ATO float switch as input
  pinMode(alarm, OUTPUT); // digital pin for alarm as output
  pinMode(ato, OUTPUT); // digital pin for auto top off as output
  pinMode(backlight, OUTPUT);
  
  
  digitalWrite(alarm, LOW); // set Alarm low
  
  sensors.begin();             // Start up the DS18B20 Temp library
  
  for (int i = 0; i < NUMREADINGS_ph; i++) /* reset vector values */
    readings_ph[i] = 0;
  for (int i = 0; i < NUMREADINGS_orp; i++) /* reset vector values */
    readings_orp[i] = 0;

delay(1000);

  int High = 0;
  int Low = 10000;

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



  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year, mil_time;
  Wire.begin();
    mil_time = hour + minute; // Create military time output [0000,2400)
  // Change these values to what you want to set your clock to.
  // You probably only want to set your clock once and then remove
  // the setDateDs1307 call.
  second = 00;
  minute = 20;
  hour = 16;
  dayOfWeek = 0;  // Sunday is 0
  dayOfMonth = 12;
  month = 6;
  year = 11;
  
  //Use the next line for setting the clock
  //setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

  analogWrite(blue, bluemin);
  analogWrite(white, whitemin);
  lcd.createChar(0, newChar);
  lcd.createChar(1, newChar1);
  lcd.createChar(2, newChar2);
  lcd.createChar(3, newChar3);
  lcd.createChar(4, newChar4);
  lcd.createChar(5, newChar5);
  lcd.createChar(6, newChar6);
  lcd.createChar(7, newChar7);
    
  lcd.begin(4, 20); // set up the LCD's number of rows and columns:
   // Print a message to the LCD.
  lcd.setCursor(0, 0);
  lcd.write(0);
  lcd.write(1);
  lcd.setCursor(3, 0);// set the cursor to column 0, line 0
  lcd.print("My Marine Tank");
  lcd.setCursor(18, 0);
  lcd.write(3);
  lcd.write(2);
  lcd.setCursor(4, 2);
  lcd.print("Version 1.06");
  delay(5000);
  lcd.clear();
  lcd.setCursor(11, 0);
  lcd.print("B:");
  lcd.print(33*bluemin/85);
  lcd.setCursor(16, 0);
  lcd.print("W:");
  lcd.print(33*whitemin/85);
  lcd.setCursor(11, 1);
  lcd.print("L: ");
  lcd.setCursor(0, 1);
  lcd.print("W: ");
 
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void loop()
{
  onesecond();
    relay2On();
    relay1On();
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L U N A R |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

  float fSecond, fHour, fMinute;		//turn the times read from the RTC into float for math ops
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year, mil_time; //declare variables to hold the times
  
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); //read the RTC times
  
  int daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
 
 
  fSecond = (float) second;			//sets fSecond as the float version of the integer second from the RTC
  fMinute = (float) minute;			//same as above, but for the minute
  fHour = (float) hour;				//ditto, but for the hour
  int lunarCycle = moonPhase(year, month, dayOfMonth); //get a value for the lunar cycle
  //--------MOON LED SETUP----------//


  if ( ((hour == 7) && (minute < 00)) || (hour < 7))//Off at 730am.
  {
    fBlueIntensity = 255;		//...then we want the blue LED at the max brightness (255)
  }

  else if (hour > 17) //&& (hour < 19))             // On at 5pm
  {	
    fBlueIntensity = 255 * (((fHour - 19) + (fMinute / 59)) / 7);   //...set intensity out of 255 based on time since 17:00
  }
  else
  {
    fBlueIntensity = 0;
  }

 //---------account for the moon cycle with the blue LEDs---------//

  if (lunarCycle == 0)		//new moon
  {
    fBlueIntensity /= 2;
  }
  if ((lunarCycle == 1) || (lunarCycle == 7))	//cresent
  {
    fBlueIntensity /= 1.75 ;
  }
  if ((lunarCycle == 2) || (lunarCycle == 6))	//half moon
  {
   fBlueIntensity /= 1.5;
  }
  if ((lunarCycle == 3) || (lunarCycle == 5))	//gibbous
  {
    fBlueIntensity /= 1.25;
  }

  //full moon is full intensity 


  //----------FLOAT TO INT-------------//
  iBlueIntensity = (int) fBlueIntensity;



  //---prepare the intensities to be written to pin----//

  if (iBlueIntensity < 0) //if the blue intensity is less then 0, set it to 0
  {
    iBlueIntensity = 0;
  }



  //------------and finally, we write the values of the lights to the pins--------------//

  analogWrite(moon, iBlueIntensity);
  delay(1000);

  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  L O O P - D I M   F U N C T I O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
   
  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/10*9))) //if time is in range of fade in, start fading in + (whiteramptime/10*9)
    {
      // Turn the fuge off.
      FugeOff();
      
      backlighton();

      // 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(13, 0);
        lcd.print(i);
        lcd.print(" "); 
        //lcd.setCursor(8,1); lcd.print("Dawn");
        int countdown = ((bluerampup*60)/10); // calculates seconds to next step
        while (countdown>0)
          {
          onesecond(); // updates clock once per second
          countdown--;
        relay2On();
        relay1On();
        }
      }      

      // 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(18, 0);
        lcd.print(i);
        lcd.print(" "); 
        //lcd.setCursor(8,1); lcd.print("Dawn");
        int countdown = ((whiterampup*60)/10); // calculates seconds to next step
        while (countdown>0)
        {
          onesecond(); // updates clock once per second
          countdown--;
        relay2On();
        relay1On();
        }
      } 
    }
  }


  /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  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
    {
      // Turn the fuge off.
      FugeOff();
      
      backlighton();

      analogWrite(blue, 255);
        lcd.setCursor(13, 0);
        lcd.print(10);
        lcd.print(" ");
      analogWrite(white, 255); 
        lcd.setCursor(18, 0);
        lcd.print(10);
        lcd.print(" "); 
        //lcd.setCursor(8,1); lcd.print("Day ");
          relay2On();
          relay1On();
      
    } 
  }



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



  if (((ontime * 60) + photoperiod + blueramptime + whiteramptime) <= daybyminute)
  { 
    if (((ontime * 60) + photoperiod + whiteramptime + 2*blueramptime + (blueramptime/10*9)) >= daybyminute)
    {
       // Turn the fuge off.
      FugeOff();
      
      backlighton();
   
      // 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(13, 0);
        lcd.print(10);
        lcd.print(" "); 
        
        analogWrite(white, whitepercent[i]); 
        lcd.setCursor(18, 0);
        lcd.print(i);
        lcd.print(" ");
      //lcd.setCursor(8,1); lcd.print("Dawn");  

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

      } 

      // 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(13, 0);
        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--;
          relay2On();
          relay1On();
        }
      }
    }
  }

// DS18B20 display
sensors.requestTemperatures(); // Send the command to get temperatures
delay(250);

  lcd.setCursor(13, 1);
  
float temp1=0, temp2=0;

  //lcd.print("Led Temp:");
  temp1= sensors.getTempFByIndex(1);
  lcd.print(sensors.getTempFByIndex(1)); 
  lcd.print((char)223);
  lcd.print("F");
 
  lcd.setCursor(2, 1);
  //lcd.print("Tank Temp: ");
  temp2= sensors.getTempFByIndex(0);
  lcd.print(sensors.getTempFByIndex(0)); 
  lcd.print((char)223);
  lcd.print("F");

if ( (temp1) > 98)
    {
    digitalWrite(fan, HIGH);
    digitalWrite(alarm, HIGH);
    delay(1000);
    digitalWrite(alarm, LOW);
    }
else if ( (temp1) < 92)
    {
    digitalWrite(fan, LOW);
    }

if ( (temp2) < 78 )
    {
    digitalWrite(heater, HIGH);
    digitalWrite(alarm, HIGH);
    delay(1000);
    digitalWrite(alarm, LOW);
    }
else if ( (temp2) > 82 )
    {
    digitalWrite(heater, LOW);
    }   
//  lcd.print(sensors.getTempCByIndex(0));
//  lcd.print((char)223); 
//  lcd.print("C");
  


// ATO count *************************************************************/
    lcd.setCursor(14,3);
    lcd.print("ATO:");
    lcd.setCursor(18,3);
    if(ato_count < 10) {
    lcd.print(" ");
    lcd.print(ato_count, DEC);
    if(digitalRead(ato_input) == LOW && ato_hour != hour){
    digitalWrite(alarm, HIGH);
    delay(500);
    digitalWrite(alarm, LOW);
    digitalWrite(ato, HIGH);
    if(hour > 7 && hour < 22){
    delay(ato_time_day * 1000); // Day time dosing with kalkwasser
    }
    else {
    delay(ato_time_night * 1000); // Night time dosing with kalkwasser
    }
    digitalWrite(ato, LOW); // Turn off ATO Pump
    ato_hour = hour; // Only allow the ATO to run once per hour / Overflow protection good for dosin kalkwasser
    ato_count = ato_count + 1;
      }
    }

/* ORP on line 1 ****************************************************************************************/
if((second % 10) == 0) {
total_orp -= readings_orp[index_orp]; // subtract the last reading
readings_orp[index_orp] = analogRead(orp_probe); // read from the sensor
total_orp += readings_orp[index_orp]; // add the reading to the total
index_orp = (index_orp + 1); // advance to the next index
if (index_orp >= NUMREADINGS_orp) { // if we're at the end of the array...
index_orp = 0;
} // ...wrap around to the beginning
average_orp = total_orp / NUMREADINGS_orp; // calculate the average
orp_val = (1.15 * average_orp);
orp_Low = min(orp_Low, orp_val);
orp_High = max(orp_High, orp_val);
}

lcd.setCursor(1,2);
lcd.print("ORP:");
lcd.setCursor(5,2);
lcd.print(orp_val);
lcd.setCursor(8,2);
lcd.print("mV");


/***********PH Probe**********************************************************************************************/
// To calibrate ph probe set 7ph to 2V and 10PH to 1V

total_ph -= readings_ph[index_ph]; // subtract the last reading
readings_ph[index_ph] = analogRead(ph_probe); // read from the sensor
total_ph += readings_ph[index_ph]; // add the reading to the total
index_ph = (index_ph + 1); // advance to the next index
if (index_ph >= NUMREADINGS_ph) { // if we're at the end of the array...
index_ph = 0;
} // ...wrap around to the beginning
average_ph = total_ph / NUMREADINGS_ph; // calculate the average
ph_val = (-1.47 * average_ph + 1300); // ph is stored 100 times value
ph_Low = min(ph_Low, ph_val);
ph_High = max(ph_High, ph_val);
Whole = (ph_val / 100); // separate off the whole and fractional portions
Fract = (ph_val % 100);

lcd.setCursor(11,2);
lcd.print("pH:");
if (Whole >= 10) {
Whole = 9;
}
lcd.print(Whole, DEC);
lcd.print(".");
if (Fract < 10){
lcd.print("0");
}
lcd.print(Fract, DEC);


  //*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  Night Time |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

if (((ontime * 60) + photoperiod + (2 * blueramptime) + (2 * whiteramptime)) < daybyminute)
  {         

    
    backlightoff();
    // Turn Fuge On
    FugeOn();
  }

    //Display what relays are on******************************************************************************************
  
 lcd.setCursor(0,3);
  if(digitalRead(fuge) == HIGH){lcd.print("Fuge");}
    else{lcd.print("");}
 lcd.setCursor(5,3);
  if(digitalRead(heater) == HIGH){lcd.print("Heat");}
    else{lcd.print("");}
 lcd.setCursor(10,3);
  if(digitalRead(fan) == HIGH){lcd.print("Fan");}
    else{lcd.print("");}

  
}
 

sandman3467

Member
For those of you asking about a simple controller I have made up a shopping list for a basic controller with enclosure and 16x2 LCD that will control the following and leave open 6 extra pins....
2 Temp Sensors (Water and Heatsink)
2 powerheads
1 fan (temp sensor controlled)
1 heater (temp sensor controlled)
1 moonlight (on/off)
1 fuge (on/off)
1 White LEDs
1 Blue LEDs

If your using meanwell drivers you would also need to buy a pkg of 2N2222 Transistors and a 10v DC wallwart to be able to use the 0-5v signal for a 0-10v driver.

Shopping List for basic lighting control

Uno
RTC
Enclosure
Power Supply
Waterproof Temp Sensor
Heatsink Temp Sensor
LCD 16x2 - White on Blue Bkgnd
I2C LCD Controller You will need to download the new Liquidcrystal library from them for this to work.
8 Relay Board 2 powerheads,1 fuge,1 sump, 1 Heater, 1 Fan, 1 moonlight
2-4 - 110 Outlets
1 - 8x7 Radioshack Project Box to hold relays and outlets.
Lots of wires....lol

And Now your Sketch:

Code:
/*******************************************************************************
 *
 * Purpose: Opensource Aquarium Controller
 *
 * 
 *
 * Initial Version 1.0   
 *
 *
 *  Changes                   Date             Version   Details
 *
 * Original                   Unknown            1.00    Originally written by Christian, cptbjorn@gmail.com
 * 20x4 LCD & Temp Sensors    Unknown            1.01    Added by Tom, droidninjas@gmail.com
 * Heater & Fan Realys        5.20.2011          1.02    Added by Martin, liquidartstattoos@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *******************************************************************************/

#include <OneWire.h>
#include <DallasTemperature.h>
#include "Wire.h" 
#define DS1307_I2C_ADDRESS 0x68 //set rtc
#include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins
#define ONE_WIRE_BUS 9 //Define the pin of the DS18B20


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



const int ledPin1 =  A2;          // pin number for relay 1 was digital pin 2
const int ledPin2 =  A3;          // pin number for relay 2 was digital pin 8


int ledState1 = LOW;             
int ledState2 = LOW; 
long previousMillis1 = 0;        
long previousMillis2 = 0;
long interval1 = 30000;          // interval at which to blink (milliseconds) for RELAY1
long interval2 = 50000;		 // interval at which to blink (milliseconds) for RELAY2


/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  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  |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/



int blueramptime = 60 ;    // time for blue LEDs to dim on and off in minutes
int whiteramptime = 120 ;  // time for white LEDs to dim on and off in minutes
int bluemin = 0 ;          // minimmum dimming value of blue LEDs, range of 0-255
int bluemax = 255 ;        // maximum dimming value of blue LEDs, range of 0-255
int whitemin = 0 ;         // minimum dimming value of white LEDs, range of 0-255
int whitemax = 255 ;       // maximum dimming value of white LEDs, range of 0-255
int photoperiod = 360 ;    // amount of time array is on at full power in minutes
int ontime = 10 ;          // time of day (hour, 24h clock) to begin photoperiod fade in
int blue = 10;              // blue LEDs connected to digital pin 3 (pwm)
int white = 11;            // white LEDs connected to digital pin 11 (pwm)
int fan = A0;            // Fan power relay connected to analog pin 0
int fuge = 4;            // fuge light power relay connected to analog pin 1
int moon = 3;            // moon light connected to digital pin 3
int heater = A1;



int bluepercent[11] = { 0, 1, 2, 5, 8 ,12, 18, 27, 44, 80, 255 };   // this line is needed if you are using meanwell ELN60-48D
int whitepercent[11] = { 0, 1, 2, 5, 8 ,12, 18, 27, 44, 80, 255 };   // these are the values in 10% increments

//int bluepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };   // this line is needed if you are using meanwell ELN60-48P
//int whitepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };   // these are the values in 10% increments

//int pwm_one = 9;        // extra pwm pin for future use

// Connect via i2c, default address #0 (A0-A2 not jumpered)
LiquidCrystal lcd(0);
//LiquidCrystal lcd(13, 12, 7, 6, 5, 4); //My diy circuit layout use this if you copy my board
//LiquidCrystal lcd(11, 12, 13, 14, 15, 16); //Teensy++ 2.0 alternate to Arduino boards
//LiquidCrystal lcd(12, 13, 4, 5, 6, 7);   // typically 8, 9, 4, 5, 6, 7
                                         // have to change to free up more pwm pins
//  Set up the custom fish icon
byte newChar[8] = {
	B00000,
	B00000,
	B10001,
	B11011,
	B11111,
	B11111,
	B11001,
	B10000
};

byte newChar1[8] = {
	B00001,
	B00010,
	B11101,
	B11010,
	B11111,
	B11110,
	B11100,
	B00000
};
byte newChar2[8] = {
	B00000,
	B00000,
	B10001,
	B11011,
	B11111,
	B11111,
	B10011,
	B00001
};
byte newChar3[8] = {
	B10000,
	B01000,
	B10111,
	B01011,
	B11111,
	B01111,
	B00111,
	B00000
};
byte newChar4[8] = {
	B00111,
	B01110,
	B11100,
	B11000,
	B11000,
	B11100,
	B01110,
	B00111
};
byte newChar5[8] = {
	B01010,
	B10000,
	B00010,
	B01001,
	B00100,
	B10000,
	B00100,
	B10001
};
byte newChar6[8] = {
	B01010,
	B00100,
	B00001,
	B11011,
	B00011,
	B00001,
	B01000,
	B10010
};
byte newChar7[8] = {
	B10010,
	B00100,
	B10000,
	B11011,
	B11000,
	B10000,
	B00010,
	B01001
};

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  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);
}


/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  F A N S   O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void FansOn()
{
  digitalWrite(fan, HIGH);  
  //analogWrite(fan, 255);
    
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  F A N S   O F F |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void FansOff()
{
  digitalWrite(fan, LOW);
    //analogWrite(fan, 0);
    
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  F U G E  L I G H T  O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void FugeOn()
{
  digitalWrite(fuge, HIGH);  

    
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  F U G E  L I G H T   O F F |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void FugeOff()
{
  digitalWrite(fuge, LOW);
   
    
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  M O O N  L I G H T  O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void MoonOn()
{
  digitalWrite(moon, HIGH);  
  lcd.setCursor(8, 0);
  lcd.write(4);
  lcd.write(5);
  
    
}
/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  M O O N  L I G H T   O F F |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void MoonOff()
{
  digitalWrite(moon, LOW);
  lcd.setCursor(8, 0);
  lcd.write(6);
  lcd.write(7);
   
    
}

/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  R E L A Y 1 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void relay1()  //FUNCTION TO TURN ON AND OFF RELAY 1.
{ 
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis1 > interval1) 
  { 
    previousMillis1 = currentMillis;   
    if (ledState1 == LOW)
      ledState1 = HIGH;
    else
      ledState1 = LOW;
    digitalWrite(ledPin1, ledState1);
  }
}



/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  D E F I N E  :  R E L A Y 2 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/




void relay2()
{
  unsigned long currentMillis2 = millis();

  if(currentMillis2 - previousMillis2 > interval2) 
  {
    previousMillis2 = currentMillis2;   
    if (ledState2 == LOW)
      ledState2 = HIGH;
    else
      ledState2 = LOW;
    digitalWrite(ledPin2, ledState2);
  }
}




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


void setup() {
  pinMode(ledPin1, OUTPUT);    // set the digital pin as output:
  pinMode(ledPin2, OUTPUT);    // set the digital pin as output:
  pinMode(fan, OUTPUT);      // Set analog pin 0 as a output
  pinMode(fuge, OUTPUT);      // Set analog pin 1 as a output
  pinMode(moon, OUTPUT);      // Set analog pin 3 as a output
  pinMode(heater, OUTPUT);
  pinMode(fan, OUTPUT);
  sensors.begin();             // Start up the DS18B20 Temp library

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



  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();

  // Change these values to what you want to set your clock to.
  // You probably only want to set your clock once and then remove
  // the setDateDs1307 call.
  second = 00;
  minute = 15;
  hour = 23;
  dayOfWeek = 25;  // Sunday is 0
  dayOfMonth = 4;
  month = 4;
  year = 11;
  
  //Use the next line for setting the clock
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);

  analogWrite(blue, bluemin);
  analogWrite(white, whitemin);
  lcd.createChar(0, newChar);
  lcd.createChar(1, newChar1);
  lcd.createChar(2, newChar2);
  lcd.createChar(3, newChar3);
  lcd.createChar(4, newChar4);
  lcd.createChar(5, newChar5);
  lcd.createChar(6, newChar6);
  lcd.createChar(7, newChar7);
  lcd.begin(20, 4); // set up the LCD's number of rows and columns: 
  lcd.setCursor(0, 0);
  lcd.setCursor(0, 0);
  lcd.write(0);
  lcd.write(1);
  lcd.setCursor(3, 0);// set the cursor to column 0, line 0
  lcd.print("My Marine Tank");
  lcd.setCursor(18, 0);
  lcd.write(3);
  lcd.write(2);
  lcd.setCursor(4, 1);
  lcd.print("Version 1.01");
  delay(5000);
  lcd.clear();
  //lcd.setCursor(7, 1);
  //lcd.print("M:");
  lcd.setCursor(11, 0);
  lcd.print("B:");
  lcd.print(33*bluemin/85);
  lcd.setCursor(16, 0);
  lcd.print("W:");
  lcd.print(33*whitemin/85);
  lcd.setCursor(11, 1);
  lcd.print("L: ");
  lcd.setCursor(0, 1);
  lcd.print("W: ");

 
}




/*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  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/10*9))) //if time is in range of fade in, start fading in + (whiteramptime/10*9)
    {
      // Turn the fans on.
      FansOn();
      // Turn the fuge off.
      FugeOff();
      // Turn the moon lights off.
      MoonOff();
      // 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(13, 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(18, 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
    {
      // Turn the fans on.
      FansOn();
      // Turn the fuge off.
      FugeOff();
      // Turn the moon lights off.
      MoonOff();
      
      analogWrite(blue, 255);
        lcd.setCursor(13, 1);
        lcd.print(10);
        lcd.print(" ");
      analogWrite(white, 255); 
        lcd.setCursor(18, 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 + whiteramptime + 2*blueramptime + (blueramptime/10*9)) >= daybyminute)
    {
      // Turn the fans on.
      FansOn();
      // Turn the fuge off.
      FugeOff();
      // Turn the moon lights off.
      MoonOff();
      
      // 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(13, 1);
        lcd.print(10);
        lcd.print(" "); 
        
        analogWrite(white, whitepercent[i]); 
        lcd.setCursor(18, 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(13, 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();
        }
      }
    }
  }

// DS18B20 display
sensors.requestTemperatures(); // Send the command to get temperatures
delay(250);
float temp1=0, temp2=0;
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(13, 1);

  //lcd.print("Led Temp:");
  lcd.print(sensors.getTempFByIndex(1)); 
  lcd.print((char)223);
  lcd.print("F");
 
  lcd.setCursor(2, 1);
  //lcd.print("Tank Temp: ");
  lcd.print(sensors.getTempFByIndex(0)); 
  lcd.print((char)223);
  lcd.print("F");
  
  if ( (temp1) > 98)
    {
    digitalWrite(fan, HIGH);
    }
else if ( (temp1) < 92)
    {
    digitalWrite(fan, LOW);
    }

if ( (temp2) < 78 )
    {
    digitalWrite(heater, HIGH);
    }
else if ( (temp2) > 82 )
    {
    digitalWrite(heater, LOW);
    }
   
//  lcd.print(sensors.getTempCByIndex(0));
//  lcd.print((char)223); 
//  lcd.print("C");
  
  //delay(500);
  //*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||  Night Time |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/

if (((ontime * 60) + photoperiod + (2 * blueramptime) + (2 * whiteramptime)) < daybyminute)
  {         
      
    FansOff();
    FugeOn();
    MoonOn();
  }
// if (daybyminute < (ontime*60))
//{
//
//    FansOff();
//  
//}
  
}  // END LOOP
 

Woodstock

The Wand Geek was here. ;)
RS STAFF
Nice work! Thanks for sharing it with us!
I'll have to show this thread to my husband. He is an embedded systems designer (as well as other skilled trades) and is a geek when it comes to this stuff. He has been wanting to make a controller for my systems for a long time...
 
Top