-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolar_tracker.ino
More file actions
57 lines (42 loc) · 1.03 KB
/
solar_tracker.ino
File metadata and controls
57 lines (42 loc) · 1.03 KB
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
#include <Servo.h>
const int LDR_LEFT_PIN = 2;
const int LDR_RIGHT_PIN = 3;
const int SERVO_PIN = 9;
Servo tracker;
int angle = 90;
const int CENTER_ANGLE = 90;
const int STEP = 5;
const int MIN_ANGLE = 30;
const int MAX_ANGLE = 150;
const int LOOP_DELAY = 80;
void setup() {
pinMode(LDR_LEFT_PIN, INPUT);
pinMode(LDR_RIGHT_PIN, INPUT);
tracker.attach(SERVO_PIN);
tracker.write(angle);
Serial.begin(9600);
}
void loop() {
int left = digitalRead(LDR_LEFT_PIN);
int right = digitalRead(LDR_RIGHT_PIN);
left = (left == 0) ? 1 : 0;
right = (right == 0) ? 1 : 0;
if (left == 1 && right == 0) {
angle -= STEP;
}
else if (right == 1 && left == 0) {
angle += STEP;
}
else if (left == 1 && right == 1) {
if (angle < CENTER_ANGLE) angle += STEP;
else if (angle > CENTER_ANGLE) angle -= STEP;
}
angle = constrain(angle, MIN_ANGLE, MAX_ANGLE);
tracker.write(angle);
Serial.print(left);
Serial.print(",");
Serial.print(right);
Serial.print(",");
Serial.println(angle);
delay(LOOP_DELAY);
}