Tuesday, December 30, 2014

Protocol controlled robot

Control a robot using various Wireless Protocols
Everyone, from a basic level kid to brainy scientists, will not create robots without controlling it by wires. This make a sense and practice of wired controlled robots. Hooking up with wires, snipping it, soldering it again and again makes the process tedious and the root looks like a crap.
So, controlling a robot without touching it has been a great challenge. Then advancements in technology and science brought out various ways of controlling a robot using various communication protocols. Hmmmm…………!
clip_image002What does a protocol mean?
Ø A protocol is a set of communication languages between two
Electronic components. The common language which the components Can exchanges information and data between them. Ex. A digital device cannot communicate to analog device without the use of A to D converter between them. Here the A to D converter act as a translator between the devices.
Common wireless protocols for robots:
Ø Radio frequency modules
· Bluetooth
· 433 Mhz Transmitter and receiver
· 2.4 Ghz Transmitter and recevier
· Zigbee radio
· Wi-fi
Ø GSM
Ø Infra-red waves
As there are many protocols available for communication, these are the reliable and the easily configurable wireless modules for robotics.

 Let’s make a Bluetooth controlled robot:
Time Duration:
Expert’s: 30 minutes
Starter’s: 60 minutes
 

Overview of Bluetooth:
Ø Bluetooth is a wireless protocol utilizing short-range communications technology facilitating data transmission over short distances. Bluetooth uses frequency hopping in timeslots. Bluetooth has been designed to operate in noisy radio frequency environments, and uses a fast acknowledgement and a frequency-hopping scheme to make the communications link robust, communication-wise. Bluetooth radio modules avoid interference from other signals by hopping to a new frequency after transmitting or receiving a packet. The Bluetooth radio is built into a small microchip and operates in a globally available frequency band ensuring communication compatibility worldwide.
Ø The Bluetooth specification has two power levels defined;
· a lower power level that covers the shorter personal area within a room, and
· clip_image004a higher power level that can cover a medium range, such as within a home.
A simple Bluetooth communication:
clip_image006
ü The one which transfers becomes the master and the one which receives becomes the slave.
Pin connections in Bluetooth module HC-05/HC-06:
Vcc, Gnd, Tx, Rx
clip_image008
Ø The pin orders may vary according to model and manufacturer.
Let’s have fun with Bluetooth>
Checking the Bluetooth >>
Steps:
Ø Power the Bluetooth module using the Arduino Uno by connecting the pins below. The Arduino Uno should be powered via the data cable or with the help of external battery. You can also use FTDI converter to power the Bluetooth module by connecting it to the computer.
clip_image010
Ø Once the Bluetooth module is powered, the LED on the Bluetooth starts blinking until it gets paired to other device.
Ø Turn ON the Bluetooth of your mobile and search for he Bluetooth named HC-05 or HC-06 depending upon the model.
Ø Then connect the Bluetooth device and it asks for the pairing code. Enter the pairing code as ‘1234’ [ Just type 1234 ]
Ø clip_image012Then add the device to your mobile. And the LED in the Bluetooth stops blinking.
Yeah! Our Bluetooth module is working fine>
v As there are many ways to check the working of Bluetooth, this will be appropriate enough to proceed with the Bluetooth controlled robots.
 
Now let’s make the Bluetooth controlled robot>
There are two ways by which you can control the robot
>Using a terminal program in PC
> By using a Mobile phone app (Android)
Both the ways are easier to control and which has the same building configuration of the robot.
Things you will need:
Ø Arduino Uno
Ø Breadboard
Ø Bluetooth module HC-05/HC-06
Ø L293D Motor driver IC
Ø Single strand wires
Ø 12V battery and 9V battery
Ø Chassis
Ø Dummy axles
Ø 2 set of wheels
Ø A pair of 500rpm DC motors
Ø Android based smart phone and PC
Follow the steps:
 clip_image014clip_image016
> Assemble the chassis with the dummy axles and DC motors and attach the wheels to it firmly.
clip_image020
 
clip_image018
 
 
 
 
 
 
 
 



