#include <Servo.h> // Include the Servo library to control the servo motor
// Define the pin connections
int ldr1Pin = A0; // Top LDR connected to analog pin A0
int ldr2Pin = A1; // Bottom LDR connected to analog pin A1
int solarPanelPin = A2; // Solar panel input connected to analog pin A2
int buttonPin = 0; // Push button connected to digital pin 0
int servoPin = 9; // Servo motor connected to digital pin D9
// 7-segment display pins (A, B, C, D, E, F, G)
int segPins[] = {1, 2, 3, 4, 5, 6, 7}; // Adjust the pins according to your setup
// Create a Servo object to control the servo motor
Servo servoMotor;
// Variables to control the delay time and the servo position
int delayTime = 100; // Default delay (100ms)
int servoAngle = 90; // Servo starts at 90 degrees (facing forward)
// Function to display a number on the 7-segment display
void displayNumber(int num) {
// Turn off all segments first (clear display)
for (int i = 0; i < 7; i++) {
digitalWrite(segPins[i], LOW);
}
// Turn on the segments based on the number to display
switch (num) {
case 1:
// Display "1" (only segments B and C)
digitalWrite(segPins[1], HIGH); // B
digitalWrite(segPins[2], HIGH); // C
break;
case 2:
// Display "2" (A, B, D, E, G)
digitalWrite(segPins[0], HIGH); // A
digitalWrite(segPins[1], HIGH); // B
digitalWrite(segPins[3], HIGH); // D
digitalWrite(segPins[4], HIGH); // E
digitalWrite(segPins[6], HIGH); // G
break;
case 3:
// Display "3" (A, B, C, D, G)
digitalWrite(segPins[0], HIGH); // A
digitalWrite(segPins[1], HIGH); // B
digitalWrite(segPins[2], HIGH); // C
digitalWrite(segPins[3], HIGH); // D
digitalWrite(segPins[6], HIGH); // G
break;
case 4:
// Display "4" (B, C, F, G)
digitalWrite(segPins[1], HIGH); // B
digitalWrite(segPins[2], HIGH); // C
digitalWrite(segPins[5], HIGH); // F
digitalWrite(segPins[6], HIGH); // G
break;
case 5:
// Display "5" (A, C, D, F, G)
digitalWrite(segPins[0], HIGH); // A
digitalWrite(segPins[2], HIGH); // C
digitalWrite(segPins[3], HIGH); // D
digitalWrite(segPins[5], HIGH); // F
digitalWrite(segPins[6], HIGH); // G
break;
default:
// Display nothing (clear display) if number is out of range
for (int i = 0; i < 7; i++) {
digitalWrite(segPins[i], LOW); // Turn off all segments
}
break;
}
}
// Function to check if the button is pressed and adjust the delay time
void checkButton() {
if (digitalRead(buttonPin) == HIGH) {
delayTime = 5000; // Button pressed, set delay to 5000ms
} else {
delayTime = 100; // Button not pressed, set delay to 100ms
}
}
// Function to adjust the servo based on LDR readings
void adjustServo() {
int ldr1Val = analogRead(ldr1Pin); // Read the value from the top LDR
int ldr2Val = analogRead(ldr2Pin); // Read the value from the bottom LDR
// If the top LDR gets more light, turn the servo counterclockwise
if (ldr1Val > ldr2Val) {
while (ldr1Val > ldr2Val) {
servoAngle--; // Decrease servo angle
servoMotor.write(servoAngle); // Move the servo
delay(delayTime); // Wait for the selected delay time
ldr1Val = analogRead(ldr1Pin); // Read the LDR values again
ldr2Val = analogRead(ldr2Pin);
}
}
// If the bottom LDR gets more light, turn the servo clockwise
else if (ldr1Val < ldr2Val) {
while (ldr1Val < ldr2Val) {
servoAngle++; // Increase servo angle
servoMotor.write(servoAngle); // Move the servo
delay(delayTime); // Wait for the selected delay time
ldr1Val = analogRead(ldr1Pin); // Read the LDR values again
ldr2Val = analogRead(ldr2Pin);
}
}
}
// Function to display the solar panel voltage on the 7-segment display
void displaySolarIntensity() {ÿ$0
int solarValue = analogRead(solarPanelPin); // Read the value from the solar panel
// Map the solar value to display numbers based on voltage ranges
if (solarValue < 205) {
displayNumber(1); // 1 volt
} else if (solarValue < 410) {
displayNumber(2); // 2 volts
} else if (solarValue < 615) {
displayNumber(3); // 3 volts
} else if (solarValue < 820) {
displayNumber(4); // 4 volts
} else if (solarValue <= 1023) {
displayNumber(5); // 5 volts
} else {
displayNumber(-1); // Clear display if out of range
}
}
void setup() {
// Set the pin modes for the 7-segment display pins
for (int i = 0; i < 7; i++) {
pinMode(segPins[i], OUTPUT); // Set the segment pins as OUTPUT
}
// Set the button pin as INPUT
pinMode(buttonPin, INPUT);
// Initialize the servo motor
servoMotor.attach(servoPin);
servoMotor.write(servoAngle); // Start at 90 degrees
// Set up LDR and solar panel pins as INPUT
pinMode(ldr1Pin, INPUT);
pinMode(ldr2Pin, INPUT);
pinMode(solarPanelPin, INPUT);
}
void loop() {
checkButton(); // Check if the button is pressed and adjust the delay
displaySolarIntensity(); // Continuously update the solar intensity on the 7-segment display
adjustServo(); // Adjust the servo motor to align with the sun
delay(delayTime); // Wait for the selected delay time, then refresh the display and readings
}