1st half Code:
/*
Set RTC. Display on LCD. LED fade Up and Down. Fan On and Off.
originally written by Christian, cptbjorn@gmail.com
*/
/*
Water topoff code. Looking for value at inPin every 60 seconds and turn
on/off base on inPin value.
code have 90 seconds timeout to shutoff pump
originally written by Xenia2, Reef Central
*/
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 //set rtc
#include // initialize the library with the numbers of the interface pins
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| A U T O T O P O F F |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
unsigned long relay_on_time;
unsigned long loopTime;
unsigned long shutDownTime=0; //
int atoPin= 2; // ATO relay connected to digital pin 2
int opticalPin = A3; // sensor input source connected to digital pin A3
int val = 0; // variable to store the read value
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 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 whiteramptime = 240 ; // time for white LEDs to dim on and off in minutes
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 = 240 ; // amount of time array is on at full power in minutes
int ontime = 9 ; // time of day (hour, 24h clock) to begin photoperiod fade in
int white = 11; // white LEDs connected to digital pin 11 (pwm)
int fan = 4; // defines pin 4 as fan
int whitepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 }; // these are the values in 10% increments
// int pwm_one = 5; // extra pwm pin for future use
// int pwm_one = 6; // extra pwm pin for future use
// int pwm_one = 3; // extra pwm pin for future use
LiquidCrystal lcd(7, 8, 9, 10, 13, 12); // typically 8, 9, 4, 5, 6, 7
// have to change to free up more pwm pins
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 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 O N |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void FanOn()
{
digitalWrite(fan, HIGH);
//analogWrite(fan, 255);
}
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| D E F I N E : F A N O F F |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void FanOff()
{
digitalWrite(fan, LOW);
//analogWrite(fan, 0);
}
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| S E T U P |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void setup()
{
relay_on_time = millis();
loopTime = relay_on_time;
pinMode(atoPin, OUTPUT); // sets the digtial pin 2 as output
pinMode(opticalPin, INPUT); // sets the digital pin A3 as input
pinMode(fan, OUTPUT); // set digital pin 4 as a output
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 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 = 15;
minute = 19;
hour = 16;
dayOfWeek = 0; // Sunday is 0
dayOfMonth = 12;
month = 2;
year = 12;
//setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
analogWrite(white, whitemin);
lcd.begin(16, 2); // set up the LCD's number of rows and columns:
// lcd.print("12:00 80.6"); // Print a message to the LCD.
// lcd.print(char(223));
lcd.setCursor(11, 0);
lcd.print("ATO:");
lcd.setCursor(8, 1);
lcd.print("LED:");
lcd.print(33*whitemin/85);
}
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| L O O P |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
void loop()
{
onesecond();
relay_on_time = millis();
if (relay_on_time >= (loopTime + 60000)) // check status of input every 60 seconds
{
val = digitalRead(opticalPin);
if (val == HIGH)
{
lcd.setCursor(15, 0);
lcd.print("+"); //amber LED on
// <<< changed to LCD "+"
digitalWrite(atoPin, HIGH);
if ( 0 == shutDownTime)
shutDownTime = relay_on_time + 90000;
}
if (val == LOW)
{
lcd.setCursor(15, 0);
lcd.print("-"); // green LED on
// <<< changed to LCD "-"
digitalWrite (atoPin, LOW);
shutDownTime = 0;
}
if ( shutDownTime && shutDownTime <= relay_on_time )
{
digitalWrite(atoPin, LOW);
while(1)
{
// wait for reset
lcd.print("X"); // red LED on
// <<< changed to LCD "X" whole screen,
// lights turn off/ fan off, wait for reset
}
}
loopTime = relay_on_time;
}