-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.ino
37 lines (35 loc) · 1.29 KB
/
Code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <Servo.h> // Including the servo header for using servo funtion codes
Servo myservo;
int pos = 90; // initial position
int sens1 = A0; // LRD 1 pin
int sens2 = A1; //LDR 2 pin
int tolerance = 2;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(sens1, INPUT);
pinMode(sens2, INPUT);
myservo.write(pos);
delay(2000); // A 2 seconds delay while we position the solar panel
}
void loop()
{
int val1 = analogRead(sens1); // read the value of sensor 1
int val2 = analogRead(sens2); // read the value of sensor 2
if((abs(val1 - val2) <= tolerance) || (abs(val2 - val1) <= tolerance)) {
//do nothing if the difference between values is within the tolerance limit
} else {
if(val1 > val2) // Comparing the voltage difference across the two ldr
{
pos = --pos; // Postion of the solar panel is adjusted towards the sun
}
if(val1 < val2)
{
pos = ++pos;
}
}
if(pos > 180) { pos = 180; } // reset to 180 if it goes higher
if(pos < 0) { pos = 0; } // reset to 0 if it goes lower
myservo.write(pos); // write the position to servo
delay(50);
}