-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHow To Make The Solar Tracker
95 lines (79 loc) · 2.35 KB
/
How To Make The Solar Tracker
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
If you would like to make the Solar Tracker or something similiar for yourself, I have included documentation of my creation at the following site including instruction, parts, code, and videos.
https://cd-cd-cd-17.github.io/Chris-D-Solar-Tracker/
Here is the code for aligning a device to the highest light position:
// include Servo library
#include <Servo.h>
// horizontal servo
Servo horizontal;
int servoh = 90;
int servohLimitHigh = 350;
int servohLimitLow = 0;
Servo vertical;
int servov = 100;
int servovLimitHigh = 235;
int servovLimitLow = 55;
// LDR pin connections
int ldrTR = A2; // LDR top right
int ldrTL = A1; // LDR top left
int ldrBR = A3; // LDR bottom right
int ldrBL = A0; // LDR bottom left
void setup() {
Serial.begin(9600);
// servo connections
horizontal.attach(9);
vertical.attach(10);
// move servos
horizontal.write(90);
vertical.write(90);
delay(3000);
}
void loop() {
int tr = analogRead(ldrTR); // top right
int tl = analogRead(ldrTL); // top left
int br = analogRead(ldrBR); // bottom right
int bl = analogRead(ldrBL); // bottom left
int dtime = 0; // change for debugging only
int tol = 50;
int avt = (tl + tr) / 2; // average value top
int avd = (bl + br) / 2; // average value bottom
int avl = (tl + bl) / 2; // average value left
int avr = (tr + br) / 2; // average value right
int dvert = avt - avd; // check the difference of up and down
int dhoriz = avl - avr; // check the difference of left and right
// check if the difference is in the tolerance else change vertical angle
if (-1 * tol > dvert || dvert > tol) {
if (avt > avd) {
servov = ++servov;
if (servov > servovLimitHigh) {
servov = servovLimitHigh;
}
}
else if (avt < avd) {
servov = --servov;
if (servov < servovLimitLow) {
servov = servovLimitLow;
}
}
vertical.write(servov);
}
// check if the difference is in the tolerance else change horizontal angle
if (-1 * tol > dhoriz || dhoriz > tol) {
if (avl > avr) {
servoh = --servoh;
if (servoh < servohLimitLow) {
servoh = servohLimitLow;
}
}
else if (avl < avr) {
servoh = ++servoh;
if (servoh > servohLimitHigh) {
servoh = servohLimitHigh;
}
}
else if (avl = avr) {
// nothing
}
horizontal.write(servoh);
}
delay(dtime);
}