This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_spg.ino
69 lines (63 loc) · 1.71 KB
/
arduino_spg.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
#include <Servo.h>
//defining Servos
Servo servohori;
int servoh = 0;
int servohLimitHigh = 160;
int servohLimitLow = 20;
Servo servoverti;
int servov = 0;
int servovLimitHigh = 160;
int servovLimitLow = 20;
//Assigning LDRs
int ldrtopl = 2; //top left LDR green
int ldrtopr = 1; //top right LDR yellow
int ldrbotl = 3; // bottom left LDR blue
int ldrbotr = 0; // bottom right LDR orange
void setup(){
servohori.attach(10);
servohori.write(0);
servoverti.attach(9);
servoverti.write(0);
delay(500);
}
void loop(){
servoh = servohori.read();
servov = servoverti.read();
//capturing analog values of each LDR
int topl = analogRead(ldrtopl);
int topr = analogRead(ldrtopr);
int botl = analogRead(ldrbotl);
int botr = analogRead(ldrbotr);
// calculating average
int avgtop = (topl + topr) / 2; //average of top LDRs
int avgbot = (botl + botr) / 2; //average of bottom LDRs
int avgleft = (topl + botl) / 2; //average of left LDRs
int avgright = (topr + botr) / 2; //average of right LDRs
if (avgtop < avgbot){
servoverti.write(servov +1);
if (servov > servovLimitHigh)
servov = servovLimitHigh;
delay(10);
}
else if (avgbot < avgtop){
servoverti.write(servov -1);
if (servov < servovLimitLow)
servov = servovLimitLow;
delay(10);
}
else servoverti.write(servov);
if (avgleft > avgright){
servohori.write(servoh +1);
if (servoh > servohLimitHigh)
servoh = servohLimitHigh;
delay(10);
}
else if (avgright > avgleft){
servohori.write(servoh -1);
if (servoh < servohLimitLow)
servoh = servohLimitLow;
delay(10);
}
else servohori.write(servoh);
delay(50);
}