#include <Stepper.h>
#include <RTClib.h> // Assuming you're using RTClib library
// Define stepper motor parameters
const int stepsPerRevolution = 200; // Adjust based on your stepper motors
// azimuthStepper pins
const int stepPin1 = 5;// Assign appropriate pins for stepper 1
const int dirPin1 = 6;// Assign appropriate pins for stepper 1
// elevationStepper pins
const int stepPin2 = 3; // Assign appropriate pins for stepper 2
const int dirPin2 = 4; // Assign appropriate pins for stepper 2
// LDR pins
const int ldrTopLeftPin = A0;
const int ldrTopRightPin = A1;
const int ldrBottomLeftPin = A2;
const int ldrBottomRightPin = A3;
// Threshold for LDR difference to trigger movement
const int threshold = 25; // Adjust for sensitivity
// Create stepper objects
Stepper azimuthStepper(stepsPerRevolution, stepPin1, dirPin1);
Stepper elevationStepper(stepsPerRevolution, stepPin2, dirPin2);
// Define RTC object
RTC_DS3231 rtc; // Assuming a DS3231 RTC module
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// Constants for calculations
const float latitude = 18.83; // Replace with your latitude,Temixco Morelos
void setup() {
// Set initial stepper speeds
azimuthStepper.setSpeed(200); // Adjust as needed
elevationStepper.setSpeed(200); // Adjust as needed
// Start serial communication for debugging
Serial.begin(9600);
// Start RTC communication
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
}
void az_elev()//Movimiento de motores
{
float p = 3.1416;
DateTime now = rtc.now();
int hour = now.hour();
int minute = now.minute();
int second = now.second();
int day = now.day();
int month = now.month();
int year = now.year();
int N;
int x;
int y;
int z;
//determinacion del dia juliano o ordinario del año
if (month > 2)
{
month = month +1;
x = month*30.6;//Factor de promedio de dias del año de ¿365.26 dias?
y = floor (x);//Duración del mes promedio valor absoluto
if ((year % 4)== 0){
z= y-62;//año biciesto
}
else{
z = y-63;//año no bisiesto
}
N = z + day;
}
else
{
month = month - 1;
if ((year% 4)== 0){
x = month*62;
}
else {
x = month*63;
}
y= x/2;
floor (y);
N = y+day;
}
float h= hour + (minute/ 60) + (second/ 3600);
//Calculo Calculo angulos solares basicos
float delta = 23.45 * sin(2 * p * (N - 81) / 365); //Calculo de declinacion solar
float H = 15 * (h - 12); //Calculo de angulo horario
// Calculate altitude
float alt = (sin(radians(latitude)) * sin(radians(delta)) + (cos(radians(latitude)) * cos(radians(delta)) * cos(radians(H))));
alt =alt = degrees(asin(alt));
// Calculate azimuth
float az= (sin(radians(delta)) - sin(radians(latitude)) * sin(radians(alt))) / (cos(radians(latitude)) * cos(radians(alt)));
az = degrees(asin(az));
if (H < 0)
{
az = az;
}
else
{
az = 360 - az;
}
// Move stepper motors using calculated azimuth and elevation/Resolucion de 200 pasos por 360° igual a 1/8° por paso
azimuthStepper.step(stepsPerRevolution * az / 360);//Assuming 360 degrees max azimuth
elevationStepper.step(stepsPerRevolution * alt / 90); // Assuming 90 degrees max elevation
{
int movAzimuth;
int movElevations;
movAzimuth = stepsPerRevolution * az / 360;
movElevations = stepsPerRevolution * alt / 90;
Serial.print(movAzimuth);Serial.print(",");
Serial.println(movElevations);
Serial.println();
}
delay(1000); // Update positions every second
}
void loop() {
// Read LDR values
int ldrTopLeft = analogRead(ldrTopLeftPin); //Pin A0
int ldrTopRight = analogRead(ldrTopRightPin); //Pin A1
int ldrBottomLeft = analogRead(ldrBottomLeftPin); //Pin A2
int ldrBottomRight = analogRead(ldrBottomRightPin); //Pin A3
// Calculate differences for horizontal and vertical movement
int horizontalDifference = ldrTopLeft + ldrBottomLeft - ldrTopRight - ldrBottomRight;
int verticalDifference = ldrTopLeft + ldrTopRight - ldrBottomLeft - ldrBottomRight;
//************************************************************************
// Determine movement directions and step "de acuerdo a las fotoresistencias"
if (ldrTopLeft < 500 && ldrTopRight < 500 && ldrBottomLeft < 500 && ldrBottomRight < 500 )
{
if (horizontalDifference > threshold)
{
azimuthStepper.step(1); // Move right
}
else if (horizontalDifference < -threshold)
{
azimuthStepper.step(-1); // Move left
}
else
{
azimuthStepper.step(0); // Stop
}
if (verticalDifference > threshold)
{
elevationStepper.step(1); // Move up
}
else if (verticalDifference < -threshold)
{
elevationStepper.step(-1); // Move down
}
else {
elevationStepper.step(0); // Stop
}
}
else
{
az_elev();
}
//***********Adicionado por JJQA/JUl-2024********************
{
DateTime now = rtc.now();
Serial.print("Current time: ");
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(horizontalDifference);Serial.print(",");
Serial.println(verticalDifference);
Serial.print(ldrTopLeft);Serial.print(",");
Serial.print(ldrTopRight);Serial.print(",");
Serial.print(ldrBottomLeft);Serial.print(",");
Serial.println(ldrBottomRight);
Serial.println();
}
delay(5000); // Adjust delay for smoother movement
}
Elevacion
Azimut
Solar Tracker/Compass
(Aim Azimuth pointer at sun
then is north is up^^^)