Pada malam ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah robot yang dapat menghindari tembok atau halangan yang ada didepannya, robot ini menggunakan 3 buah sensor jarak HC-SRF04 dan menggunakan Arduino sebagai kontrollernya. robot ini jika terdapat halangan didepannya maka dia akan menghindarinya dan akan mencari jalan yang terbaik. untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino Uno
b. Motor Driver L298
c. Motor DC + Gearbox
d. Sensor Jarak HC-SRF04
e. Program Arduino IDE
#define trigPin1 A0
#define echoPin1 A1
#define trigPin2 A2
#define echoPin2 A3
#define trigPin3 A4
#define echoPin3 A5
int mtrkanan1 = 4;
int mtrkanan2 = 5;
int mtrkiri1 = 6;
int mtrkiri2 = 7;
int kecmtr1 = 9;
int kecmtr2 = 10;
long duration1, distance1;
long duration2, distance2;
long duration3, distance3;
void setup() {
Serial.begin(9600);
pinMode(trigPin1,OUTPUT);
pinMode(echoPin1,INPUT);
pinMode(trigPin2,OUTPUT);
pinMode(echoPin2,INPUT);
pinMode(trigPin3,OUTPUT);
pinMode(echoPin3,INPUT);
pinMode(mtrkanan1,OUTPUT);
pinMode(mtrkanan2,OUTPUT);
pinMode(mtrkiri1,OUTPUT);
pinMode(mtrkiri2,OUTPUT);
pinMode(kecmtr1,OUTPUT);
pinMode(kecmtr2,OUTPUT);
}
void loop() {
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1,LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
digitalWrite(trigPin2,LOW);
delayMicroseconds(2);
digitalWrite(trigPin2,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2,LOW);
duration2 = pulseIn(echoPin2,HIGH);
distance2 = (duration2/2) / 29.1;
digitalWrite(trigPin3,LOW);
delayMicroseconds(2);
digitalWrite(trigPin3,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3,LOW);
duration3 = pulseIn(echoPin3,HIGH);
distance3 = (duration3/2) / 29.1;
Serial.print(distance1);
Serial.print(" ");
Serial.print(distance2);
Serial.print(" ");
Serial.println(distance3);
if ((distance1 > 20)&&(distance2 > 20)&&(distance3 > 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,HIGH);
}
if ((distance1 > 20)&&(distance2 < 20)&&(distance3 > 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,HIGH);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,HIGH);
}
if ((distance1 < 20)&&(distance2 < 20)&&(distance3 > 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
if ((distance1 > 20)&&(distance2 < 20)&&(distance3 < 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,HIGH);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,HIGH);
}
if ((distance1 < 20)&&(distance2 < 20)&&(distance3 < 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,HIGH);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
if ((distance1 < 20)&&(distance2 > 20)&&(distance3 > 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
if ((distance1 > 20)&&(distance2 > 20)&&(distance3 < 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,HIGH);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,HIGH);
}
}
#define echoPin1 A1
#define trigPin2 A2
#define echoPin2 A3
#define trigPin3 A4
#define echoPin3 A5
int mtrkanan1 = 4;
int mtrkanan2 = 5;
int mtrkiri1 = 6;
int mtrkiri2 = 7;
int kecmtr1 = 9;
int kecmtr2 = 10;
long duration1, distance1;
long duration2, distance2;
long duration3, distance3;
void setup() {
Serial.begin(9600);
pinMode(trigPin1,OUTPUT);
pinMode(echoPin1,INPUT);
pinMode(trigPin2,OUTPUT);
pinMode(echoPin2,INPUT);
pinMode(trigPin3,OUTPUT);
pinMode(echoPin3,INPUT);
pinMode(mtrkanan1,OUTPUT);
pinMode(mtrkanan2,OUTPUT);
pinMode(mtrkiri1,OUTPUT);
pinMode(mtrkiri2,OUTPUT);
pinMode(kecmtr1,OUTPUT);
pinMode(kecmtr2,OUTPUT);
}
void loop() {
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1,LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1/2) / 29.1;
digitalWrite(trigPin2,LOW);
delayMicroseconds(2);
digitalWrite(trigPin2,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2,LOW);
duration2 = pulseIn(echoPin2,HIGH);
distance2 = (duration2/2) / 29.1;
digitalWrite(trigPin3,LOW);
delayMicroseconds(2);
digitalWrite(trigPin3,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3,LOW);
duration3 = pulseIn(echoPin3,HIGH);
distance3 = (duration3/2) / 29.1;
Serial.print(distance1);
Serial.print(" ");
Serial.print(distance2);
Serial.print(" ");
Serial.println(distance3);
if ((distance1 > 20)&&(distance2 > 20)&&(distance3 > 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,HIGH);
}
if ((distance1 > 20)&&(distance2 < 20)&&(distance3 > 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,HIGH);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,HIGH);
}
if ((distance1 < 20)&&(distance2 < 20)&&(distance3 > 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
if ((distance1 > 20)&&(distance2 < 20)&&(distance3 < 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,HIGH);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,HIGH);
}
if ((distance1 < 20)&&(distance2 < 20)&&(distance3 < 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,HIGH);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
if ((distance1 < 20)&&(distance2 > 20)&&(distance3 > 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,HIGH);
digitalWrite(mtrkanan2,LOW);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,HIGH);
digitalWrite(mtrkiri2,LOW);
}
if ((distance1 > 20)&&(distance2 > 20)&&(distance3 < 20)) {
analogWrite(kecmtr1,250);
digitalWrite(mtrkanan1,LOW);
digitalWrite(mtrkanan2,HIGH);
analogWrite(kecmtr2,250);
digitalWrite(mtrkiri1,LOW);
digitalWrite(mtrkiri2,HIGH);
}
}
f. VIDEO HASILNYA
No comments:
Post a Comment