#include <RTClib.h>
RTC_DS1307 rtc;
const char direction_pin = 4;
const char step_pin = 5;
const char direction_pin1 = 2;
const char step_pin1 = 3;
int current_position = 0;
int readvalue = 0;
int x = 0;
int current_position1 = 0;
int y = 0;
unsigned long previousMillis = 0; // Stores the last time the potentiometer value was read
const long interval = 1000; // Interval at which to read the potentiometer (1 second)
// get solar data
const float latitude = 35.20653;
const float longitude = -0.63590;
const float solarNoon = 13.0833; // 1:05 PM
void calculateSolarAngles(int dayOfYear, int hour, int minute, float &azimuth, float &elevation) {
float timeOffset = (hour + minute / 60.0) - solarNoon;
float declination = 23.45 * sin(radians((360.0 / 365.0) * (284 + dayOfYear)));
float hourAngle = timeOffset * 15.0;
elevation = asin(sin(radians(latitude)) * sin(radians(declination)) +
cos(radians(latitude)) * cos(radians(declination)) * cos(radians(hourAngle))) * 180.0 / PI;
azimuth = atan2(-sin(radians(hourAngle)),
tan(radians(declination)) - tan(radians(latitude)) * cos(radians(hourAngle))) * 180.0 / PI;
if (azimuth < 0) azimuth += 360;
}
/*
int calculateDayOfYear(int day, int month, int year) {
static const int daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int dayOfYear = day;
for (int i = 0; i < month - 1; ++i) {
dayOfYear += daysInMonth[i];
}
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
if (month > 2) dayOfYear++;
}
return dayOfYear;
}
*/
int calculateDayOfYear(DateTime now) {
int day = now.day();
int month = now.month();
int year = now.year();
static const int daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Leap year check
bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
int dayOfYear = 0;
for (int i = 0; i < month - 1; ++i) {
dayOfYear += daysInMonth[i];
}
dayOfYear += day;
// Adjust for leap year
if (isLeapYear && month > 2) {
dayOfYear += 1;
}
return dayOfYear;
}
void setup() {
pinMode(direction_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(direction_pin1, OUTPUT);
pinMode(step_pin1, OUTPUT);
Serial.begin(9600);
// Initialize the RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// Set the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
unsigned long currentMillis = millis();
/*
// Manually set the current date and time
int year = 2024;
int month = 6;
int day = 29;
int hour = 12; // 1:00 PM
int minute = 37;
int dayOfYear = calculateDayOfYear(day, month, year);
*/
DateTime now = rtc.now();
int dayOfYear = calculateDayOfYear(now);
int hour = now.hour();
int minute = now.minute();
float azimuth, elevation;
// Get the current date and time
if (currentMillis - previousMillis >= interval) {
calculateSolarAngles(dayOfYear, hour, minute, azimuth, elevation);
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print("-->");
Serial.print("Azimuth: ");
Serial.print(azimuth);
Serial.print(" degrees, Elevation: ");
Serial.print(elevation);
Serial.println(" degrees");
}
//readvalue = analogRead(pot_pin);
// x=map(readvalue,0,1023,0,200);
x = ((azimuth+3) * 400) / 360;
Serial.println(x);
if (x > current_position) {
step_motor_forward();
current_position += 1;
} else if (x < current_position) {
step_motor_back();
current_position -= 1;
}
y = (elevation * 400) / 360;
Serial.println(y);
if (y > current_position1) {
step_motor_forward1();
current_position1 += 1;
} else if (y < current_position1) {
step_motor_back1();
current_position1 -= 1;
}
} //end of loop
void step_motor_forward() {
digitalWrite(direction_pin, LOW);
delay(10);
digitalWrite(step_pin, HIGH);
delay(10);
digitalWrite(step_pin, LOW);
}
void step_motor_back() {
digitalWrite(direction_pin, HIGH);
delay(10);
digitalWrite(step_pin, HIGH);
delay(10);
digitalWrite(step_pin, LOW);
}
void step_motor_forward1() {
digitalWrite(direction_pin1, LOW);
delay(10);
digitalWrite(step_pin1, HIGH);
delay(10);
digitalWrite(step_pin1, LOW);
}
void step_motor_back1() {
digitalWrite(direction_pin1, HIGH);
delay(10);
digitalWrite(step_pin1, HIGH);
delay(10);
digitalWrite(step_pin1, LOW);
}