> Place the breadboard and Arduino board on the chassis and fix it using double side tapes.
clip_image022
Ø Pin the Bluetooth module into the breadboard and wire the pin connections of the Bluetooth to the Arduino board for making up the wireless connection.
Ø Now place the L293D IC on the breadboard and connect the pins of the IC as said in the previous project. [Scroll above to see the connections again]
Ø Follow the pin connections below for your reference.
Ø Make sure that the Arduino board do not make any electrical contact with the chassis, in case the chassis is made up of metal.
Ø Now the frame work of the robot is ready and let’s program the Arduino.
 
Pin connections:

clip_image024Fix the L293D IC on the breadboard and wire all the pins of the IC accordingly as shown in the pin configuration below:
· motor1 Pin1 = 3 on Arduino // pin 2 on L293D IC
· motor1 Pin2 = 4 on Arduino // pin 7 on L293D IC
· enable1 Pin = 5 on Arduino // pin 1 on L293D IC
· motor2 Pin1 = 8 on Arduino // pin 10 on L293D IC
· motor2 Pin2 = 9 on Arduino // pin 15 on L293D IC
· enable2 Pin = 10 on Arduino // pin 9 on L293D IC
· The out 1,2 and 3,4 is connected to motor pins.
clip_image025Fix the Bluetooth on the breadboard and make the connection below:
Ø VCC pin of Bluetooth to 5V pin of Arduino Uno
Ø Gnd pin of Bluetooth to Gnd pin of Arduino Uno
Ø Tx pin of Bluetooth to Rx pin of Arduino Uno
Ø Rx pin of Bluetooth to Tx pin of Arduino Uno
clip_image027clip_image028

clip_image030
Let’s program the Arduino>
Ø The codes and algorithm is same as previous robot. The functions for moving the robot forward, backward, left and right are set and it is executed only when a command is given. This command is sent serially as output by the user via the Bluetooth in PC/Mobile and Arduino reads the serial input via the receiving Bluetooth. As per the command, specific function is enabled until the next command is received by the Arduino.
clip_image031
Hmmm….. What is serial communication mean?
 



Let’s learn some stuff>
Ø The word serial means “one after the other”. Information is passed back and forth between the computer and Arduino by setting the pin HIGH or LOW. Serial interfaces stream their data, one single bit at a time. These interfaces can operate on as little as one wire, usually never more than four.

clip_image033



clip_image035
clip_image037






In case of Bluetooth module, this communication happens in terms of wireless manner by the form of radio waves in air. Then the serial communication happens between the Bluetooth module and Arduino board for data communication.
clip_image038The following code does the function given below:
Robot moves,
Ø Forward – if 1 is pressed in Bluetooth enabled device
Ø Backward - if 5 is pressed in Bluetooth enabled device
Ø Left - if 2 is pressed in Bluetooth enabled device
Ø Right - if 4 is pressed in Bluetooth enabled device
Ø Stop - if 3 is pressed in Bluetooth enabled device


>> You can change the numbers in the program for which the specific function takes place.


int motor1Pin1 = 3; // pin 2 on L293D IC
int motor1Pin2 = 4; // pin 7 on L293D IC
int enable1Pin = 5; // pin 1 on L293D IC
int motor2Pin1 = 8; // pin 10 on L293D IC
int motor2Pin2 = 9; // pin 15 on L293D IC
int enable2Pin = 10; // pin 9 on L293D IC
int state;

void setup() {
    // sets the pins as outputs:
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
   pinMode(enable1Pin, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(enable2Pin, OUTPUT);
    // sets enable1Pin and enable2Pin high so that motor can turn on:
    digitalWrite(enable1Pin, HIGH);
    digitalWrite(enable2Pin, HIGH);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}

void loop() {
    //if some date is sent, reads it and saves in state
    if(Serial.available() > 0){     
      state = Serial.read();   
    }   
    // if the state is '1' the DC motor will go forward
    if (state == '1') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
        }
    
    
    // if the state is '2' the motor will turn left
    else if (state == '2') {
    
        digitalWrite(motor1Pin1, HIGH); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
    }
    // if the state is '3' the motor will Stop
    else if (state == '3' ){
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
        }
    // if the state is '4' the motor will turn right
    else if (state == '4') {
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, HIGH); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
    }
    // if the state is '5' the motor will Reverse
    else if (state == '5') {
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, HIGH);
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
    }
}

