-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino Light Tracker.cpp
More file actions
79 lines (60 loc) · 2.07 KB
/
Arduino Light Tracker.cpp
File metadata and controls
79 lines (60 loc) · 2.07 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <Servo.h>
Servo servoTB; // Servo for Top-Bottom (Vertical)
Servo servoLR; // Servo for Left-Right (Horizontal)
// Analog pins for the photoresistors
int sensorTopR = A2;
int sensorBottomR = A1;
int sensorTopL = A3;
int sensorBottomL = A0;
int const margin = 10;
// Analog pins for Solar panal voltage
// int SolarP = A4;
// Servo degree variables
int posTB = 90; // Initial position TB Servo in the center
int posLR = 90; // Initial position LR servo in the center
const int safeRest = 90; // Saftey for the design
void setup() {
// Connect the servos to the listed pins
servoTB.attach(3);
servoLR.attach(5);
// Start servos at the center position (90).
servoTB.write(posTB);
servoLR.write(posLR);
// Lets us read the voltage from the solar panel, remove slashes for your own use
//pinMode(SolarP, input);
Serial.begin(9600); // Allows us to read analog output in the serial monitor. Helpful for debugging.
}
void loop() {
// Read light from each sensor
int TopR = analogRead(sensorTopR);
int BottomR = analogRead(sensorBottomR);
int TopL = analogRead(sensorTopL);
int BottomL = analogRead(sensorBottomL);
int Top = TopR + TopL;
int Bottom = BottomR + BottomL;
int Left = TopL + BottomL;
int Right = TopR + BottomR;
int TBDifference = Top - Bottom; // Measures the difference in light intensity on an axis
int LRDifference = Left - Right;
// Adjust servo incrementally
if (TBDifference > margin) {
posTB -= 1;
} else if (TBDifference < margin) {
posTB += 1;
} else {}
servoTB.write(posTB);
if (LRDifference > margin) {
posLR -= 1;
} else if (LRDifference < margin) {
posLR += 1;
} else {}
servoLR.write(posLR);
// This is a block of code you should use to make sure your photoresistors are connected correctly
Serial.println(TBDifference);
Serial.println(TopR);
Serial.println(BottomR);
// This block will plot the voltage from the solar panel. Add "//" to remove overhead if not tracking solar panel data.
//SolarVolt = analogRead(SolarP);
//serial.print(SolarVolt);
delay(150); // Delay, adjust as needed
}