#include <Servo.h>
Servo solarMover;
int leftPhotoResistPin = A1;//pins in left and right resistors
int rightPhotoResistPin = A0;
int leftPhotoResistValue = 0; //Variables for the photoresistor values
int rightPhotoResistValue = 0;
int photoDiff = 0; //the difference in left and right photoresistors
int solarMoverPos = 90; //Varaible for the base position of the servo motor ONLY WORKS IF THE SETUP IS EXACTLY THE SAME AS MINE WAS
Servo solarMoverHor;
int topPhotoResistPin = A2;//pins in left and right resistors
int bottomPhotoResistPin = A3;
int topPhotoResistValue = 0; //Variables for the photoresistor values
int bottomPhotoResistValue = 0;
int photoDiff2 = 0; //the difference in left and right photoresistors
int solarMoverPos2 = 90; //Varaible for the base position of the servo motor ONLY WORKS IF THE SETUP IS EXACTLY THE SAME AS MINE WAS
void setup() {
Serial.begin(9600);//sets the bits per second for serial data transmission
solarMover.attach(11);//the pin the servo was attached to
solarMoverHor.attach(9);
}
void loop() {
rightPhotoResistValue = analogRead(rightPhotoResistPin);// reads values from the right photoresistor
leftPhotoResistValue = analogRead(leftPhotoResistPin);// reads values from the left photoresistor
topPhotoResistValue = analogRead(topPhotoResistPin);// reads values from the right photoresistor
bottomPhotoResistValue = analogRead(bottomPhotoResistPin);// reads values from the left photoresistor
/*
Serial.print("rightphotoresistValue: ");
Serial.println(rightPhotoResistValue); //prints the value of the rightPhotoResistValue
Serial.print("leftphotoresistValue: ");
Serial.println(leftPhotoResistValue); // prints the value of the leftPhotoResistValue
*/
if(rightPhotoResistValue<50 && leftPhotoResistValue<50){ //checks if both of photoresistors are not getting much light
while(solarMoverPos < 90 ){// if the photoresistors are both not getting light it will move the solar panel back to a base position
solarMoverPos++;//incriments angle
solarMover.write(solarMoverPos);//updates the angle
delay(10);//delays for 10 ms
}
while(solarMoverPos >90){
solarMoverPos--;//decriments angle
solarMover.write(solarMoverPos);//updates the angle
delay(10);//delays for 10 ms
}
}
photoDiff = rightPhotoResistValue - leftPhotoResistValue; //calculates the difference in the values of the photoresistors
Serial.println(photoDiff);//prints the value of the difference for debugging
if(photoDiff>25){ // checks if the difference is too much and the Right LDR is reading much higher than the left
if(solarMoverPos>40){ // checks if the motor is already at the end of one side // this value for rotation comes specifically from the bounds of my setup
solarMoverPos--; //decreases the rotation by 1
solarMover.write(solarMoverPos); // sets the new rotation value.
}
}
if(photoDiff<-25){ // checks if the difference is too much and the left LDR is reading much higher than the right
if(solarMoverPos<140){ // checks if the motor is at the highest value for safe rotation // this value for rotation comes specifically from the bounds of my setup
solarMoverPos++; //increases the rotation by one
solarMover.write(solarMoverPos); // sets the rotation
}
}
delay(15);//delays 20 ms to make sure the machine is not rotating too fast
if(topPhotoResistValue<50 && bottomPhotoResistValue<50){ //checks if both of photoresistors are not getting much light
while(solarMoverPos2 < 90 ){// if the photoresistors are both not getting light it will move the solar panel back to a base position
solarMoverPos2++;//incriments angle
solarMoverHor.write(solarMoverPos2);//updates the angle
delay(10);//delays for 10 ms
}
while(solarMoverPos2 >90){
solarMoverPos2--;//decriments angle
solarMoverHor.write(solarMoverPos2);//updates the angle
delay(10);//delays for 10 ms
}
}
photoDiff2 = topPhotoResistValue - bottomPhotoResistValue;
Serial.println(photoDiff2);
if (photoDiff2 > 25) {
solarMoverPos2--;
solarMoverHor.write(solarMoverPos2);
}
if (photoDiff2 < -25) {
solarMoverPos2++;
solarMoverHor.write(solarMoverPos2);
}
delay(15);
}