I am working on a solar tracking robot project using 2 servo motors and 4 LDRs with an Arduino Uno. I am excited about this project, but I am struggling to use the SolarPosition library for the project. I have experience working with the servo motor and the LDR, but I have no prior knowledge of the SolarPosition library.
I am reaching out to the community to ask for help and guidance on how to use the SolarPosition library effectively for my project. I would appreciate any resources or tips on how to get started and make the most of this library for my project.
Thank you in advance for your help and support. I am looking forward to learning from the community and making progress on my project......
#include <Servo.h>
// Define servo motors
Servo servo1;
Servo servo2;
// Define LDR pins
const int ldrPin1 = A0;
const int ldrPin2 = A1;
const int ldrPin3 = A2;
const int ldrPin4 = A3;
// Define servo motor positions
int servoPos1 = 90; // starting position
int servoPos2 = 90; // starting position
void setup() {
// Initialize servo motors
servo1.attach(9);
servo2.attach(10);
}
void loop() {
// Read LDR sensor values
int ldrVal1 = analogRead(ldrPin1);
int ldrVal2 = analogRead(ldrPin2);
int ldrVal3 = analogRead(ldrPin3);
int ldrVal4 = analogRead(ldrPin4);
// Calculate average LDR sensor value
int avgLdrVal = (ldrVal1 + ldrVal2 + ldrVal3 + ldrVal4) / 4;
// Determine which direction to move the servo motors
if (avgLdrVal > 500) {
// Sun is on the right side, move servos to the right
servoPos1 += 1;
servoPos2 -= 1;
} else {
// Sun is on the left side, move servos to the left
servoPos1 -= 1;
servoPos2 += 1;
}
// Ensure servo positions stay within range
if (servoPos1 < 0) {
servoPos1 = 0;
}
if (servoPos1 > 180) {
servoPos1 = 180;
}
if (servoPos2 < 0) {
servoPos2 = 0;
}
if (servoPos2 > 180) {
servoPos2 = 180;
}
// Move servo motors to new positions
servo1.write(servoPos1);
servo2.write(servoPos2);
// Delay before next loop
delay(50);
}
IF the tracker library you referenced is utilized, maybe a graphic of how to connect 4 LDR will be shown. Is this library a requirement or just a suggestion???
But you wish to utilize LDRs to set the platform position which feeds the necessary "position" data to the library.
Thought: just mentally playing around with the "accuracy" of a completed system, I do not believe you can reliably know the (absolute) position of robot to any reasonable position ... maybe within a few kilometers at best.
IMO, as stated, you will need to build a prototype, write code, and come back with specific question regarding the codebase. At this point, you are in research mode.
Using LDRs, there is no need for the SolarPosition library. That library calculates sun position based on location (latitude/longitude) and time, for which you need an accurate way of determining time, generally a real-time clock (RTC). If your "robot" is mobile, you also need some way of telling the exact orientation of the robot, because the SolarPosition library gives an angle and elevation, and you need a fixed reference of which direct the robot is facing and what angle it is sitting at.
One could use this library SolarCalculator library for Arduino. I use the library to determine when the sun comes up and where the sun will be to point a solar array at the sun. Then I use the library to get sun position once every 6 minutes to move the array to the suns new position.
Did you try the example sketches that come with the SolarPosition library? It would be relatively easy to just take the real time az/el values that it produces, and scale them to servo input values. Disclaimer, I'm the library author. I never made any examples specific to any hardware, for the obvious reason that end users would have diverse applications. If you make a reasonable effort to do that, and post the results here, I will help you.
It was originally written for lighting control, but is definitely generic enough for the application you outline in your first post.
There are no fancy mathematical tasks, as both sun and servo platform use a two dimensional polar coordinate system.
Have you been able to get a current sun angle from the library? Take the azimuth value of the sun and rotate the servo to match the angle. Take the elevation value of the sun and rotate the servo to match the angle. Position the unit so that it points North, so that the angles produced by the calculator relate to the servo torque angle.
To sharpen my point, we are not getting any feedback about what the OP has actually tried. Just some boilerplate code with, and followed by, one drawing and some questions.
It is necessary to see some activity, in order to guide the activities.