-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSun_Tracker.ino
153 lines (134 loc) · 2.68 KB
/
Sun_Tracker.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
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <Servo.h>
int top_servo=9;
int bottom_servo=10;
int ldr_tr,ldr_tl,ldr_br,ldr_bl;
int spd_top,spd_bottom;
int mini;
int s1=80,s2=80;
Servo servo_top,servo_bottom;
void setup() {
Serial.begin(9600);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(top_servo,OUTPUT);
pinMode(bottom_servo,OUTPUT);
servo_top.attach(top_servo);
servo_bottom.attach(bottom_servo);
servo_top.write(80);
servo_bottom.write(80);
delay(2000);
}
void read_ldr(){
ldr_tr=analogRead(A0);
ldr_tl=analogRead(A1);
ldr_br=analogRead(A2);
ldr_bl=analogRead(A3);
ldr_tr=map(ldr_tr,0,750,0,300);
ldr_tl=map(ldr_tl,0,750,50,300);
ldr_br=map(ldr_br,0,750,0,300);
ldr_bl=map(ldr_bl,0,750,0,300);
mini=min(min(ldr_tr,ldr_tl),min(ldr_br,ldr_bl));
}
void move_right(){
if(s2<160){
s2++;
servo_bottom.write(s2);
}
}
void move_left(){
s2--;
servo_bottom.write(s2);
}
void move_up(){
if(s1<160){
s1++;
servo_top.write(s1);
}
}
void move_down(){
s1--;
servo_top.write(s1);
}
void move_upleft(){
if(s1<160){
s1++;
}
s2--;
servo_bottom.write(s2);
servo_top.write(s1);
}
void move_upright(){
if(s1<160){
s1++;
}
if(s2<160){
s2++;
}
servo_top.write(s1);
servo_bottom.write(s2);
}
void move_downleft(){
s1--;
s2--;
servo_top.write(s1);
servo_bottom.write(s2);
}
void move_downright(){
s1--;
if(s2<160){
s2++;
}
servo_top.write(s1);
servo_bottom.write(s2);
}
void printldr(){
Serial.print("ldr_tr = ");
Serial.print(ldr_tr);
Serial.print(" ldr_tl = ");
Serial.print(ldr_tl);
Serial.print(" ldr_br = ");
Serial.print(ldr_br);
Serial.print(" ldr_bl = ");
Serial.println(ldr_bl);
Serial.print(" MIN = ");
Serial.print(mini);
if(mini==ldr_tr)
Serial.println(" ldr_tr");
else if(mini==ldr_tl)
Serial.println(" ldr_tl");
else if(mini==ldr_br)
Serial.println(" ldr_br");
else if(mini==ldr_bl)
Serial.println(" ldr_bl");
}
int diff=20;
void loop() {
read_ldr();
printldr();
/*
servo_top.write(160);
servo_bottom.write(160);
*/
//move_left();
if(mini==ldr_tr && mini==ldr_tl && mini==ldr_br && mini==ldr_bl);
else if((ldr_tr-mini)<=diff && (ldr_tl-mini)<=diff)
move_down();
else if((ldr_tr-mini)<=diff && (ldr_br-mini)<=diff)
move_downright();
else if((ldr_br-mini)<=diff && (ldr_bl-mini)<=diff)
move_up();
else if((ldr_tl-mini)<=diff && (ldr_bl-mini)<=diff)
move_downleft();
else if((ldr_tr-mini)<=diff)
move_downright();
else if((ldr_tl-mini)<=diff)
move_downleft();
else if((ldr_br-mini)<=diff)
move_upleft();
else if((ldr_bl-mini)<=diff)
move_upright();
else;
delay(100);
}