0% found this document useful (0 votes)
10 views

Solar Code

The document defines variables and functions to control multiple servos to adjust the angle of a device based on light sensor readings from different directions. It includes variables for servo positions and limits, functions to read sensor values and calculate differences, and conditional logic to incrementally adjust the servo angles based on the sensor comparisons.

Uploaded by

aman
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Solar Code

The document defines variables and functions to control multiple servos to adjust the angle of a device based on light sensor readings from different directions. It includes variables for servo positions and limits, functions to read sensor values and calculate differences, and conditional logic to incrementally adjust the servo angles based on the sensor comparisons.

Uploaded by

aman
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <Servo.

h>

Servo horizontal; // horizontal servo


int servoh = 180;
int servohLimitHigh = 175;
int servohLimitLow = 5;
// 65 degrees MAX

Servo vertical; // vertical servo


int servov = 45;
int servovLimitHigh = 60;
int servovLimitLow = 1;

Servo myservo; // Cleaning Servo

// LDR pin connections


// name = analogpin;
int ldrlt = A1 ; //LDR top left - BOTTOM LEFT <--- BDG
int ldrrt = A2 ; //LDR top rigt - BOTTOM RIGHT
int ldrld = A0; //LDR down left - TOP LEFT
int ldrrd = A3; //ldr down rigt - TOP RIGHT
int pos = 0;
void setup(){
horizontal.attach(8);
vertical.attach(9);
horizontal.write(180);
vertical.write(45);
myservo.attach(10); //cleaning servo
delay(2500);
Serial.begin(9600);
}
void loop() {
int lt = analogRead(ldrlt); // top left
int rt = analogRead(ldrrt); // top right
int ld = analogRead(ldrld); // down left
int rd = analogRead(ldrrd); // down right
int rain = analogRead(A4); // Rain detection
//Serial.print(rt);
//Serial.print(" ");
//Serial.print(lt);
//Serial.print(" ");
//Serial.print(rd);
//Serial.print(" ");
//Serial.print(ld);
//Serial.println(" ");
//delay(2000);
int dtime = 10; int tol = 40; // dtime=diffirence time, tol=toleransi
int avt = (lt + rt) / 2; // average value top
int avd = (ld + rd) / 2; // average value down
int avl = (lt + ld) / 2; // average value left
int avr = (rt + rd) / 2; // average value right
int dvert = avt - avd; // check the diffirence of up and down
int dhoriz = avl - avr;// check the diffirence og left and rigt

if (-1*tol > dvert || dvert > tol)


{
Serial.println("loop 1");
delay(200);
if (avt > avd)
{
Serial.println("loop 2");
delay(200);
servov = ++servov;
Serial.println("angle increased");
delay(200);
if (servov > servovLimitHigh)
{
servov = servovLimitHigh;
Serial.println("angle Limit High");
}
}
else if (avt < avd)
{servov= --servov;
Serial.println("angle decreased");
if (servov < servovLimitLow)
{ servov = servovLimitLow;
Serial.println("angle Limit Low");
}
}
vertical.write(servov);
}
if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is in the tolerance
else change horizontal angle
{
Serial.println("loop 2");
delay(200);
if (avl > avr)
{
Serial.println("loop 3");
delay(200);
servoh = --servoh;
if (servoh < servohLimitLow)
{
Serial.println("loop 4");
delay(200);
servoh = servohLimitLow;
}
}
else if (avl < avr)
{
Serial.println("loop 5");
delay(200);
servoh = ++servoh;
if (servoh > servohLimitHigh)
{
Serial.println("loop 6");
delay(200);
servoh = servohLimitHigh;
}
}
else if (avl = avr)
{
Serial.println("loop 7");
delay(200);
delay(5000);
}
horizontal.write(servoh);
}

delay(dtime);
if(rain < 400){
int pos;
for (pos = 0; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 120; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in
variable 'pos'
delay(15); // waits 15ms for the servo to reach the
position
}
}

You might also like