Yup! Upload the program into the Arduino and the robot is ready to control>
clip_image043
Steps to control the robot>

By using a Terminal program in PC/Laptop
ü In case of using a PC/laptop, make sure you have the Bluetooth connected with it. Most Laptops have in-built Bluetooth with it. But for PC’s, buy a Bluetooth and insert it to the computer.
ü After installing the Bluetooth device, Right click on Bluetooth icon on the right corner of the task bar and click add a device or you can go to control panel and add a device by that way.
ü It will search for the device and it shows HC-O5. [Make sure to turn on the Bluetooth device on the Arduino]
ü Add the passcode as 1234 and the Bluetooth gets paired. This is identified by the LED indication on the Bluetooth device. The LED light’s continuously if it’s get paired to any device.
ü Now then download any terminal software on your PC/Laptop such as Hyper Terminal, Tera Term and install it. Hope it’s free to download and use.
ü Open the Hyper terminal program and choose a name and icon to new connection. Now a window opens for you to choose a port for making connection. Choose the outgoing port number which is allocated for your Bluetooth device. This can be found out by right clicking on the Bluetooth device in PC/Laptop and going to ‘properties’ and clicking ‘Hardware’ tab.
clip_image045clip_image047
clip_image049clip_image051
clip_image052ü After choosing the correct port, the hyper terminal will establish the serial communication with the Bluetooth device connected with Arduino.
ü Once done, the robot is ready to control. Type the number in the hyper terminal workspace to do a specific function in the robot.
clip_image054
By using an Android app:
Ø Instead of using the terminal program in PC/laptop, you can use your android phone to control the robot functions.
Ø Install the app from the link below:
Click here to get the app

Ø After installing it, pair the Bluetooth device HC-05 to your mobile phone using the passcode 1234.
Ø After pairing, open the app, and search for device and connect again. Once connected, you can click the buttons in the app for doing the specific functions of the robot.
Follow the video for the step by step procedure:





clip_image058
clip_image060


                             Yeah! We controlled the robot using the Bluetooth.


Monday, December 15, 2014

Range Finding Autonomous Cat

     
     
                                                                                                                                                                         Arduino and Advanced robotics
b1

                   Robotics is been fun and playful for kids and even for youngsters. But, it’s been a challenging scenario for engineers and researchers to build an advanced electro-mechanical machines. Science and engineering plays a greater role to develop a new technology for building advanced robots. Science and mathematics lies behind from a tiny little walking robot (which is called as ‘toys’ for normal humans) to a bigger size humanoid robot. Everyone admires and appreciates the product and the creator, but not the science and maths behind it.

images (1)              
      It’s not about equations and coding. It’s about curiosity and interest. So, here  is  your place to learn robotics in advancements with fun and maths!

 
> Let’s smash up things and play with robots…………!


Cool Smiley

    Basic things you will need:
     > Scissors, Cello tapes, Double side tapes, a few pieces of card boards, screw driver, wire cutter, soldering iron, PC/Laptop with Arduino IDE software for programming.



Range finding Autonomous cat

A wheeled robot which can avoid the obstacles in its visibility by finding and comparing range of the object in its view by mimicking the function of cute kitty. It’s autonomous in function. This robot is a basic start to autonomous robotics.

Time Duration:
>Expert’s: 40 minutes
>Starter’s: 180 minutes 
                 
Things you will need:
>Ultrasonic sensor
>Arduino based microcontroller- Arduino Uno
>Single strand wires / Jumper wires
>Servo motor
>Bread board
>L293D driver IC
>2 set of Wheels
>1 set Dc motors (500rpm)
>Dummy axles
>Chassis
>12V Battery

You can get these components from a local electronic stores market or from online stores such as www.ebay.in / www.ebay.com or you can refer to your advisor.



Sensor overview:

