arduino日出日落定时照明定时器

#include <DS3231.h>
#include <Wire.h>

DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
byte year, month, date, DoW, hour, minute, second;
int now;
//日出日落部分
float degreesToRadians = 3.1416 /180.0000;
float radiansToDegrees = 180.0000 /3.1416;
float degreeMinutesToDecimal = 1.0000 /60.0000;
float degreeSecondsToDecimal = 1.0000 /3600.0000;

bool input_East,input_South;
float input_Longitude,input_Latitude,input_Elevation,input_Month,input_Date,input_Year,input_TimeZone;//原始需要数据
int chu,luo,kai,guan;//结果
byte chu1,chu2,luo1,luo2;//结果转为小时、分钟
byte kai1,kai2,guan1,guan2;//结果转为小时、分钟
float NormalizeTo360 (float theThing) {
return (theThing - floor (theThing / 360.0) * 360);
}
void compute (void) {
float signedLongitude,signedLatitude,meridian,longitudeMeridianDifference,correctedYear,correctedMonth; //计算用转换数据
float t,G,C,L,alpha,obliquity,declination,eotAdjustment,clockTimeToLSOTAdjustment,sunRiseSetLSoTMinutes; //计算用中间数据
if (input_Latitude == 0) { input_Latitude = 0.000000001; }
if (input_Longitude == 0) { input_Longitude = 0.000000001; }
signedLongitude = input_Longitude;
if (input_East == 1) signedLongitude *= -1; // [1] = 东, [0] = 西
signedLatitude = input_Latitude;
if (input_South == 1) signedLatitude *= -1; // [0] = 北, [1] = 南
// 修复经度 > 180 deg
if (signedLongitude > 180) {
signedLongitude = signedLongitude - 360;
}
// 修复经度< -180 deg
if (signedLongitude < -180) {
signedLongitude = signedLongitude + 360;
}
// 当地标准时间子午线
meridian = input_TimeZone * -15;
// 如果太多与时区经度不同的警报
longitudeMeridianDifference = signedLongitude - meridian;
if ((longitudeMeridianDifference > 30) || (longitudeMeridianDifference < -30)) {
//alert ("所选择的时区与所在位置差距较大!");
}
// 计算通用时间
if (input_Month > 2) {
correctedYear = input_Year;
correctedMonth = input_Month - 3;
}
else {
correctedYear = input_Year - 1;
correctedMonth = input_Month + 9;
}
t = (input_Date + floor (30.6 * correctedMonth + 0.5) + floor (365.25 * (correctedYear - 1976)) - 8707.5) / 36525.0;
G = 357.528 + 35999.05 * t;
G = NormalizeTo360 (G);
C = (1.915 * sin (G * degreesToRadians)) + (0.020 * sin (2.0 * G * degreesToRadians));
L = 280.460 + (36000.770 * t) + C;
L = NormalizeTo360 (L);
alpha = L - 2.466 * sin (2.0 * L * degreesToRadians) + 0.053 * sin (4.0 * L * degreesToRadians);
obliquity = 23.4393 - 0.013 * t;
declination = atan (tan (obliquity * degreesToRadians) * sin (alpha * degreesToRadians)) * radiansToDegrees;
eotAdjustment = (L - C - alpha) / 15.0;
clockTimeToLSOTAdjustment = ((signedLongitude - meridian) / 15.0) - eotAdjustment; // 以小时为单位
sunRiseSetLSoTMinutes = radiansToDegrees * acos ( -1.0 * (sin (signedLatitude * degreesToRadians) * sin (declination * degreesToRadians) - sin ((-0.8333 - 0.0347 * sqrt (input_Elevation)) * degreesToRadians)) / cos (signedLatitude * degreesToRadians) / cos (declination * degreesToRadians)) * 4;
//日出时间
chu=12 * 60 - sunRiseSetLSoTMinutes + (clockTimeToLSOTAdjustment * 60);
//日落时间
luo=12 * 60 + sunRiseSetLSoTMinutes + (clockTimeToLSOTAdjustment * 60);
kai=luo+25;//日落后40分钟开灯
guan=chu-40;//日出前40分钟天亮,关灯
chu1=(int)chu/60;
chu2=(int)chu%60;
luo1=(int)luo / 60;
luo2=(int)luo % 60;
Serial.print("sunrise:");
Serial.print(chu1);
Serial.print(':');
Serial.print(chu2);
Serial.print(",sunset:");
Serial.print(luo1);
Serial.print(':');
Serial.print(luo2);
Serial.print('\n');
guan1=(int)(guan)/60;
guan2=(int)(guan)%60;
kai1=(int)(kai) / 60;
kai2=(int)(kai) % 60;
Serial.print("break:");
Serial.print(guan1);
Serial.print(':');
Serial.print(guan2);
Serial.print(",dark:");
Serial.print(kai1);
Serial.print(':');
Serial.print(kai2);
Serial.print('\n');
}

void setup() {
pinMode( 2 , OUTPUT);
digitalWrite( 2 , LOW );
// Start the I2C interface
Wire.begin();
//Clock.setSecond(50);//Set the second
//Clock.setMinute(36);//Set the minute
//Clock.setHour(8); //Set the hour
//Clock.setDoW(6); //Set the day of the week
//Clock.setDate(10); //Set the date of the month
//Clock.setMonth(3); //Set the month of the year
//Clock.setYear(17); //Set the year (Last two digits of the year)
// Start the serial interface
Serial.begin(115200);
//提供的参数
input_Longitude=114.41; //经度
input_East=1;//东经1,西经0
input_Latitude=36.93;//维度
input_South=0;//北纬0,南纬1
//input_Year=2017;
//input_Month=2;
//input_Date=2;
input_TimeZone=8;
}
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 ReadDS3231()
{
int second,minute,hour,date,month,year,temperature;
second=Clock.getSecond();
minute=Clock.getMinute();
hour=Clock.getHour(h12, PM);
date=Clock.getDate();
month=Clock.getMonth(Century);
year=Clock.getYear();

temperature=Clock.getTemperature();
// input_Year=2017;
//input_Month=3;
//input_Date=10;
input_Year=2000+year;
input_Month=month;
input_Date=date;
compute();//计算
now=hour*60+minute;
//Serial.print(input_Year);
//Serial.print('\n');
//Serial.print(now);
//Serial.print('-');
//Serial.print(kai);
//Serial.print('-');
//Serial.print(guan);
//Serial.print('\n');
if (now>guan&&now<kai){
digitalWrite( 2 , LOW );
}else{
digitalWrite( 2 , HIGH );
}
Serial.print("now:20");
Serial.print(year,DEC);
Serial.print('-');
Serial.print(month,DEC);
Serial.print('-');
Serial.print(date,DEC);
Serial.print(' ');
Serial.print(hour,DEC);
Serial.print(':');
Serial.print(minute,DEC);
Serial.print(':');
Serial.print(second,DEC);
Serial.print('\n');
Serial.print("Temperature=");
Serial.print(temperature);
Serial.print('\n');
Serial.print('\n');
Serial.print('\n');
}
void loop() {ReadDS3231();delay(10000);

}

此条目发表在待分类分类目录。将固定链接加入收藏夹。