-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTesla_Ultra_Pro.ino
325 lines (286 loc) · 4.94 KB
/
Tesla_Ultra_Pro.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include <Servo.h>
#include <NewPing.h>
#include <IRremote.h>
#define UNLOCK 16736925 // 2
#define LOCK 16754775 // 8
#define RECV_PIN 3
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long val;
unsigned long previousMillis = 0;
const long interval = 500;
#define IN1 8 // Left wheel forward
#define IN2 9 // Left wheel reverse
#define IN3 10 // Right wheel reverse
#define IN4 11 // Right wheel forward
#define ECHO_PIN A1
#define TRIG_PIN A2
#define AF digitalRead(5) // Abyss forward
#define LT digitalRead(7) // Light sensor
#define LWF digitalRead(8)
#define LWR digitalRead(9)
#define RWR digitalRead(10)
#define RWF digitalRead(11)
#define max_distance 150
bool carUnlock = false;
int distance = 0;
#define LED 1
Servo myservo;
NewPing sonar(TRIG_PIN, ECHO_PIN, max_distance);
void forward()
{
myservo.write(90);
delay(500);
distance = readPing();
if (distance >= 20)
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
}
void back()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void left()
{
myservo.write(165);
delay(500);
distance = readPing();
if (distance >= 20)
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
}
void right()
{
myservo.write(15);
delay(500);
distance = readPing();
if (distance >= 20)
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
}
void stopcar()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
void stopcarButCanBack()
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
if (Serial.available())
{
String data = Serial.readStringUntil('\n');
if (data == "b")
{
back();
delay(700);
}
}
}
void unLockCar()
{
if (irrecv.decode(&results))
{
val = results.value;
irrecv.resume();
if (LWF == LOW and LWR == LOW and RWR == LOW and RWF == LOW){
switch (val)
{
case UNLOCK:
carUnlock = true;
stopcar();
break;
case LOCK:
carUnlock = false;
stopcar();
break;
default:
break;
}
}
}
}
int readPing()
{
int cm = sonar.ping_cm();
if (cm == 0)
{
cm = 150;
}
return cm;
}
int avoidCollision()
{
distance = readPing();
if (distance > 100)
{
return 0; // safe - green
}
else if ((100 > distance) && (distance > 20) )
{
return 1; // yellow
}
else if (distance <= 20)
{
return 2; // red
}
}
void autoTurnOnLed(){
if (LT == HIGH){
digitalWrite(4,HIGH);
}
else{
digitalWrite(4,LOW);
}
}
void currentData()
{
String result;
if (carUnlock == true)
{
result += "1";
}
else
{
result += "0";
}
if (LWF == HIGH and LWR == LOW and RWR == LOW and RWF == HIGH)
{
result += "1";
} // forward
if (LWF == LOW and LWR == HIGH and RWR == HIGH and RWF == LOW)
{
result += "2";
} // back
if (LWF == HIGH and LWR == LOW and RWR == LOW and RWF == LOW)
{
result += "3";
} // left
if (LWF == LOW and LWR == LOW and RWR == LOW and RWF == HIGH)
{
result += "4";
} // right
if (LWF == LOW and LWR == LOW and RWR == LOW and RWF == LOW)
{
result += "5";
} // stop
if (AF == HIGH)
{
result += "1";
}
else
{
result += "0";
}
if (LT == HIGH)
{
result += "1";
}
else
{
result += "0";
}
int lval = avoidCollision();
if (lval == 0)
{
result += "0";
}
else if (lval == 1)
{
result += "1";
distance = readPing();
result += String(distance);
}
else if (lval == 2)
{
result += "2";
distance = readPing();
result += String(distance);
}
Serial.println(result);
}
void setup()
{
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
stopcar();
irrecv.enableIRIn();
pinMode(AF, INPUT);
pinMode(LT, INPUT);
pinMode(4, OUTPUT);
digitalWrite(4,LOW);
myservo.attach(12);
myservo.write(90);
}
void loop()
{
unLockCar();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
currentData();
previousMillis = currentMillis;
}
if (carUnlock == true)
{
autoTurnOnLed();
distance = readPing();
if (distance <= 20)
{
stopcarButCanBack();
}
if(AF == HIGH){
stopcarButCanBack();
}
if (Serial.available())
{
String data = Serial.readStringUntil('\n');
if (data == "f")
{
forward();
}
if (data == "r")
{
right();
}
if (data == "l")
{
left();
}
if (data == "b")
{
back();
}
if (data == "s")
{
stopcar();
}
}
}
else
{
stopcar();
}
}