//biaxial solar tracking system using
//arduino uno microcontroler
#include <Servo.h>
#include <LiquidCrystal.h>
const int rs = 7;
const int en = 6;
const int d4 = 12;
const int d5 = 10;
const int d6 = 9;
const int d7 = 8;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
Servo sg90; //initializing a variable for servo named sg90
int initial_position = 90; //Declaring the initial position at 90
int LDR1 = A0;
int LDR2 = A1;
int error = 5;
int servopin=9;
void setup()
{
sg90.attach(servopin); // attaches the servo on pin 9
pinMode(LDR1, INPUT);
pinMode(LDR2, INPUT);
sg90.write(initial_position); //Move servo at 90 degree
lcd.begin(16, 2);
lcd.print("DIRECTION CHANGE");
delay(2000);
}
void loop()
{
int R1 = analogRead(LDR1);
int R2 = analogRead(LDR2);
int diff1= abs(R1 - R2); // Calculating the difference between the LDR's
int diff2= abs(R2 - R1);
if((diff1 <= error) || (diff2 <= error)) {
//if the difference is under the error then do nothing
} else {
if(R1 > R2)
{
initial_position = --initial_position; //Move the servo towards 0 degree
}
if(R1 < R2)
{
initial_position = ++initial_position; //Move the servo towards 180 degree
}
}
sg90.write(initial_position);
lcd.setCursor(0, 1);
lcd.print("intensity ");
lcd.print(millis() / 1000);
delay(100);
}