/*
//This was taken from DZW post above. I give credit to him, I just added and changed a couple of things.
// Pins to control LEDs. Change these if you're using different pins.
int blue1Led = 3; // LED PWM channel for blues
int blue2Led = 4;
int blue3Led = 5;
int blue4Led = 6;
int white1Led = 11; // LED PWM channel for whites
int white2Led = 12;
int white3Led = 13;
int white4Led = 14;
// Set up RTC
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
// RTC variables
byte second, rtcMins, oldMins, rtcHrs, oldHrs, dayOfWeek, dayOfMonth, month, year;
// Other variables. These control the behavior of lighting. Change these to customize behavior
int minCounter = 0; // counter that resets at midnight. Don't change this.
int blue1StartMins = 480;
int blue2StartMins = 495;
int blue3StartMins = 525;
int blue4StartMins = 555;
int white1StartMins = 510;
int white2StartMins = 540;
int white3StartMins = 570;
int white4StartMins = 585;
int bluePhotoPeriod = 510;
int whitePhotoPeriod = 510; // photoperiod in minutes, whites. Same as above.
int fadeDuration = 60; // duration of the fade on and off for sunrise and sunset.
int blueMax = 255; // max intensity for blues. Change if you want to limit max intensity.
int whiteMax = 255; // max intensity for whites. Same as above.
/****** LED Functions ******/
/***************************/
//function to set LED brightness according to time of day
//function has three equal phases - ramp up, hold, and ramp down
void setLed(int mins, // current time in minutes
int ledPin, // pin for this channel of LEDs
int start, // start time for this channel of LEDs
int period, // photoperiod for this channel of LEDs
int fade, // fade duration for this channel of LEDs
int ledMax // max value for this channel
) {
if (mins > start && mins <= start + fade) {
analogWrite(ledPin, map(mins - start, 0, fade, 0, ledMax));
}
if (mins > start + period && mins <= start + period - fade) {
analogWrite(ledPin, ledMax);
}
if (mins > start + period - fade && mins <= start + period) {
analogWrite(ledPin, map(mins - start + period - fade, 0, fade, ledMax, 0));
}
}
/***** RTC Functions *******/
/***************************/
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
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));
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();
}
// Gets the date and time from the ds1307
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());
}
void setup() {
// init I2C
Wire.begin();
}
/***** Main Loop ***********/
/***************************/
void loop() {
// get time from RTC and put in hrs and mins variables
getDateDs1307(&second, &rtcMins, &rtcHrs, &dayOfWeek, &dayOfMonth, &month, &year);
minCounter = rtcHrs * 60 + rtcMins;
// determine if it is day or night, and act accordingly
if ((minCounter > blue1StartMins || minCounter > white1StartMins)
&& (minCounter < blue1StartMins + bluePhotoPeriod || minCounter < white1StartMins + whitePhotoPeriod)) { //day
// set LED states
setLed(minCounter, blue1Led, blue1StartMins, bluePhotoPeriod, fadeDuration, blueMax);
setLed(minCounter, white1Led, white1StartMins, whitePhotoPeriod, fadeDuration, whiteMax);
setLed(minCounter, blue2Led, blue2StartMins, bluePhotoPeriod, fadeDuration, blueMax);
setLed(minCounter, white2Led, white2StartMins, whitePhotoPeriod, fadeDuration, whiteMax);
setLed(minCounter, blue3Led, blue3StartMins, bluePhotoPeriod, fadeDuration, blueMax);
setLed(minCounter, white3Led, white3StartMins, whitePhotoPeriod, fadeDuration, whiteMax);
setLed(minCounter, blue4Led, blue4StartMins, bluePhotoPeriod, fadeDuration, blueMax);
setLed(minCounter, white4Led, white4StartMins, whitePhotoPeriod, fadeDuration, whiteMax);
}
else { //night
analogWrite(blue1Led, 0);
analogWrite(white1Led, 100);
analogWrite(blue2Led, 0);
analogWrite(white2Led, 100);
analogWrite(blue3Led, 0);
analogWrite(white3Led, 100);
analogWrite(blue4Led, 0);
analogWrite(white4Led, 100);
}
// Get ready for next iteration of loop
delay(1000);
}