Introduction: 'Oort' the Obstacle Avoiding Robot

This is a robot that uses the ultrasonic rangefinder to map out and find obstacles and move around it. It is one of the most practical robot that can be built by young makers and hobbyists.

Step 1: BoM

* Arduino

* L293D IC

* Ultrasonic Sensor

* Servo Motor

* Motor

* Wheels

* Wires

* Breadboard

* Foam board

Step 2: Make Chassis

* Cut out a foam board to the size of the robot that you like ( but not too small that the Arduino and breadboard does not fit in)

* Glue the two motors to the back corners of the frame.

* Glue a castor wheel to the front.

* Snugly fit in the two wheels on to the DC motors

Step 3: Wiring

  1. Connect the first motor to pins 3 and 6. And connect the second motor to pins 11 and 14.
  2. Connect the pins 2, 7, 10, and 15 to pins 3, 5, 6, and 9 on the Arduino board.
  3. Connect pins 4, 5, 12, and 13 all to a common ground to the blue breadboard rail. Then connect the red rail to 5V and blue rail to GND on the Arduino with jumper wires.
  4. `Connect pin 1, 8, 9, and 16 all to a common voltage source, VCC, on the red power rail of the breadboard.

Step 4: Mount Ultrasonic Sensor

* Glue the Ultrasonic Sensor onto the servo horn

* Mount the servo to the front of the robot.

Step 5:

#include  
#include "Ultrasonic.h"
//Constants
const int button = 2;		  //Button pin to pin 2
const int led 	 = 3;		  //Led pin (throught resistor) to pin 3
const int buzzer = 4;		  //Buzzer pin to pin 4
const int motorA1= 6;		  //motor A positive (+) pin to pin 6 (PWM) (from L298 module!)
const int motorA2= 9;         //motor A negative (-) pin to pin 9 (PWM)
const int motorB1=10;		  //motor B positive (+) pin to pin 10 (PWM)
const int motorB2=11;		  //motor B negative (-) pin to pin 11 (PWM)
Ultrasonic ultrasonic(A4 ,A5); //Create Ultrsonic object ultrasonic(trig pin,echo pin)
Servo myservo;  			  //Create Servo object to control a servo
//Variables
int distance;				  //Variable to store distance from an object
int checkRight;
int checkLeft;
int function=0;				  //Variable to store function of robot: '1' running or '0' stoped. By default is stoped
int buttonState=0;            //Variable to store the state of the button. By default '0'
int pos=90;    				  //Variable to store the servo position. By default 90 degrees - sensor will 'look' forward
int flag=0;					  //Useful flag to keep the button state when button is released 
void setup()
{
    myservo.attach(5);  	  //Servo pin connected to pin 5
    myservo.write(pos);        // tell servo to go to position in variable 'pos' 
	pinMode(button, INPUT_PULLUP);
	pinMode(led, 	OUTPUT);
	pinMode(buzzer, OUTPUT);
	pinMode(motorA1,OUTPUT);
	pinMode(motorA2,OUTPUT);
	pinMode(motorB1,OUTPUT);
	pinMode(motorB2,OUTPUT);
	
}
void loop()
{
	//Check button state
	buttonState = digitalRead(button);
	unsigned long currentMillis = millis(); //counting...
	//Change main function (stoped/running) when button is pressed 
  	if (buttonState == LOW) {//If button is pressed once...
  		delay(500);
    	if ( flag == 0){
      		function = 1;
        	flag=1; //change flag variable
    	}
    	else if ( flag == 1){	 //If button is pressed twice
    		function = 0;
	    	flag=0; //change flag variable again 
    	}    
  	}
  	
	if (function == 0){ 	//If button is unpressed or pressed twice then:
		myservo.write(90);  	//set servo 90 degress - sensor will look forward
		stop();					//robot remain stoped
		noTone(buzzer);			//buzzer off
		digitalWrite(led, HIGH);// and led on
		
	}
	else if (function == 1){//If button is pressed then:
		//Read distance...
		distance = ultrasonic.Ranging(CM); //Tip: Use 'CM' for centimeters or 'INC' for inches
		//Check for objects...
		if (distance > 10){
			forward(); //All clear, move forward!
			noTone(buzzer);
			digitalWrite(led,LOW);
		}
		else if (distance <=10){
			stop(); //Object detected! Stop the robot and check left and right for the better way out!
			tone(buzzer,500); // play a tone
			digitalWrite(led,HIGH); // turn the led on
			//Start scanning... 
			for(pos = 0; pos < 180; pos += 1){  //goes from 0 degrees to 180 degrees 
                myservo.write(pos);             //tell servo to go to position in variable 'pos' 
                delay(10);                      //waits 10ms for the servo to reach the position 
            } 
            
            checkLeft = ultrasonic.Ranging(CM);		//Take distance from the left side
            
            for(pos = 180; pos>=0; pos-=1){     //goes from 180 degrees to 0 degrees                           
                myservo.write(pos);             //tell servo to go to position in variable 'pos' 
                delay(10);                      //waits 10ms for the servo to reach the position   
            }
            
            checkRight= ultrasonic.Ranging(CM);
            
            myservo.write(90);                   // Sensor "look" forward again
            
            //Finally, take the right decision, turn left or right?
            if (checkLeft < checkRight){
            	left();
            	delay(400); // delay, change value if necessary to make robot turn.            
            	}
            else if (checkLeft > checkRight){
            	right();
            	delay(400); // delay, change value if necessary to make robot turn.
            }
            else if (checkLeft <=10 && checkRight <=10){
            	backward(); //The road is closed... go back and then left ;)
            	left();
            }
		}
	}
}
void forward(){
	digitalWrite(motorA1, HIGH);
	digitalWrite(motorA2, LOW);
	digitalWrite(motorB1, HIGH);
	digitalWrite(motorB2, LOW);	
}
void backward(){
	digitalWrite(motorA1, LOW);
	digitalWrite(motorA2, HIGH);
	digitalWrite(motorB1, LOW);
	digitalWrite(motorB2, HIGH);
}
void left(){
	digitalWrite(motorA1, HIGH);
	digitalWrite(motorA2, LOW);
	digitalWrite(motorB1, LOW);
	digitalWrite(motorB2, HIGH);
}
void right(){
	digitalWrite(motorA1, LOW);
	digitalWrite(motorA2, HIGH);
	digitalWrite(motorB1, HIGH);
	digitalWrite(motorB2, LOW);	
}
void stop(){
	digitalWrite(motorA1, LOW);
	digitalWrite(motorA2, LOW);
	digitalWrite(motorB1, LOW);
	digitalWrite(motorB2, LOW);
}

Comments

About This Instructable

82views

2favorites

License:

Bio: Aerospace Engineer working in Software Development with a passion for Hardware.
More by TechMartian:Flying Captain America's Shield - RC PlaneWide Angle ShotsLED Ping Pong With Shift Register
Add instructable to: