Introduction: Obstacle Avoiding Robot

Mabuhay!

Darwin here,

This is for my Artificial Intelligence project in my school and it's my first Arduino project and I would like to share this project to you. In this tutorial making a Robot is not that hard you can make your own by following this Steps.

_____________________________________________________________
NOTE! To be updated for my future projects and have inquires. Please contact me thru this following social media account. I'm willing to help.

Subscribe to my Youtube channel here: https://www.youtube.com/channel/UCHDgqVphwxcUjYaR...

Like my Facebook: https://www.facebook.com/darwindelacruzofficial/

Follow me on twitter: https://www.facebook.com/darwindelacruzofficial/ _____________________________________________________________

Step 1: Building the Chasis

First build the Arduino Robot chassis. The kit, contains the chassis, two motors with the wheels attached, a front wheel, a battery holder, some screws and wires.

Step 2: Connecting the Motor and Motorshield

Solder the thick black and red wires at the motors.

Attach the front wheel. After attached the rear wheels according to the instructions leaflet that comes with kit.

The next step is to attach the switch to the battery holder. Cut the battery holder black wire and solder the one end to separate side of the switch. Our switch is ready. Then attach it to the battery holder using the screw that comes with the chassis kit.

The next step is to connect the motors. We connect the left motor to the M1 connector of the shield like this. We connect the right motor to the M3 connector of the shield this way.

Move on to the motor shield. This shield provides power to the motors and the servo motor and makes our project easier. The motors need a lot of current, and this shield can provide up to 600mA of current to each motor, that’s why we need it. We have to solder 4 wires to the motor shield. These wires will be then connected with the supersonic sensor. We solder one to 5V, one to GND, one to Analog Pin 5, and the last one to analog pin 4.

Next we attach the motor shield to the Arduino Uno board. With a double sided tape attach the Arduino Uno and the Battery holder to the chassis. With a double sided tape also attach the supersonic sensor to the Servo motor and both of them to the robot chassis. I used a hot glue gun to attach the servo motor to the chassis.

Step 3: Configuring the Ultrasonic Sensor and Connection to Servo Motor

In configuring the supersonic sensor we need to this steps.

Supersonic sensor. Vcc goes to 5V via the wire we attached earlier. GND goes to the GND wire. ECHO pin goes to Analog Pin 5, and TRIG pin goes to Analog Pin 4. Next have to connect the power. We connect the black wire from the switch to the motor shield. It goes to the GND pin of this external power connector. The red wire from the switch goes to the M+ connector. If we open the switch we can see that the green LED lights up, the Arduino is receiving power from the batteries.

Lastly we connect the servo motor to the servo_2 slot. The darker color on the right end side red in the middle and orange in the left side order.

Step 4: Codes

The code uses three libraries. Two of them must be downloaded in order the program to compile. The first one is the motor shield driver from Adafruit. The second library is the NewPing library for the supersonic distance sensor. You can find the links for both libraries in here:


Motor Shield Library: https://github.com/adafruit/Adafruit-Motor-Shield...

New Ping Library: https://github.com/adafruit/Adafruit-Motor-Shield...




Codes:

#include
#include #include

#define TRIG_PIN A4 #define ECHO_PIN A5 #define MAX_DISTANCE_POSSIBLE 1000 #define MAX_SPEED 150 // #define MOTORS_CALIBRATION_OFFSET 3 #define COLL_DIST 20 #define TURN_DIST COLL_DIST+10 NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE_POSSIBLE);

AF_DCMotor leftMotor(4, MOTOR12_8KHZ); AF_DCMotor rightMotor(3, MOTOR12_8KHZ); Servo neckControllerServoMotor;

int pos = 0; int maxDist = 0; int maxAngle = 0; int maxRight = 0; int maxLeft = 0; int maxFront = 0; int course = 0; int curDist = 0; String motorSet = ""; int speedSet = 0;

void setup() { neckControllerServoMotor.attach(10); neckControllerServoMotor.write(90); delay(2000); checkPath(); motorSet = "FORWARD"; neckControllerServoMotor.write(90); moveForward(); }

void loop() { checkForward(); checkPath(); }

void checkPath() { int curLeft = 0; int curFront = 0; int curRight = 0; int curDist = 0; neckControllerServoMotor.write(144); delay(120); for(pos = 144; pos >= 36; pos-=18) { neckControllerServoMotor.write(pos); delay(90); checkForward(); curDist = readPing(); if (curDist < COLL_DIST) { checkCourse(); break; } if (curDist < TURN_DIST) { changePath(); } if (curDist > curDist) {maxAngle = pos;} if (pos > 90 && curDist > curLeft) { curLeft = curDist;} if (pos == 90 && curDist > curFront) {curFront = curDist;} if (pos < 90 && curDist > curRight) {curRight = curDist;} } maxLeft = curLeft; maxRight = curRight; maxFront = curFront; }

void setCourse() { if (maxAngle < 90) {turnRight();} if (maxAngle > 90) {turnLeft();} maxLeft = 0; maxRight = 0; maxFront = 0; }

void checkCourse() { moveBackward(); delay(500); moveStop(); setCourse(); }

void changePath() { if (pos < 90) {lookLeft();} if (pos > 90) {lookRight();} }

int readPing() { delay(70); unsigned int uS = sonar.ping(); int cm = uS/US_ROUNDTRIP_CM; return cm; }

void checkForward() { if (motorSet=="FORWARD") {leftMotor.run(FORWARD); rightMotor.run(FORWARD); } }

void checkBackward() { if (motorSet=="BACKWARD") {leftMotor.run(BACKWARD); rightMotor.run(BACKWARD); } }

void moveStop() {leftMotor.run(RELEASE); rightMotor.run(RELEASE);}

void moveForward() { motorSet = "FORWARD"; leftMotor.run(FORWARD); rightMotor.run(FORWARD); for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) { leftMotor.setSpeed(speedSet+MOTORS_CALIBRATION_OFFSET); rightMotor.setSpeed(speedSet); delay(5); } }

void moveBackward() { motorSet = "BACKWARD"; leftMotor.run(BACKWARD); rightMotor.run(BACKWARD); for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2) { leftMotor.setSpeed(speedSet+MOTORS_CALIBRATION_OFFSET); rightMotor.setSpeed(speedSet); delay(5); } }

void turnRight() { motorSet = "RIGHT"; leftMotor.run(FORWARD); rightMotor.run(BACKWARD); delay(400); motorSet = "FORWARD"; leftMotor.run(FORWARD); rightMotor.run(FORWARD); }

void turnLeft() { motorSet = "LEFT"; leftMotor.run(BACKWARD); rightMotor.run(FORWARD); delay(400); motorSet = "FORWARD"; leftMotor.run(FORWARD); rightMotor.run(FORWARD); }

void lookRight() {rightMotor.run(BACKWARD); delay(400); rightMotor.run(FORWARD);} void lookLeft() {leftMotor.run(BACKWARD); delay(400); leftMotor.run(FORWARD);}

Step 5: Schematic Diagram

Comments

author
Swansong made it! (author)2017-04-10

Thanks for sharing :)

author
Darwin Dela Cruz made it! (author)Darwin Dela Cruz2017-04-17

Your welcome :) Mabuhay!

About This Instructable

366views

3favorites

License:

Bio: Mabuhay! I'm a Filipino maker that build Artificial Intelligence and Robotic projects. I'm also a tester and reviewer of latest technology.
More by Darwin Dela Cruz:Portable Monitoring System Using Raspberry Pi (2 USB Camera)Obstacle Avoiding Robot
Add instructable to: