-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmartbin-mega.ino
521 lines (400 loc) · 11.6 KB
/
smartbin-mega.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#include <Servo.h>
#include "DHT.h"
#include <NewPing.h>
//UltraSonic Pin Area
#define echo1 24
#define trig1 25
#define echo2 26
#define trig2 27
#define echo3 28
#define trig3 29
//Driver Kanan Pin Area
#define ENA1 A0
#define ENA2 A1
#define driver1 30
#define driver2 31
#define driver3 32
#define driver4 33
//Driver Kiri Pin Area
#define ENB1 A2
#define ENB2 A3
#define driver5 34
#define driver6 35
#define driver7 36
#define driver8 37
//Line tracer pin area
#define right 38
#define center 39
#define left 40
//Servo Pin Area
#define servo1 9
int pos = 0;
//#define servo2 //Jaga2
//DHT
#define DHTPIN 3
#define DHTTYPE DHT22
//PULLUP PIN
#define MasterSwitch 22
#define ResetCount 23
//PULLUP MANUAL
#define MasterManual 41
#define PP1 42 //maju
#define PP2 43 //kiri
#define PP3 44 //kanan
#define PP4 45 //mundur
#define PP5 46 //special
//DFPLAYER
int suara = 4;
int counter = 0;
#define MAX_DISTANCE 400
DHT dht(DHTPIN, DHTTYPE);
Servo servo;
long duration_atas, distance_atas, duration_dalam, distance_dalam, duration_depan, distance_depan;
int ValueMasterSwitch, ValueResetCount, ValueMasterManual;
int ValueRight, ValueCenter, ValueLeft;
int P1,P2,P3,P4,P5;
String disatas;
String disdalam;
String disdepan;
String dakanan;
String datengah;
String dakiri;
String jalanke;
String dadhtemp;
String dadhthum;
String isAuto;
NewPing sonar1(trig1, echo1, MAX_DISTANCE);
NewPing sonar2(trig2, echo2, MAX_DISTANCE);
NewPing sonar3(trig3, echo3, MAX_DISTANCE);
//==========================[Millis Function)==========================//
unsigned long previousMillis = 0; // Waktu terakhir cetakan dilakukan
const unsigned long interval = 1000; // Interval waktu antara setiap cetakan (dalam milidetik)
void gosuara(){
digitalWrite(suara,LOW);
delay(100);
digitalWrite(suara,HIGH);
}
//===========================[NORMAL MOVE]==============================//
void maju(){
digitalWrite(driver1, HIGH);
digitalWrite(driver2, LOW);
digitalWrite(driver3, HIGH);
digitalWrite(driver4, LOW);
digitalWrite(driver5, HIGH);
digitalWrite(driver6, LOW);
digitalWrite(driver7, HIGH);
digitalWrite(driver8, LOW);
jalanke = "Maju";
}
void kanan(){
digitalWrite(driver1, LOW);
digitalWrite(driver2, HIGH);
digitalWrite(driver3, LOW);
digitalWrite(driver4, HIGH);
digitalWrite(driver5, HIGH);
digitalWrite(driver6, LOW);
digitalWrite(driver7, HIGH);
digitalWrite(driver8, LOW);
jalanke = "Kanan";
}
void kiri(){
digitalWrite(driver1, HIGH);
digitalWrite(driver2, LOW);
digitalWrite(driver3, HIGH);
digitalWrite(driver4, LOW);
digitalWrite(driver5, LOW);
digitalWrite(driver6, HIGH);
digitalWrite(driver7, LOW);
digitalWrite(driver8, HIGH);
jalanke = "Kiri";
}
void mundur(){
digitalWrite(driver1, LOW);
digitalWrite(driver2, HIGH);
digitalWrite(driver3, LOW);
digitalWrite(driver4, HIGH);
digitalWrite(driver5, LOW);
digitalWrite(driver6, HIGH);
digitalWrite(driver7, LOW);
digitalWrite(driver8, HIGH);
jalanke = "Mundur";
}
void stop(){
digitalWrite(driver1, LOW);
digitalWrite(driver2, LOW);
digitalWrite(driver3, LOW);
digitalWrite(driver4, LOW);
digitalWrite(driver5, LOW);
digitalWrite(driver6, LOW);
digitalWrite(driver7, LOW);
digitalWrite(driver8, LOW);
jalanke = "Stop";
}
//==============================[Special Move]===============================//
void maju_kiri(){
digitalWrite(driver1, HIGH);
digitalWrite(driver2, LOW);
digitalWrite(driver3, LOW);
digitalWrite(driver4, LOW);
digitalWrite(driver5, LOW);
digitalWrite(driver6, LOW);
digitalWrite(driver7, HIGH);
digitalWrite(driver8, LOW);
jalanke = "Maju-Kiri";
}
void maju_kanan(){
digitalWrite(driver1, LOW);
digitalWrite(driver2, LOW);
digitalWrite(driver3, HIGH);
digitalWrite(driver4, LOW);
digitalWrite(driver5, HIGH);
digitalWrite(driver6, LOW);
digitalWrite(driver7, LOW);
digitalWrite(driver8, LOW);
jalanke = "Maju-Kanan";
}
void slide_kanan(){
digitalWrite(driver1, HIGH);
digitalWrite(driver2, LOW);
digitalWrite(driver3, LOW);
digitalWrite(driver4, HIGH);
digitalWrite(driver5, HIGH);
digitalWrite(driver6, LOW);
digitalWrite(driver7, LOW);
digitalWrite(driver8, HIGH);
jalanke = "Slide-Kanan";
}
void slide_kiri(){
digitalWrite(driver1, LOW);
digitalWrite(driver2, HIGH);
digitalWrite(driver3, HIGH);
digitalWrite(driver4, LOW);
digitalWrite(driver5, LOW);
digitalWrite(driver6, HIGH);
digitalWrite(driver7, HIGH);
digitalWrite(driver8, LOW);
jalanke = "Slide-Kiri";
}
void mundur_kanan(){
digitalWrite(driver1, LOW);
digitalWrite(driver2, LOW);
digitalWrite(driver3, LOW);
digitalWrite(driver4, HIGH);
digitalWrite(driver5, LOW);
digitalWrite(driver6, HIGH);
digitalWrite(driver7, LOW);
digitalWrite(driver8, LOW);
jalanke = "Mundur-Kanan";
}
void mundur_kiri(){
digitalWrite(driver1, LOW);
digitalWrite(driver2, HIGH);
digitalWrite(driver3, LOW);
digitalWrite(driver4, LOW);
digitalWrite(driver5, LOW);
digitalWrite(driver6, LOW);
digitalWrite(driver7, LOW);
digitalWrite(driver8, HIGH);
jalanke = "Mundur-Kiri";
}
void lacteral_arc(){
digitalWrite(driver1, LOW);
digitalWrite(driver2, HIGH);
digitalWrite(driver3, LOW);
digitalWrite(driver4, LOW);
digitalWrite(driver5, HIGH);
digitalWrite(driver6, LOW);
digitalWrite(driver7, LOW);
digitalWrite(driver8, LOW);
jalanke = "Lacteral-Arc";
}
void setup() {
//Serial
Serial.begin(9600);
servo.attach(servo1);
//setup enable driver
analogWrite(ENA1, 255);
analogWrite(ENA2, 255);
analogWrite(ENB1, 255);
analogWrite(ENB2, 255);
//setup driver
pinMode(driver1, OUTPUT);
pinMode(driver2, OUTPUT);
pinMode(driver3, OUTPUT);
pinMode(driver4, OUTPUT);
pinMode(driver5, OUTPUT);
pinMode(driver6, OUTPUT);
pinMode(driver7, OUTPUT);
pinMode(driver8, OUTPUT);
//setup DFplayer
pinMode(suara,OUTPUT);
digitalWrite(suara,HIGH);
//setup line tracer
pinMode(right, INPUT);
pinMode(center, INPUT);
pinMode(left, INPUT);
//setup ultrasonic
pinMode(trig1, OUTPUT);
pinMode(echo1, INPUT);
pinMode(trig2, OUTPUT);
pinMode(echo2, INPUT);
pinMode(trig3, OUTPUT);
pinMode(echo3, INPUT);
//PullUp Pin
pinMode(MasterSwitch, INPUT_PULLUP);
pinMode(ResetCount, INPUT_PULLUP);
//Pullup manual
pinMode(PP1, INPUT_PULLUP);
pinMode(PP2, INPUT_PULLUP);
pinMode(PP3, INPUT_PULLUP);
pinMode(PP4, INPUT_PULLUP);
pinMode(PP5, INPUT_PULLUP);
}
void loop() {
ValueMasterSwitch = digitalRead(MasterSwitch);
ValueResetCount = digitalRead(ResetCount);
ValueMasterManual = digitalRead(MasterManual);
if(ValueResetCount == 0) return counter = 0;
if(ValueMasterManual == 1){
isAuto = "true";
if(ValueMasterSwitch == 0){
ValueRight = digitalRead(right);
ValueCenter = digitalRead(center);
ValueLeft = digitalRead(left);
if(ValueRight == 1){
dakanan = "HIGH";
} else {
dakanan = "LOW";
}
if(ValueCenter == 1){
datengah = "HIGH";
} else {
datengah = "LOW";
}
if(ValueLeft == 1){
dakiri = "HIGH";
} else {
dakiri = "LOW";
}
distance_atas = sonar1.ping_cm();
disatas = String(distance_atas);
//Serial.print(distance_atas);
//Serial.println(" Centimeter");
distance_dalam = sonar2.ping_cm();
disdalam = String(distance_dalam);
//Serial.print(distance_dalam);
//Serial.println(" Centimeter");
distance_depan = sonar3.ping_cm();
disdepan = String(distance_depan);
//Serial.print(distance_depan);
//Serial.println(" Centimeter");
delay(50);
//------------------------------< Read DHT22 >-----------------------------------//
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
//-----------------------< DHT REMOVE NAN DISPLAY >---------------------------//
if(isnan(temp)){
dadhtemp = "-";
} else{
dadhtemp = String(temp);
}
if(isnan(humidity)){
dadhthum = "-";
} else {
dadhthum = String(humidity);
}
if(distance_dalam > 0 && distance_dalam < 10){
if(distance_depan > 0 && distance_depan < 20) return stop();
if(ValueCenter == 0 && ValueRight == 1 && ValueLeft == 1){
maju();
//jalanke = "Maju";
}
if(ValueCenter == 0 && ValueRight == 0 && ValueLeft == 1){
kanan();
//jalanke = "Kanan";
}
if(ValueCenter == 0 && ValueRight == 1 && ValueLeft == 0){
kiri();
//jalanke = "Kiri";
}
if(ValueCenter == 1 && ValueRight == 0 && ValueLeft == 1){
kanan();
//jalanke = "Kanan";
}
if(ValueCenter == 1 && ValueRight == 1 && ValueLeft == 0){
kiri();
//jalanke = "Kiri";
}
if(ValueCenter == 1 && ValueRight == 1 && ValueLeft == 1){
stop();
//jalanke = "Stop";
}
if(ValueCenter == 0 && ValueRight == 0 && ValueLeft == 0){
stop();
//jalanke = "Stop";
}
} else {
if(distance_atas > 0 && distance_atas < 40){
servo.writeMicroseconds(1000);
pos = -720;
delay(7000);
servo.writeMicroseconds(1500);
pos = 0;
delay(3000);
servo.writeMicroseconds(2000);
pos = 720;
gosuara();
delay(4000);
servo.writeMicroseconds(1500);
pos = 0;
counter = counter + 1;
} else {
//myservo.write(0); //do nothing
}
}
} else {
//Serial.println("Bin Disable"); //do nothing
}
} else {
isAuto = "false";
P1 = digitalRead(PP1);
P2 = digitalRead(PP2);
P3 = digitalRead(PP3);
P4 = digitalRead(PP4);
P5 = digitalRead(PP5);
//------------------------------------[MANUAL MOVE]-------------------------------------//
if(P1 == LOW && P2 == HIGH && P3 == HIGH && P4 == HIGH && P5 == HIGH){
maju();
} else if(P1 == HIGH && P2 == LOW && P3 == HIGH && P4 == HIGH && P5 == HIGH){
kiri();
} else if(P1 == HIGH && P2 == HIGH && P3 == LOW && P4 == HIGH && P5 == HIGH){
kanan();
} else if(P1 == HIGH && P2 == HIGH && P3 == HIGH && P4 == LOW && P5 == HIGH){
mundur();
} else if(P1 == LOW && P2 == HIGH && P3 == LOW && P4 == HIGH && P5 == HIGH){
maju_kanan();
} else if(P1 == LOW && P2 == LOW && P3 == HIGH && P4 == HIGH && P5 == HIGH){
maju_kiri();
} else if(P1 == HIGH && P2 == HIGH && P3 == LOW && P4 == LOW && P5 == HIGH){
mundur_kanan();
} else if(P1 == HIGH && P2 == LOW && P3 == HIGH && P4 == LOW && P5 == HIGH){
mundur_kiri();
} else if(P1 == HIGH && P2 == HIGH && P3 == LOW && P4 == HIGH && P5 == LOW){
slide_kanan();
} else if(P1 == HIGH && P2 == LOW && P3 == HIGH && P4 == HIGH && P5 == LOW){
slide_kiri();
} else {
stop();
}
}
//---------------------[ Send Serial To Nodemcu ESP8266 ]----------------------//
//Serial.println("testto");
//Serial.println("*"+disatas+","+disdalam+","+disdepan+","+dakanan+","+datengah+","+dakiri+","+jalanke+","+dadhtemp+","+dadhthum+","+String(counter)+","+isAuto+"#"); //inject wrapping
//delay(1000);
unsigned long currentMillis = millis(); // Ambil waktu saat ini
// Periksa apakah sudah mencapai interval waktu yang ditentukan
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Perbarui waktu terakhir
Serial.println("*"+disatas+","+disdalam+","+disdepan+","+dakanan+","+datengah+","+dakiri+","+jalanke+","+dadhtemp+","+dadhthum+","+String(counter)+","+isAuto+"#"); //inject wrapping
}
}