and the other half
//Calculate Sunrise/set by date & predefined season & rise/set times
void Seasons()
{
//Set the hour you want the calculations of rise an set to be based on
int UserRiseHour = 7;
int UserSetHour = 17;
#define forceseasoncalculation
static byte ssn , ssnp = 0 , ssnpt ;
long stime, wstime, vstime, wrtime, rtime, vrtime;
int wrhour,wrmin,wrsec,wshour,wsmin,wssec,rsec,ssec,vrhour,vrmin,vrsec,vshour,vsmin,vssec;
int iDiffrise = 0;
int iDiffset = 0;
int risediffperday = 0;
int setdiffperday = 0;
int totalrise = 0;
int totalset = 0;
byte s=0;
int DaysPerYear;
//rise and set times set by hour and minute. there are 4 seasons however there are 8 highs & lows in rise and set throughout the year
//first spot is second half of winter starting jan 1st - DO NOT CHANGE
int risehour[8]= {
UserRiseHour+1,UserRiseHour+1,UserRiseHour+1,UserRiseHour,UserRiseHour,UserRiseHour-1,UserRiseHour,UserRiseHour };/*{
7,7,7,6,6,5,6,6 };*/
int riseminute[8]={
00,30,00,30,00,30,00,30 };
int sethour[8] = {
UserSetHour-2,UserSetHour-1,UserSetHour-1,UserSetHour,UserSetHour,UserSetHour,UserSetHour,UserSetHour-1 };/*{
17,18,18,19,19,19,19,18 };*/
int setminute[8] = {
30,00,30,00,00,30,00,00 };
/* //
if (hour()==0 && minute()==0 && second()==0) ssnp=0;
#ifdef forceseasoncalculation
if (ssnp==0)
#else
if (hour()==0 && minute()==0 && second()==1 && ssnp==0)
#endif
{*/
//leapyear or not to define DaysPerYear - DO NOT CHANGE
if (year()%4 == 0 && !(year()%100 == 0 && year()%400 != 0))
{
DaysPerYear=366;
}
else
{
DaysPerYear = 365;
}
//Call Day Number Calc to determin day ie december 31st on a non leap year is day 365 - DO NOT CHANGE
DayNumber(year(),month(),day());
//define days between beginning, middle and end of seasons high peaks - DO NOT CHANGE
int seasons[9] ={
0,45,96,135,187,238,283,328,DaysPerYear };
//define season and array pulling variable - DO NOT CHANGE
for (s=0; seasons[s] < Ndays; s++) ssn = s+1, ssnpt = s+1, ssnp = s;
//set loop on array time pulling variable to go back to beginning instead of increasing array size - DO NOT CHANGE
if (ssn >= 7) ssn = 0;
//differece in seconds between two rise/set array times pulled - DO NOT CHANGE
long rise1 = calcSec(risehour[ssn],riseminute[ssn]);
long rise2 = calcSec(risehour[ssnp],riseminute[ssnp]);
iDiffrise = calcTime(rise1, rise2);
long set1 = calcSec(sethour[ssn],setminute[ssn]);
long set2 = calcSec(sethour[ssnp],setminute[ssnp]);
iDiffset = calcTime(set1,set2);
//calculate new sunrise/set difference from array value & last group of code - DO NOT CHANGE
risediffperday = iDiffrise/(seasons[ssnpt]-seasons[ssnp]);
totalrise = risediffperday*(Ndays - seasons[ssnp]);
setdiffperday = iDiffset/(seasons[ssnpt]-seasons[ssnp]);
totalset = setdiffperday*(Ndays - seasons[ssnp]);
//creating time in seconds for main sun rise/set number - DO NOT CHANGE
rtime=calcSec(risehour[ssnp],riseminute[ssnp]);
if (ssnp == 0 || ssnp == 2 || ssnp == 4 || ssnp == 6){
rtime += totalrise;
}
else {
rtime -= totalrise;
}
stime=calcSec(sethour[ssnp],setminute[ssnp]);
if (ssnp == 1 || ssnp == 3 || ssnp == 5 || ssnp == 7){
stime -= totalset;
}
else {
stime += totalset;
}
//These are the off set times, standard rtime and stime are for Royal Blues & Blues
// DO NOT CHANGE the operators in these equations ie +-
// The number is in seconds (1200) change this number to change the offset for each color
wrtime = rtime + 1800;//w r/stime is for Whites - shorter time span then blues
wstime = stime - 3600;
vrtime = rtime - 1800;//v r/stime is for Violets - Longer time then blues
vstime = stime + 1800;
//turning seconds back to Hours:Minutes:Seconds
//Blues
rhour=rtime/3600;
rtime=rtime%3600;
rmin=rtime/60;
rtime=rtime%60;
rsec=rtime;
if(rsec > 30) rmin++;
shour=stime/3600;
stime=stime%3600;
smin=stime/60;
stime=stime%60;
ssec=stime;
if(ssec > 30) smin++;
//White
wrhour = wrtime/3600;
wrtime=wrtime%3600;
wrmin=wrtime/60;
wrtime=wrtime%60;
wrsec=wrtime;
if(wrsec>30) wrmin++;
wshour = wstime/3600;
wstime=wstime%3600;
wsmin=wstime/60;
wstime=wstime%60;
wssec=wstime;
if(wssec>30) wsmin++;
//Violet
vrhour = vrtime/3600;
vrtime=vrtime%3600;
vrmin=vrtime/60;
vrtime=vrtime%60;
vrsec=vrtime;
if(vrsec>30) vrmin++;
vshour = vstime/3600;
vstime=vstime%3600;
vsmin=vstime/60;
vstime=vstime%60;
vssec=vstime;
if(vssec>30) vsmin++;
if (rmin==60)rmin=00, rhour+=1;
if (smin==60)smin=00, shour+=1;
if (wrmin==60)wrmin=00, wrhour+=1;
if (wsmin==60)wsmin=00, wshour+=1;
if (vrmin==60)vrmin=00, vrhour+=1;
if (vsmin==60)vsmin=00, vshour+=1;
//This is the PWM Slope for each channel, each channel pulls an array value from above(hour,minute) to use, how you set them is up to you.
//Just always use a Rise hour in a Rise spot, always a set hour in a set spot ect ect
ChannelValue[LEDPWM0]=PWMSlope(wrhour,wrmin,wshour,wsmin,0,2.55*28,120,ChannelValue[LEDPWM0]);
ChannelValue[LEDPWM1]=PWMSlope(vrhour,vrmin,vshour,vsmin,0,2.55*58,70,ChannelValue[LEDPWM1]);
ChannelValue[LEDPWM2]=PWMSlope(rhour,rmin,shour,smin,0,2.55*58,90,ChannelValue[LEDPWM2]);
ChannelValue[LEDPWM3]=PWMSlope(wrhour,wrmin,wshour,wsmin,0,2.55*36,105,ChannelValue[LEDPWM3]);
//ChannelValue[LEDPWM4]=PWMSlope(vrhour,vrmin,vshour,vsmin,0,2.55*90,45,ChannelValue[LEDPWM4]);
//ChannelValue[LEDPWM0]=PWMSlope(wrhour,wrmin,wshour,wsmin,0,2.55*85,180,ChannelValue[LEDPWM4]);
}
//End Seasons Calculation
//Calculators for Seasons function
long calcSec(long hr, long minu)
{
long totalseconds;
totalseconds=(hr*3600)+(minu*60);
return totalseconds;
}
long calcTime(long seconds1, long seconds2)
{
long timediff=abs(seconds1-seconds2);
return timediff;
}
void DayNumber(unsigned int y, unsigned int m, unsigned int d)
{
int days[]={
0,31,59,90,120,151,181,212,243,273,304,334 }; // Number of days at the beginning of the month in a not leap year.
//Start to calculate the number of day
if (m==1 || m==2){
Ndays = days[(m-1)]+d; //for any type of year, it calculate the number of days for January or february
} // Now, try to calculate for the other months
else if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0){ //those are the conditions to have a leap year
Ndays = days[(m-1)]+d+1; // if leap year, calculate in the same way but increasing one day
}
else { //if not a leap year, calculate in the normal way, such as January or February
Ndays = days[(m-1)]+d;
}
}
//End calculators