forked from VivekRamanshBhanot/SEVAK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraMove.ino
211 lines (211 loc) · 4.45 KB
/
CameraMove.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <SoftwareSerial.h>
#include<Servo.h>
Servo m,M;
int TX = 5;
int RX = 6;
int value;
char ch,UorD;
int HEpin=A0; //orange colour wire connected to Hall Effect Sensor
int HEsense,HED=0;
int x;
int AngleMicro;
int record;char Direction;
int CPWD=1,CPWDu=1,LPWD=1,RPWD=1,UPWD=1,DPWD=1;//All the <Button> Pressed Work Done initiators
int Passed=0,temprecord;char tempDirection;//Variables of Pass Function
int rall=7;
SoftwareSerial HC_06(TX, RX); //Bluetooth TX to 10 and Bluetooth RX to 11.
//=====================================================
void setup()
{
Serial.begin(57600);
HC_06.begin(57600);
m.attach(A1); //Servo connected to white colour wire
M.attach(A2); //Servo connected to yellow colour wire
pinMode(HEpin,INPUT);
pinMode(rall,OUTPUT);
digitalWrite(rall,LOW);
delay(100);
AngleMicro=100;
//Rotating the camera to the front view after the vehicle is turned ON
if(HEADC()!=1)
{
while(HEADC()!=1)
M.write(86);
delay(89);
M.write(91);
}
else
{
while(HEADC()==1)
M.write(89);
delay(5);
M.write(97);
delay(200);
M.write(91);
}
}
//Hall Effect Analog to Digital Converter
int HEADC()
{
HEsense=analogRead(HEpin);
if(HEsense<=611 && HEsense>=575)
HED=0;
else if(HEsense>=647)
HED=2;
else if(HEsense<=557)
HED=1;
return HED;
}
//Assign the direction in which the 360 Servo must rotate to be centered
int ClockOrAnti(int R,char D)
{
if(R)
{
if(D=='C')
return 86;
return 97;
}
else
{
if(D=='C')
return 97;
return 86;
}
}
//Keeping record of the direction and magnet number in one "pass"(HEADC() 0 then non 0 )
void Pass(char D)
{
x=HEADC();
if(x!=0)
{
temprecord=x;
tempDirection=D;
Passed=1;
}
else if(Passed==1)
{
record=temprecord;
Direction=tempDirection;
Passed=0;
}
}
//Which direction should the micro servo move to come to the center
char UpOrDown()
{
if(AngleMicro>100)
return 'U';
if(AngleMicro<100)
return 'D';
return 'N';
}
//Function for Camera Movement
void CameraMove(char CH)
{
if(CH=='C')
{
UorD=UpOrDown();
if(UorD=='U')
CPWDu=0;
else if(UorD=='D')
CPWDu=-1;
if(HEADC()==1)
CPWD=-1;
else
CPWD=0;
}
if(CPWD==-1||CPWD==0||CPWDu==0||CPWDu==-1)
{
if(CPWDu==0)
{
if(AngleMicro!=100)
{
AngleMicro-=2;
m.write(AngleMicro);
}
else
CPWDu=1;
}
if(CPWDu==-1)
{
if(AngleMicro!=100)
{
AngleMicro+=2;
m.write(AngleMicro);
}
else
CPWDu=1;
}
if(CPWD==-1)
{
if(HEADC()==1)
M.write(89);
else
{
delay(5);
M.write(97);
delay(200);
M.write(91);
CPWD=1;
}
}
if(CPWD==0)
{
if(HEADC()!=1)
M.write(ClockOrAnti(record,Direction));
else
{
delay(84);
M.write(91);
CPWD=1;
}
}
}
else
{
switch(CH)
{
case 'L': LPWD=0;break;
case 'R': RPWD=0;break;
case 'U': UPWD=0;break;
case 'D': DPWD=0;break;
case '#': M.write(91);LPWD=1;RPWD=1;break;
case '@': UPWD=1;DPWD=1;break;
}
if(LPWD==0)
{
M.write(89);
Pass('A');
}
else if(RPWD==0)
{
M.write(94);
Pass('C');
}
else if(UPWD==0 && AngleMicro>20)
{
AngleMicro-=2;
m.write(AngleMicro);
}
else if(DPWD==0&&AngleMicro<180)
{
AngleMicro+=2;
m.write(AngleMicro);
}
}
}
void loop()
{
if(HC_06.available()> 0 )
{
value = HC_06.parseInt();
ch=HC_06.read();
CameraMove(ch);
Serial.print(value);
Serial.print(" value ");
Serial.print(ch);
Serial.print("\n");
}
else
CameraMove('$');
delay(5);
}