/* ------------------------------
* 0 HEATER A0 FAN
* 1 ATS A1 24V
* 2 LCD 14-D7 A2
* 3 LCD 13-D6 A3
* 4 LCD 12-D5 A4 RTC
* 5 LCD 11-D4 A5 RTC
* 6 Moon
* 7
* 8 TEMP IC2
* 9 LED B PWM
* 10 LED W PWM
* 11 LCD E
* 12 LCD RS
* -------------------------------
**********************************************************************************/
//#include
//#include
//#include "Wire.h"
//#define DS1307_I2C_ADDRESS 0x68 // Set rtc
//#include // initialize the library with the numbers of pins
//#define ONE_WIRE_BUS 2 // Define the pin of the DS18B20
#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 8 //Define the pin of the DS18B20
/*||||||||||||||||||| L E D D I M M I N G P A R T ||||||||||||||||||||||||||*/
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 = 610 ; // amount of time array is on at full power in minutes
int ontime = 11 ; // when start photoperiod fade in
int ramptime = 60 ; // time for LEDs to dim on and off in minutes
/*||||||||||||||||||||||||||||||||| P I N ||||||||||||||||||||||||||||||||||||||*/
int blue = 9; // blue LEDs connected to digital pin 11 (pwm)
int white = 10; // white LEDs connected to digital pin 10 (pwm)
int fan = A0;
int ats = 1;
int heater = 0;
int light = A1;
int moon = 6;
int backlight = 1;
int iBlueIntensity; //declare the integer of blue intensity
float fBlueIntensity; //declare the floating point version of blue intensity
int bluepercent[] = { 0, 2, 3, 4, 5, 7, 11, 15, 26, 52, 78, 100, 128, 154, 180, 205, 230, 255, 255, 255, 255, 255 };
int whitepercent[]= { 0, 0, 0, 0, 2, 3, 4, 8, 18, 40, 54, 75, 99, 110, 130, 155, 175, 200, 210, 230, 255, 255 };
int abc(sizeof(bluepercent)/sizeof(bluepercent[0]));
LiquidCrystal lcd(12,11,5,4,3,2); // initialize the library with the numbers of the interface pins
byte newChar[8] = { // Set up the custom icon saule leidzias ir kyla ( 0 ir 1 kyla) (2 ir 3 leidzias) (4 ir 5 menulis )
B01001,
B01001,
B00101,
B00101,
B10101,
B10111,
B01111,
B11111
};
byte newChar1[8] = {
B10010,
B10010,
B10100,
B10100,
B10101,
B11101,
B11110,
B11111
};
byte newChar2[8] = {
B11111,
B01111,
B10111,
B10101,
B00101,
B00101,
B01001,
B01001
};
byte newChar3[8] = {
B11111,
B11110,
B11101,
B10101,
B10100,
B10100,
B10010,
B10010
};
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
};
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
/*||||||||||||||||||||||||||||||||||||||| 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) );
}
void setDateDs1307(byte second,
byte minute,
byte hour,
byte dayOfWeek,
byte dayOfMonth,
byte month,
byte year)
{
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));
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.endTransmission();
}
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
*second = bcdToDec(Wire.receive() & 0x7f);
*minute = bcdToDec(Wire.receive());
*hour = bcdToDec(Wire.receive() & 0x3f);
*dayOfWeek = bcdToDec(Wire.receive());
*dayOfMonth = bcdToDec(Wire.receive());
*month = bcdToDec(Wire.receive());
*year = bcdToDec(Wire.receive());
}
/*|||||||||||||||||||||||||||||||||||| D E F I N E : B A C K L I G H T ||||||||||||||||||||||||||||||*/
void backlighton()
{
analogWrite(backlight, 200);
}
void backlightoff()
{
analogWrite(backlight, 25); // If too bright or dim adjust the value accordingly.
}
/*|||||||||||||||||||||||||||||||||| 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, 1);
if(hour>0)
{
if(hour<=12)
{
lcd.print(hour, DEC);
}
else
{
lcd.print(hour, 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);
delay(1000);
}
/*|||||||||||||||||||||||||| D E F I N E : 24 V O N - O F |||||||||||||||||||||||||||||||||*/
void LightOn()
{
digitalWrite(light, HIGH);
lcd.setCursor(3, 3);
lcd.print("+");
}
void LightOff()
{
digitalWrite(light, LOW);
lcd.setCursor(3, 3);
lcd.print("-");
}
/*|||||||||||||||||||||||||| D E F I N E : A T S O N - O F |||||||||||||||||||||||||||||||||*/
void AtsOn()
{
digitalWrite(ats, LOW);
lcd.setCursor(19, 3);
lcd.print("+");
}
void AtsOff()
{
digitalWrite(ats, HIGH);
lcd.setCursor(19, 3);
lcd.print("-");
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Define Temperature<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
void Temperature()
{
sensors.requestTemperatures(); // Send the command to get temperatures
delay(750);
lcd.setCursor(13, 2);
float temp1=0, temp2=0;
temp1=sensors.getTempFByIndex(1);
lcd.print(sensors.getTempFByIndex(1));
lcd.print((char)223);
lcd.print("F");
lcd.setCursor(2, 2);
temp2=sensors.getTempFByIndex(0);
lcd.print(sensors.getTempFByIndex(0));
lcd.print((char)223);
lcd.print("F");
if ( (temp2) > 79.7)
{
digitalWrite(fan, HIGH);
lcd.setCursor(8, 3);
lcd.print("+");
}
else if ( (temp1) > 113)
{
digitalWrite(fan, HIGH);
lcd.setCursor(8,3);
lcd.print("+");
}
else if ((temp2) < 79.5)
{
digitalWrite(fan, LOW);
lcd.setCursor(8,3);
lcd.print("-");
}
if ( (temp2) < 75.5 )
{
digitalWrite(heater, HIGH);
lcd.setCursor(14,3);
lcd.print("+");
}
else if ( (temp2) > 76.28 )
{
digitalWrite(heater, LOW);
lcd.setCursor(14,3);
lcd.print("-");
}
}
/*||||||||||||||||||||||||||||||||||||| S E T U P P I N ||||||||||||||||||||||||||||||||||||||||||||*/
void setup() {
pinMode(heater, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(light, OUTPUT);
pinMode(ats, OUTPUT);
pinMode(moon, OUTPUT);
pinMode(backlight, OUTPUT);
pinMode (A3, OUTPUT);
digitalWrite (A3, HIGH); //sets pin A3 as a + for the RTC
pinMode (A2, OUTPUT);
digitalWrite (A2, LOW); //sets pin A3 as a - for the RTC
sensors.begin(); // Start up the DS18B20 Temp library
/*||||||||||||||||||||||||||||||| S E T U P - C L O C K ||||||||||||||||||||||||||||||||||||||||*/
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
second = 00;
minute = 25;
hour = 20;
dayOfWeek = 5; // Sunday is 0
dayOfMonth = 06;
month = 07;
year = 12;
//setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year); // ******* set the clock *******
// 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.
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 LCD as a 20x4 display
/*
lcd.setCursor(0, 0);
lcd.write(0);
lcd.write(1);
lcd.setCursor(18, 0);
lcd.write(3);
lcd.write(2);
*/
lcd.setCursor(11, 1);
lcd.print("B:");
lcd.print(33*bluemin/85);
lcd.setCursor(16, 1);
lcd.print("W:");
lcd.print(33*whitemin/85);
lcd.setCursor(11, 2);
lcd.print("L: ");
lcd.setCursor(0, 2);
lcd.print("W: ");
lcd.setCursor(0, 3);
lcd.print("24V");
lcd.setCursor(5, 3);
lcd.print("Fan");
lcd.setCursor(11, 3);
lcd.print("Htr");
lcd.setCursor(16, 3);
lcd.print("Ats");
}
/*|||||||||||||||||||@@@@@@@@@@@@@@@@@@@@@@@@@@@||||| L O O P |||||||@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@||||||||||||||||||||||*/
void loop()
{
onesecond();
/*||||||||||||||||||||||||||||||||||||||||||||||| 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; //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
Serial.begin(9600);
Serial.println (fBlueIntensity);
Serial.println (lunarCycle);
//--------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 > 22) //&& (hour < 19)) // On at 10pm
else if (hour > 18) //&& (hour < 19)) // On at 5pm
{
fBlueIntensity = 255 * (((fHour - 16) + (fMinute / 59)) / 7); //...set intensity out of 255 based on time since 17:00
Serial.println (fHour);
Serial.println (fMinute);
Serial.println (fSecond);
}
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;
}
analogWrite(moon, iBlueIntensity);
lcd.setCursor(6, 0);
lcd.print("Moon:");
lcd.setCursor(12, 0);
lcd.print(iBlueIntensity);
//delay(1000); // why is this here?
/*|||||||||||||||||||||||||||||||||||||||| T E M P E R A T U R E DS18B20 |||||||||||||||||||||||||||||||||||*/
Temperature();
/*|||||||||||||||||||||||||||||||||||||||||||||||||||| ATS |||||||||||||||||||||||||||||||||||||||||||||||||*/
if (hour >= 11) // butu 14 val
{
if ( hour < 23) AtsOff();
}
else
{
AtsOn();
}
/*|||||||||||||||||||||||||||||||| R A M P T I M E C A L C U L A T I O N ||||||||||||||||||||||||||||||||*/
int rampup;
if (daybyminute >= (ontime*60))
rampup = (((ontime*60) + ramptime) - daybyminute);
else
rampup = ramptime;
int rampdown;
if (((ontime * 60) + photoperiod + ramptime) <= daybyminute)
rampdown = (((ontime*60) + photoperiod + 2*ramptime) - daybyminute);
else
rampdown = ramptime;
/*|||||||||||||||||||||||||||||||||||||||||| F A D E I N ||||||||||||||||||||||||||||||||||||||||||||||*/
if (daybyminute >= (ontime*60))
{ if (daybyminute < ((ontime*60) + ramptime))
{
LightOn();
backlighton();
lcd.setCursor(0, 0);
lcd.write(0);
lcd.write(1);
lcd.setCursor(18, 0);
lcd.write(0);
lcd.write(1);
int i;
for (int i = 0; i < abc; i++)
{
analogWrite(blue, bluepercent[i]);
analogWrite(white, whitepercent[i]);
lcd.setCursor(13, 1);
lcd.print((bluepercent[i]*99)/255);
if (i < 10) lcd.print(" ");
lcd.setCursor(18, 1);
lcd.print((whitepercent[i]*99)/255);
if (i < 10) lcd.print(" ");
int countdown = ((rampup*60)/abc);
while (countdown>0)
{
onesecond();
countdown--;
Temperature();
lcd.setCursor(6, 1);
if (countdown < 100) lcd.print ("0");
if (countdown < 10) lcd.print ("0");
lcd.print(countdown);
}
}
}
}
/*||||||||||||||||||||||||||||||||||||||||||||||| M A X |||||||||||||||||||||||||||||||||||||||||||||*/
if ( daybyminute >= ((ontime * 60) + ramptime))
{
if ( daybyminute < ((ontime * 60) + ramptime + photoperiod ))
{
LightOn();
backlighton();
AtsOff();
analogWrite(blue, 255);
analogWrite(white, 255);
lcd.setCursor(0, 0);
lcd.write(6);
lcd.write(7);
lcd.setCursor(18, 0);
lcd.write(6);
lcd.write(7);
lcd.setCursor(0, 3);
lcd.print("24V");
lcd.setCursor(13, 1);
lcd.print(99);
lcd.setCursor(18, 1);
lcd.print(99);
lcd.setCursor(6, 1);
lcd.print(" ");
}
}
/*||||||||||||||||||||||||||||||||||||||||||||| F A D E O U T |||||||||||||||||||||||||||||||||||||||||*/
if ( daybyminute >= ((ontime * 60) + photoperiod + ramptime))
{
if ( daybyminute < (ontime * 60) + photoperiod + (ramptime *2) )
{
LightOn();
backlighton();
lcd.setCursor(0, 0);
lcd.write(2);
lcd.write(3);
lcd.setCursor(18, 0);
lcd.write(2);
lcd.write(3);
for (int i = abc-1; i >= 0; i--)
{
analogWrite(blue, bluepercent[i]);
analogWrite(white, whitepercent[i]);
lcd.setCursor(13, 1);
lcd.print((bluepercent[i]*99)/255);
if (i < 10) lcd.print(" ");
lcd.setCursor(18, 1);
lcd.print((whitepercent[i]*99)/255);
if (i < 10) lcd.print(" ");
int countdown = ((rampdown*60)/abc); // calculates seconds to next step
while (countdown>0)
{
onesecond();
countdown--;
Temperature();
lcd.setCursor(6, 1);
if (countdown < 100) lcd.print ("0");
if (countdown < 10) lcd.print ("0");
lcd.print(countdown);
}
}
}
}
//*|||||||||||||||||||||||||||||||||||||||||||||||| Night Time ||||||||||||||||||||||||||||||||||||||||||||||*/
if (daybyminute >= (((ontime * 60) + photoperiod + (2 * ramptime))))
{
LightOff();
backlightoff();
AtsOn();
lcd.setCursor(0, 3);
lcd.print("24V");
lcd.setCursor(0, 0);
lcd.write(4);
lcd.write(5);
lcd.setCursor(18, 0);
lcd.write(4);
lcd.write(5);
lcd.setCursor(6, 1);
lcd.print(" ");
}
}
//*|||||||||||||||||||||||||||||||||||||||||||||||| T H E E N D ||||||||||||||||||||||||||||||||||||||||||*/