Ultrasonic sensor / Ping sensor:
 Ultrasonic sensor is simply a transducers which evaluate the attributes of the target by interpreting the echoes from radio or sound waves. It functions by measuring the time interval between sending the signal and receiving the echo to determine the distance to an object.

Working of an ultrasonic sensor:









Ultrasonic sensor has 4 pins/ 3 pins:
>Vcc, Gnd, Trigger, Echo
OR
>Vcc, Gnd, SIG
>The trigger and echo pins are connected in a single pin ‘SIG’

Servo motor:
>Servo motors has three simple parts: a DC motor, potentiometer and a control circuit. The motor is attached by gears to the control wheel. As the motor rotates, the potentiometer's resistance changes, so the control circuit can precisely regulate how much movement there is and in which direction.

When the shaft of the motor is at the desired position, power supplied to the motor is stopped. If not, the motor is turned in the appropriate direction. The desired position is sent via electrical pulses through the signal wire. The motor's speed is proportional to the difference between its actual position and desired position. So if the motor is near the desired position, it will turn slowly, otherwise it will turn fast. This is called proportional control.

Servos are controlled by sending an electrical pulse of variable width, or pulse width modulation (PWM), through the control wire. There is a minimum pulse, a maximum pulse, and a repetition rate. A servo motor can usually only turn 90 degrees in either direction for a total of 180 degree movement. The motor's neutral position is defined as the position where the servo has the same amount of potential rotation in the both the clockwise or counter-clockwise direction. The PWM sent to the motor determines position of the shaft, and based on the duration of the pulse sent via the control wire; the rotor will turn to the desired position. The servo motor expects to see a pulse every 20 milliseconds and the length of the pulse will determine how far the motor turns.


Working of a servo motor:



Servo motor connections:





L293D IC:
L293D is a motor driver IC which has the H-Bridge circuit inbuilt within it. It can control two motors by enabling the corresponding input and output pins of the IC.





Pin configuration:
Enable and input pins should be supplied with 5V from the Arduino board.
Vs, Vss should be powered separately from external battery.
Make sure to place the IC in a correct direction by identifying the notch.



Let’s understand and have fun with components used>


Programming the Arduino:
<Programming is just a way to speak/communicate with the components>

Download the Arduino IDE software from the link below, www.arduino.cc/en/Main/Software

Install suitable drivers to your PC/Laptop for uploading the codes into the Arduino Uno.

<Open the New sketch and use the code below to understand the working of thr components use to build the cat robot>

Let’s do programming >                    

At first let us work on ultrasonic sensor>

Step 1:

Connect the Arduino Uno to the PC which has Arduino IDE software. Type the codes below, which is used to find the distance of the obstacle using the ultrasonic sensor.

Wire the sensor:
Connect the ‘trigger pin’ to ‘pin 12’ of Arduino Uno and ‘Echo pin’ to ‘pin 11’ of Arduino Uno.
Connect the ‘Vcc pin’ and ‘Gnd pin’ to ‘5V pin’ and ‘Gnd Pin’ of Arduino Uno.




coding:

#include 

#define TRIGGER_PIN   12   // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11      // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200   // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);  // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(9600);   // Open serial monitor at 9600 baud to see ping results.
}

void loop() {
  delay(50);                      // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

  unsigned int uS = sonar.ping();   // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM);   // Convert ping time to distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");
}

Download the “New Piing” library from www.arduino.cc website and place it in the library folder of Arduino in PC. [The ‘trigger pin’ and ‘echo pin’ can be replaced by ‘SIG pin’ in the program depending upon the model of sensor]

Output:
Ø Use the serial monitor to see the output from Arduino Uno.
Ø The above code is used to test the ultrasonic sensor by giving the distance of the objects as output in the serial monitor of Arduino IDE. Try focusing the ultrasonic sensor to various objects at various distances.




We are now able to see the working of ultrasonic sensor in action.
Let’s Centre the servo:

Step 2:
Take the servo and connect the wires of the servos to the Arduino as said below:

Identify the Vcc, Gnd, Signal wires of the servo motor correctly >
· Vcc pin of servo to 5V pin on Arduino
· Gnd pin of servo to Gnd pin on Arduino
· Signal pin of Servo to pin 9 on Arduino


