Introduction: Obstacle Avoiding Robotic Car
How To Build An Obstacle Avoiding Robot
Step 1: Black Box
the first step i used a black box as the base for my robot.
Step 2: Arduino
The Arduino is the brain of the whole system and orchestrate our motors
Step 3: Attaching the Arduino to Blackbox
I attached the arduino to the blackbox using hot glue
Step 4: Ultrasonic Sensor
In Order to make a robot that can move by its own we need some sort of an input, a sensor that fits our goal. An ultrasonic sensor is an instrument that measures the distance to an object using ultrasonic sound waves. An ultrasonic sensor uses a transducer to send and receive ultrasonic pulses that relay back information about an object's proximity
Step 5: Breadboard Connection of Sensor to Arduino
I used wires to male the connection between the breadboard and arduino.
Pay attention that your ping sensor may have a different pin layout but it should have a voltage pin, ground pin, trig pin and an echo pin.
Step 6: Motor Shield
Arduino boards can't control dc motors by their own, because the currents they are generating are too low.To solve this problem we use motor shields.The motor shield has 2 channels, which allows for the control of two DC motors, or 1 stepper motor. ... By addressing these pins you can select a motor channel to initiate, specify the motor direction (polarity), set motor speed (PWM), stop and start the motor, and monitor the current absorption of each channel
Step 7: Connecting Motor Shield to Arduino
Simply attach the motor shield to the arduino with the sensor wires crunched in
Step 8: Connecting the 4 Motors and Batteries to Shield
Every Motor Shield has (at least) two channels, one for the motors, and one for a power source, Connect them with respect to each other
Step 9: Program the Robot
run this code
#include
#include
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
AF_DCMotor motor1(1, MOTOR12_1KHZ); AF_DCMotor motor2(2, MOTOR12_1KHZ); AF_DCMotor motor3(3, MOTOR34_1KHZ); AF_DCMotor motor4(4, MOTOR34_1KHZ); Servo myservo;
#define TRIG_PIN A2 #define ECHO_PIN A3 #define MAX_DISTANCE 150 #define MAX_SPEED 100 #define MAX_SPEED_OFFSET 10
boolean goesForward=false; int distance = 80; int speedSet = 0;
void setup() {
myservo.attach(10); myservo.write(115); delay(2000); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); distance = readPing(); delay(100); }
void loop() { int distanceR = 0; int distanceL = 0; delay(40); if(distance<=15) { moveStop(); delay(50); moveBackward(); delay(150); moveStop(); delay(100); distanceR = lookRight(); delay(100); distanceL = lookLeft(); delay(100);
if(distanceR>=distanceL) { turnRight(); moveStop(); }else { turnLeft(); moveStop(); } }else { moveForward(); } distance = readPing(); }
int lookRight() { myservo.write(50); delay(250); int distance = readPing(); delay(50); myservo.write(100); return distance; }
int lookLeft() { myservo.write(120); delay(300); int distance = readPing(); delay(100); myservo.write(115); return distance; delay(100); }
int readPing() { delay(70); int cm = sonar.ping_cm(); if(cm==0) { cm = 200; } return cm; }
void moveStop() { motor1.run(RELEASE); motor2.run(RELEASE); motor3.run(RELEASE); motor4.run(RELEASE); } void moveForward() {
if(!goesForward) { goesForward=true; motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(FORWARD); motor4.run(FORWARD); for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) { motor1.setSpeed(speedSet); motor2.setSpeed(speedSet); motor3.setSpeed(speedSet); motor4.setSpeed(speedSet); delay(5); } } }
void moveBackward() { goesForward=false; motor1.run(BACKWARD); motor2.run(BACKWARD); motor3.run(BACKWARD); motor4.run(BACKWARD); for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) { motor1.setSpeed(speedSet); motor2.setSpeed(speedSet); motor3.setSpeed(speedSet); motor4.setSpeed(speedSet); delay(5); } void turnLeft() { motor1.run(BACKWARD); motor2.run(BACKWARD); motor3.run(FORWARD); motor4.run(FORWARD); delay(500); motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(FORWARD); motor4.run(FORWARD); }
void turnLeft() { motor1.run(BACKWARD); motor2.run(BACKWARD); motor3.run(FORWARD); motor4.run(FORWARD); delay(500); motor1.run(FORWARD); motor2.run(FORWARD); motor3.run(FORWARD); motor4.run(FORWARD); }