Hello everyone,
I am a beginner in Arduino programming and I came across this code that uses several libraries such as Servo, EEPROM, Timezone, and SunPosition. I would appreciate it if someone could explain to me what this code is doing and how it works.
Here's the code:
#include <Servo.h>
#include <EEPROM.h>
#include <Timezone.h>
#include <SunPosition.h>
Servo xServo;
Servo yServo;
int xServoPin = 9; // X-axis servo connected to digital pin 9
int yServoPin = 10; // Y-axis servo connected to digital pin 10
int xServoPos = 90; // Starting position for X-axis servo
int yServoPos = 90; // Starting position for Y-axis servo
int xMax = 180; // Maximum rotation angle for X-axis servo
int xMin = 0; // Minimum rotation angle for X-axis servo
int yMax = 180; // Maximum rotation angle for Y-axis servo
int yMin = 0; // Minimum rotation angle for Y-axis servo
int tolerance = 10; // Tolerance for LDR readings
int eepromAddr = 0; // Starting EEPROM address to store servo positions
int eepromXPos = 0; // EEPROM address to store X-axis servo position
int eepromYPos = 2; // EEPROM address to store Y-axis servo position
// Latitude and longitude of La Castellana, Negros Occidental, Philippines
float latitude = 10.3881;
float longitude = 123.1155;
// Timezone object for Philippines Standard Time
TimeChangeRule dstStart = {"", Second, Sun, Mar, 2, 540}; // DST starts at 2:00 AM on the second Sunday of March
TimeChangeRule dstEnd = {"", First, Sun, Nov, 2, 480}; // DST ends at 2:00 AM on the first Sunday of November
Timezone tz(dstStart, dstEnd);
void setup() {
xServo.attach(xServoPin);
yServo.attach(yServoPin);
Serial.begin(9600);
// Read the initial servo positions from EEPROM
xServoPos = EEPROM.read(eepromXPos);
yServoPos = EEPROM.read(eepromYPos);
// If the EEPROM values are invalid, set the servo positions to the default values
if (xServoPos < xMin || xServoPos > xMax || yServoPos < yMin || yServoPos > yMax) {
xServoPos = 90;
yServoPos = 90;
}
// Set the servo positions to the initial values
xServo.write(xServoPos);
yServo.write(yServoPos);
}
void loop() {
// Get the current date and time in Philippines Standard Time
time_t t = now();
TimeElements te;
breakTime(tz.toLocal(t), te);
// Calculate the altitude and azimuth of the sun at the current time and location
float altitude = getSunAltitude(te.Year + 1900, te.Month, te.Day, te.Hour, te.Minute, te.Second, latitude, longitude);
float azimuth = getSunAzimuth(te.Year + 1900, te.Month, te.Day, te.Hour, te.Minute, te.Second, latitude, longitude);
// Calculate the new servo positions based on the sun's position
int newXPos = map(azimuth, -180, 180, xMin, xMax);
int newYPos = map(altitude, 0, 90, yMin, yMax);
xServoPos = constrain(newXPos, xMin, xMax);
// Determine whether the new positions are within the tolerance range of the current positions
bool xWithinTolerance = abs(xServoPos - xServo.read()) <= tolerance;
bool yWithinTolerance = abs(newYPos - yServo.read()) <= tolerance;
// If the new positions are not within tolerance, move the servos to the new positions
if (!xWithinTolerance || !yWithinTolerance) {
xServo.write(xServoPos);
yServo.write(newYPos);
// Store the new positions in EEPROM
EEPROM.write(eepromXPos, xServoPos);
EEPROM.write(eepromYPos, newYPos);
}
// Print the current time and sun position
Serial.print("Time: ");
Serial.print(tz.toLocal(t));
Serial.print(" | Altitude: ");
Serial.print(altitude);
Serial.print(" | Azimuth: ");
Serial.println(azimuth);
delay(1000);
}
Thank you in advance for your help!