-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolartracker.ino
140 lines (115 loc) · 3.73 KB
/
solartracker.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//Solartracker.ino
//Dual axis solar tracking system
//by [email protected]
#include <String.h>
int leftLDRpin = 0;// analog input pins for reading the LDR;s
int rightLDRpin = 1;
int upLDRpin = 2;
int downLDRpin = 3;
int up = 13; //digitial output pin connected to relay to move unit up
int down = 12; // digital output pin connected to relay to move unit down
int left = 11; // digital output pin connected to relay to move unit left
int right = 10;// digital output pin connected to realy to move unit right
int leftLDR = 0;
int rightLDR = 0;
int upLDR = 0;
int downLDR = 0;
int elevation = 0;
int azmith = 0;
int elevationerror = 0;
int azmitherror = 100;
int count = 0;
int movetime = 2000; //amount of time to energize the relays to move the mount
int dark = 300; // set the value for dark to put the panel to sleep for the night.
boolean debug = true; // set to true to print out debugging into to serial console, false to skip printing
int delaytime=20000;//Set the delay time before checks on movement.
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(up, OUTPUT);
pinMode(down, OUTPUT);
pinMode(left, OUTPUT);
pinMode(right, OUTPUT);
digitalWrite(up, HIGH); // since relay is low triggered turn all relay outputs high.
digitalWrite(down, HIGH);
digitalWrite(left, HIGH);
digitalWrite(right, HIGH);
}
void moveup(void) {
digitalWrite(up, LOW);
if (debug) Serial.println("Moving up");
delay(movetime);
if (debug) Serial.println("done");
digitalWrite(up, HIGH);
}
void movedown(void) {
digitalWrite(down, LOW);
if (debug) Serial.println("Moving down");
delay(movetime);
if (debug) Serial.println("done");
digitalWrite(down, HIGH);
}
void moveleft(void) {
digitalWrite(left, LOW);
if (debug) Serial.println("Moving left");
delay(movetime);
if (debug) Serial.println("done");
digitalWrite(left, HIGH);
}
void moveright(void) {
digitalWrite(right, LOW);
if (debug) Serial.println("Moving right");
delay(movetime);
if (debug) Serial.println("done");
digitalWrite(right, HIGH);
}
void nightmove(void){
digitalWrite(right,LOW);
delay(movetime*4);
digitalWrite(right,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
upLDR = analogRead(upLDRpin);
downLDR = analogRead(downLDRpin);
leftLDR = analogRead(leftLDRpin);
rightLDR = analogRead(rightLDRpin);
elevation = (upLDR - downLDR) - elevationerror; //compare up to down to determine if movement is needed;
azmith = (leftLDR - rightLDR) - azmitherror; //compare east to west to determin if movement is needed;
if (debug) Serial.print("Elevation: ");
if (debug) Serial.print(elevation);
if (debug) Serial.print(" Azmith: ");
if (debug) Serial.println(azmith);
if (debug) Serial.print("UP: ");
if (debug) Serial.print(upLDR);
if (debug) Serial.print(" Down: ");
if (debug) Serial.print(downLDR);
if (debug) Serial.print(" Left: ");
if (debug) Serial.print(leftLDR);
if (debug) Serial.print(" Right: ");
if (debug) Serial.print(rightLDR);
if (debug) Serial.println();
if ((upLDR < dark) && (downLDR < dark) && (leftLDR < dark) && (rightLDR < dark)) {
if (debug) Serial.println("dark sleeping in debug so does not really sleep");
nightmove();
if (!debug) delay(7200000);
if (debug) delay(2000);
}
if (debug) Serial.println("Checking elevation");
if (elevation > 100 || elevation < 100) {
if (elevation < 1) {
moveup();
} else {
movedown();
}
}
if (debug) Serial.println("checking azmith");
if (azmith > 100 || azmith < 100) {
if (azmith < 1 ) {
moveleft();
} else {
moveright();
}
}
if (!debug) delay(delaytime);// delay until next check so unit does not just move bank and forth.
}