#include 
Servo servo;
void setup()
{
  servo.attach(9);
}

void loop()
{
  servo.write(90); 
}
Use the code below to centre the servo at 90 degree position in case of 0 to 180 degree servo.




Output:
Ø You will find that your servo has turned to 90 degree/centred.
Thus the servo has been centred >



Let’s see how L293D works>
Ø Fix the L293D IC on the breadboard and wire all the pins of the IC accordingly as shown in the pin configuration below:

· motor1 Pin1 = 3 on Arduino // pin 2 on L293D IC
· motor1 Pin2 = 4 on Arduino // pin 7 on L293D IC
· enable1 Pin = 5 on Arduino // pin 1 on L293D IC
· motor2 Pin1 = 8 on Arduino // pin 10 on L293D IC
· motor2 Pin2 = 9 on Arduino // pin 15 on L293D IC
· enable2 Pin = 10 on Arduino // pin 9 on L293D IC

>Connect the output 1, 2 and output 3, 4 to the motor 1 and motor 2 respectively.
>Connect the VCC and V+ to a external battery of 6V to 12V. This will act as a power supply to the motors.

Schematic circuit:




Coding:


int motor1Pin1 = 3; // pin 2 on L293D IC
int motor1Pin2 = 4; // pin 7 on L293D IC
int enable1Pin = 5; // pin 1 on L293D IC
int motor2Pin1 = 8; // pin 10 on L293D IC
int motor2Pin2 = 9; // pin 15 on L293D IC
int enable2Pin = 10; // pin 9 on L293D IC
int state;

void setup() {
    // sets the pins as outputs:
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
   pinMode(enable1Pin, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(enable2Pin, OUTPUT);
    // sets enable1Pin and enable2Pin high so that motor can turn on:
    digitalWrite(enable1Pin, HIGH);
    digitalWrite(enable2Pin, HIGH);
    // initialize serial communication at 9600 bits per second:
    Serial.begin(9600);
}

void loop() {
    //if some date is sent, reads it and saves in state
    if(Serial.available() > 0){     
      state = Serial.read();   
    }   
    // if the state is '1' the DC motor will go forward
    if (state == '1') {
        digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
        }
    
    
    // if the state is '2' the motor will turn left
    else if (state == '2') {
    
        digitalWrite(motor1Pin1, HIGH); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
    }
    // if the state is '3' the motor will Stop
    else if (state == '3' ){
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
        }
    // if the state is '4' the motor will turn right
    else if (state == '4') {
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, HIGH); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);
    }
    // if the state is '5' the motor will Reverse
    else if (state == '5') {
        digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, HIGH);
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW);
    }
}
Ø Upload the codes into the Arduino and open the serial monitor and type 1 or 2 or 3 or 4 or 5 for respective actions to take place. You should have the data cable connected to the Arduino Uno.

Ø Thus the motor direction is controlled using a serial interface between the Arduino and PC with the help of L293D motor driver.

Let’s jump into action:
>Attach the Dc motors and dummy axles to the chassis and connect the wheels to it. Make sure to fix the wheels tight enough to the axles.


 

 
 


>Then place a cardboard piece above the chassis (if the chassis is made of metal) to mount the micro-controller board and the bread board on the chassis.
>This is done, so as to reduce any electrical conductivity between the metal chassis and the micro-controller board.
>Mount the servo motor on the front end of the chassis and attach the ultrasonic sensor over its head by using a double side tape.
>Use L-clamps to support the servo motor firmly to the chassis of the robot.
>Follow the steps below for connecting the micro-controller (Arduino Uno) to the ultrasonic sensor, L293D driver and the servo motor.


Circuit diagram:


Connections overview>
Servo motor- signal wire to pin 11 on Arduino Uno
Ultrasonic sensor- Echo pin to pin 6 on Arduino Uno
Ultrasonic sensor- Trigger pin to pin 7 on Arduino Uno
L293D IC- Input 1, 2 (motor 1) to pin 3, 4 on Arduino Uno
Input 3, 4 (motor 2) to pin 8, 9 on Arduino Uno
Enable 1 (motor 1) to pin 5 on Arduino Uno
Enable 2 (motor 2) to pin 10 on Arduino Uno






