#define pi 3.141592653589793238462643383279502884197169399375105820974944
#include <Servo.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
Servo servomotor1;
Servo servomotor2;
void setup() {
Serial.begin(9600);
servomotor1.attach(9);
servomotor2.attach(11);
Wire.begin();
RTC.begin();
RTC.adjust(DateTime("Nov 22 2019", "8:02:0"));
}
void loop() {
DateTime now = RTC.now();
String var;
int Loc1, Loc2, Loc3, Loc4, Loc5;
String var1, var2, var3, var4, var5, var6;
String Temp_var, Temp2_var, Temp3_var, Temp4_var;
double phi, now.month(), n, now.hour(), minuate, now.second()s; //n은 일을 뜻함
if(Serial.available()){
var = Serial.readString();
Loc1 = var.indexOf(" ");
var1 = var.substring(0,Loc1); // 위도
Temp_var = var.substring(Loc1+1);
Loc2 = Temp_var.indexOf(" ");
var2 = Temp_var.substring(0,Loc2); //월
Temp2_var = Temp_var.substring(Loc2+1);
Loc3 = Temp2_var.indexOf(" ");
var3 = Temp2_var.substring(0, Loc3); //일
Temp3_var = Temp2_var.substring(Loc3+1);
Loc4 = Temp3_var.indexOf(" ");
var4 = Temp3_var.substring(0, Loc4); //시
Temp4_var = Temp3_var.substring(Loc4+1);
Loc5 = Temp4_var.indexOf(" ");
var5 = Temp4_var.substring(0, Loc5); //분
var6 = Temp4_var.substring(Loc5+1); //초
phi = var1.toFloat();
now.month() = var2.toFloat();
n = var3.toFloat();
now.hour() = var4.toFloat();
minuate = var5.toFloat();
now.second()s = var6.toFloat(); //위도, 날짜(월,일), 시간(시,분,초)을 입력받았다 - 오케 성공
*/
float h;
int day1;
//위도 연산과정 - 성공
int phi = 37*pi/180;
//월, 일 연산과정 - 오케 성공
if(now.month()==1){
day1 = now.day();
}
else if(now.month()==2){
day1 = now.day()+31;
}
else if(now.month()==3){
day1 = now.day()+60;
}
else if(now.month()==4){
day1 = now.day()+90;
}
else if(now.month()==5){
day1 = now.day()+120;
}
else if(now.month()==6){
day1 = now.day()+151;
}
else if(now.month()==7){
day1 = now.day()+180;
}
else if(now.month()==8){
day1 = now.day()+211;
}
else if(now.month()==9){
day1 = now.day()+242;
}
else if(now.month()==10){
day1 = now.day()+272;
}
else if(now.month()==11){
day1 = now.day()+303;
}
else if(now.month()==12){
day1 = now.day()+333;
}
//시분초 - 오우 성공
float watch = (3600*now.hour()+60*now.minute()+now.second());
//시분초 이용 시간각 연산과정 - 성공
if (now.hour()>=12){
h = (3600*(now.hour()-12)+(60*now.minute())+now.second())*(pi/43200);
}
else{
h = (3600*(now.hour()-12)+(60*now.minute())+now.second())*(pi/43200)+2*pi;
}
//날짜 이용 적위 계산 - 성공
float delta=((23.5*pi)/180)*sin((day1()-80)*360/365*pi/180);
//Height고도를 h,delta,phi로부터 연산
float Height=asin(cos(h)*cos(delta)*cos(phi)+sin(delta)*sin(phi));
if(Height>=0){
Height = Height;
}
else{
Height = -Height;
}
//Angle방위각을 h,delta,phi,Height로 부터 연산
float sinA = (sin(h)*cos(delta))/cos(Height);
float cosA = (cos(h)*cos(delta)*sin(phi)-sin(delta)*cos(phi))/cos(Height);
float Angle;
if(sinA >= 0 && cosA >= 0){
Angle = asin(sinA);
}
else if(sinA >= 0 && cosA <0) {
Angle = pi - asin(sinA);
}
else if(sinA <0 && cosA >=0){
Angle = 2*pi + asin(sinA);
}
else{
Angle = pi - asin(sinA);
}
Height = Height*180/pi;
Angle = Angle*180/pi;
Serial.println(Height,10);
servomotor1.write(Height);
Serial.println(Angle,10);
servomotor2.write(Angle);
Serial.println();
delay(1000);
|