In this article, we going to explain how to make an obstacle-avoiding robot car. you can make this using the Arduino Uno board so easily. Let's get started. If you interested in Arduino Uno projects, refer to my previous articles.
Arduino Uno Obstacle Avoiding Robot Car |
- In this robot car, the ultrasonic sensor acts as an obstacle detector so that the robot car change the traveling path automatically.
- First, you need these components to build this robot car.
Apparatus
1.Arduino Uno board
Arduino Uno board |
2.Ultrasonic sensor (hc-sr04)
Ultrasonic sensor (hc-sr04) |
3. Motor driver (L298N)
Motor driver (L298N) |
4.Servo motor (Gear SG90 9g)
5.Jumper cables
Jumper cables |
6.Dot board
Dot board |
7.four phone battery
phone battery |
8.Toy car wheel with gear
CONNECTION DIAGRAM
Arduino code
#include<Servo.h>
Servo name;
//m1
int enA = 10;
int in1 = 12;
int in2 = 8;
//m2
int enB = 5;
int in3 = 7;
int in4 = 6;
char val;
#define t 11 //trig-white
#define e 13 //echo-black
long duration, cm ;
void setup() {
Serial.begin(9600);
name.attach(9);
name.write(90);
delay(300);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(t, OUTPUT );
pinMode(e, INPUT );
digitalWrite(t, LOW);
delayMicroseconds(5);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
}
void mpower(int motor, int rotation, int spd) {
int pwm;
int pA;
int pB;
if (motor == 1) {
pwm = enA;
pA = in1;
pB = in2;
}
else if (motor == 2) {
pwm = enB;
pA = in3;
pB = in4;
}
else {
return;
}
if (rotation == 0) {
digitalWrite(pA, LOW);
digitalWrite(pB, LOW);
}
else if (rotation == 1) {
digitalWrite(pA, HIGH);
digitalWrite(pB, LOW);
}
else if (rotation == -1) {
digitalWrite(pA, LOW);
digitalWrite(pB, HIGH);
}
analogWrite(pwm, spd);
}
int speedval = 0; //speed of car(max 40)
int maxspeed = 40;
int normalspeed = 127;//less than 125(255/2)
int min_dis = 15;
int mode = 0;
//0-run
//1-scan
//2-rotate
int f_max = 0;
int f_max_ang = 0;
void loop() {
cm = getDistance();
if (mode == 0) {
if (cm > min_dis || cm == 0 ) {
speedval++;
if (speedval > maxspeed) {
speedval = maxspeed;
}
mpower(2, -1, 255);
mpower(1, 1, 255);
delay(100);
}
else {
mpower(1, -1, 255);
mpower(2, 1, 255);
delay(speedval * 6);
speedval = 0;
if (cm > 1 || speedval == 5) {
mode = 1;
}
}
Serial.print("CM");
Serial.print(cm);
Serial.print(",Speed-");
Serial.print(speedval);
Serial.print(",Mode-");
Serial.print(mode);
Serial.println();
}
else if (mode == 1) {
f_max = 0;
name.write(0);
delay(200);
for (int a = 0; a <= 180; a += 10) {
name.write(a);
delay(20);
cm = getDistance();
if (cm > f_max) {
f_max = cm;
f_max_ang = a;
}
Serial.print(",D=");
Serial.print(cm);
}
Serial.print("f_max_ang=");
Serial.print(f_max_ang);
Serial.print("MAX:");
Serial.println(f_max);
name.write(90);
delay(200);
mode = 2;
}
else if (mode == 2) {
if (f_max_ang < 90) {
Serial.println("Rotatiog LEFT");
mpower(1, -1, 255);
mpower(2, -1, 255);
delay(f_max_ang * 10);
}
else {
Serial.println("Rotatiog RIGH");
mpower(1, 1, 255);
mpower(2, 1, 255);
delay((f_max_ang-90) * 10);
}
mpower(1, 0, 0);
mpower(2, 0, 0);
mode = 0;
}
mpower(1, 0, 0);
mpower(2, 0, 0);
//mpower(1, 1, 50);
//mpower(2, -1, 100);
}
long getDistance() {
digitalWrite(t, HIGH);
delayMicroseconds(10);
digitalWrite(t, LOW);
duration = pulseIn(e, HIGH, 100000);
return (duration / 2) / 29.1;
}
0 Comments