Programming the Arduino for Autonomous cat robot:
The program is simple and involves the combinations of above implied programs for ultrasonic sensor, servo motor and L293D motor driver.

Function of the program:
>The ultrasonic sensor is swept 30 degree towards left and right from servos centre position.
>This determines the range of object on the left and right of the robot’s front end.
>The distance is compared and the robot is moved along the direction which has obstacle at a longer distance. This is looped again and again and the robot moves in a confined space by avoiding the obstacles in its way autonomously

Coding:


#include  //include Servo library
int motor1Pin1 = 3; // pin 2 on L293D IC
int motor1Pin2 = 4; // pin 7 on L293D IC
int enable1Pin = 5; // pin 1 on L293D IC
int motor2Pin1 = 8; // pin 10 on L293D IC
int motor2Pin2 = 9; // pin 15 on L293D IC
int enable2Pin = 10;
const int trigpin = 7; 
const int echopin = 6; 
const int dangerThresh = 15; //threshold for obstacles (in cm)
int leftDistance, rightDistance; //distances on either side
Servo panMotor;  
long duration; //time it takes to recieve PING))) signal

void setup()
{
 pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
   pinMode(enable1Pin, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(enable2Pin, OUTPUT); 
  panMotor.attach(11); //attach motors to proper pins
  panMotor.write(90); //set PING))) pan to center
}

void loop()
{
  int distanceFwd = ping();
  if (distanceFwd>dangerThresh) //if path is clear
  {
  digitalWrite(motor1Pin1, HIGH);
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH);   //move forward
  }
  else //if path is blocked
  {
    digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, LOW);
    panMotor.write(60); 
    delay(1000);
    rightDistance = ping(); //scan to the right
    delay(1000);
    panMotor.write(120);
    delay(1000);
    leftDistance = ping(); //scan to the left
    delay(1000);
    panMotor.write(90); //return to center
    delay(1000);
    compareDistance();
  }
}
  
void compareDistance()
{
  if (leftDistance>rightDistance) //if left is less obstructed 
  {
    digitalWrite(motor1Pin1, HIGH); 
        digitalWrite(motor1Pin2, LOW); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW); //turn left
    delay(1000); 
  }
  else if (rightDistance>leftDistance) //if right is less obstructed
  {
    digitalWrite(motor1Pin1, LOW); 
        digitalWrite(motor1Pin2, HIGH); 
        digitalWrite(motor2Pin1, LOW);
        digitalWrite(motor2Pin2, HIGH); //turn right
    delay(1000);
  }
   else //if they are equally obstructed
  {
     
  digitalWrite(motor1Pin1, LOW);
        digitalWrite(motor1Pin2, HIGH); 
        digitalWrite(motor2Pin1, HIGH);
        digitalWrite(motor2Pin2, LOW); //turn 180 degrees
    delay(1000);
  }
}

long ping()
{
  // Send out PING))) signal pulse
  pinMode(trigpin, OUTPUT); 
pinMode(echopin,INPUT); 
digitalWrite(trigpin, LOW); 
delayMicroseconds(2); 
digitalWrite(trigpin, HIGH); 
delayMicroseconds(10); 
digitalWrite(trigpin, LOW); 
  
  //Get duration it takes to receive echo
  duration = pulseIn(echopin, HIGH); 

//Convert duration into distance 
return duration / 29 / 2; 
} 

>Change the connections of respective “input pins” accordingly to rotate the motor in particular direction specified by the program
>You can modify the angle given to sweep the sensor according to your robot size. You can modify the program according to the model of ping sensor used as 4 pins or 3pins.
>Once the program is uploaded, Connect the batteries separately for Arduino and L293D driver and place the robot on the floor and see the fun and the behaviour of robot >




Check out the video below:












Finally we made it. An autonomous cat that rolls on the Surface by avoiding its obstacles on its way, by using its Micro-controller as its brain!


Let’s rock on to next robot >


Presented by,
Robotics and Artificial Intelligence Foundation-RAIF