#include <Servo.h>
// Define servos
Servo horizontalServo;
Servo verticalServo;
// Define pins for LDR modules
const int LDR1 = A0; // LDR Module 1 (Left/Right)
const int LDR2 = A1; // LDR Module 2 (Up/Down)
// Servo pins
const int horizontalPin = 9; // Horizontal Servo
const int verticalPin = 10; // Vertical Servo
// Servo positions
int horizontalPos = 90; // Initial horizontal position (centered)
int verticalPos = 90; // Initial vertical position (centered)
// Sensitivity adjustment for smooth tracking
const int tolerance = 10;
void setup() {
// Attach servos
horizontalServo.attach(horizontalPin);
verticalServo.attach(verticalPin);
// Move servos to initial position
horizontalServo.write(horizontalPos);
verticalServo.write(verticalPos);
// Start serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read light intensity from LDR modules
int lightLDR1 = analogRead(LDR1); // Left/Right LDR
int lightLDR2 = analogRead(LDR2); // Up/Down LDR
// Debugging: Print light values to Serial Monitor
Serial.print("LDR1: ");
Serial.print(lightLDR1);
Serial.print(" | LDR2: ");
Serial.println(lightLDR2);
// Calculate the difference between light intensities
int lightDifference = lightLDR1 - lightLDR2;
// Adjust horizontal servo position
if (abs(lightDifference) > tolerance) {
horizontalPos = map(lightDifference, -1023, 1023, 0, 180); // Map LDR difference to 0-180 degrees
horizontalPos = constrain(horizontalPos, 0, 180); // Ensure within servo range
horizontalServo.write(horizontalPos);
}
// Optional: Add vertical movement for dual-axis tracking
/*
// Adjust vertical servo position
if (abs(lightDifference) > tolerance) {
verticalPos = map(lightDifference, -1023, 1023, 0, 180); // Map LDR difference to 0-180 degrees
verticalPos = constrain(verticalPos, 0, 180); // Ensure within servo range
verticalServo.write(verticalPos);
}
*/
// Add a short delay for stability
delay(50);
}