5th Member
5th Member
5th Member
INSTUTUTE OF THECNOLOGY
SCHOOL OF ELECTRICAL AND COMPUTER ENGINERRING
INDUSTRIAL CONTROL STREAM
FINAL BSC PROJECT
TITLE:- DESIGN LINE FOLLOWER AND OBSTACLE AVOIDANCE
ROBOT
NAME ID NO
1- Yonas fsaha.........................................................04253/09
2- Mahder mandefro..............................................02578/09
3- Yilma mengstie….................................................04205/09
4- teame afera........................................................03697/09
5- berihun desye…..................................................1212283
DECLARATION
This is to certify the thesis entitled “Design and simulate a Line follower obstacle
avoiding robot using Arduino” is submitted by us, partial fulfillment of the requirement
for the award of B.sc degree in Electrical and computer Engineering Stream of industry
control, to woldia university.
Name ID NO Signature
Mr. Abemelek
pg. i
2021 G.C
ACKNOWLEDGEMENT
First of all, we want to give special thanks to our almighty GOD, who gave us courage
strength and allowed safe health to prepare this project.
Secondly, we would like to thanks our advisor, Mr. Abemelek for his expert guidance,
constructive comments, suggestions and encouragement for this work could have been
completed. We are also grateful to Woldia University Electrical and Computer
Engineering department teachers and students for their kind full help on giving related
information. They had been a constant source of inspiration throughout our studying
period.
Finally, we would like to extend our thanks to those who helped us with different ideas
and motivation which inspire us for the successful completion of this final year BSC
thesis.
ABSTRACT
Robots are new technologies of human based performer that accomplishes a task settled
forward more accurately than human being. Robots are different types for different
purposes so that different structures. The one we focus on now is line follower with
obstacle avoiding robots. Line follower robot is a hobo car that can follow a path. It is a
machine that follows a certain line (black line on white surface). It uses a sensor for
detecting a line, ultrasonic sensor to detect obstacle, Arduino microcontroller for giving a
decision and motors to progress motion. It is designed to carry materials from one place
to another place. It operates automatically without any human interaction by sensing
color. When the robot near to the obstacle then the ultrasonic sensor gives message signal
to the Arduino to turn left or right and When the robot reaches its end point, the infrared
sensor gives message signal to it and then it stops movement. This concept can be applied
for an autonomous vehicle by applying the similar micro control method line following
robot using Arduino. microcontroller can be modified into a guidance system for
industrial robots that involving traveling in the building. Besides that, it also can apply in
automated cars running on roads. It is designed to move automatically and follow the
made plot line. The robot is used IR sensors to identify the black line avoid the robot to
steer away from its track. robot is driven using dc motors to control the movement of the
wheels. The following process is automatically controlled using infrared sensor and
ultrasonic sensor by having this automatic control system large amount of energy is
saved. This path way for saving energy the result of the simulation obtained indicate that
the system presented has the potential to control moving system to meet requirement of
industry consumption. This document is intended to describe the information regarding
the project. The Arduino coding is developed on using C programming and tested and
verified. The proposed system can be implemented on any commercial, industrial,
medical and also in educational labs.
TABLE OF CONTENT
DECLARATION i
ACKNOWLEDGEMENT ii
ABSTRACT iii
Acronym vii
CHAPTER ONE 1
INTRODUCTION 1
1.1 Background History 1
1.2 Problem Statement 2
1.3 Objectives 3
1.3.1 General Objective 3
1.3.2 Specific Objectives 3
1.4 Significance of The Project 3
1.5 Scope of The Project 4
CHAPTER TWO 5
LITRATURE REVIEW 5
2.1 Backgrounds 5
2.2 History of Robotic 5
2.3 Related Works 5
Difference between Our Project and Related Topics 7
CHAPTER THREE 8
METHODOLOGY 8
3.1 Introduction 8
3.2Block diagram of the system 8
3.3 System Components and there description 9
3.3.1 Software Component Requirement---------------------------------------------------9
3.3.1.1 1A
rduino software (c language) 9
3.3.1.2 Proteus professional 8 software--------------------------------------------------10
3.3.2 Hardware Component Requirement-------------------------------------------------12
3.3.2.1 1Ar
duino 12
3.3.2.2 2IR
Sensor 16
3.3.2.3 Motor Driver (L298N) 18
3.3.2.4Dc motor 20
3.3.2.5Ultrasonic sensor (HC-SR04) 22
3.3.2.6Potentiometer (POT) 24
3.3.2.7Servo motor 26
CHAPTER FOUR 28
SYSTEM DESIGN AND ANALYSIS 28
4.1 Overall operation of the System 28
4.2 System Design 30
4.2.1 Circuit Diagram Design 30
Connection of Arduino pins with L298N 31
Connection of Arduino pins with ultra-sonic sensor----------------------------------31
Connection of Arduino pins with Virtual terminal------------------------------------32
Connection of Arduino pins with Servo motor----------------------------------------33
4.3 System Flow Chart 34
4.4 Result Analysis and discussion 36
4.4.1 esult 36
4.4.2 Discussions 40
CHAPTER FIVE 41
CONCLUSION AND RECOMMENDATION 41
5.1 Conclusion 41
5.2 Recommendation and Future Work 41
Reference 42
Appendix 44
LIST OF FIGURE
Figure 3.1 block diagram of the system...............................................................................8
Figure 3.2 Arduino programming (IDE) software.............................................................10
Figure 3.3 Proteus software diagram.................................................................................12
Figure 3.4 Arduino Nano controller...................................................................................14
Figure 3.5 IR sensor diagram.............................................................................................16
Figure 3.6 IR transmitter....................................................................................................17
Figure 3.7 IR receiver........................................................................................................17
Figure 3.8 IR sensor working principle.............................................................................18
Figure 3.9 L298N Motor driver module............................................................................18
Figure 3.10 L298N motor driver pin description diagram.................................................19
Figure 3.11 working principal diagram of dc motor..........................................................21
Figure 3.12 Gear dc motor.................................................................................................22
Figure 3.13 Diagram of the basic ultrasonic sensor operation...........................................23
Figure 3.14 ultrasonic sensor pin configuration.................................................................23
Figure 3. 15 ultrasonic sensor working principle...............................................................24
Figure 3.16 Potentiometer diagram....................................................................................25
Figure 3.17servo motor operation......................................................................................26
Figure 3.18 pulse width modulation..................................................................................27
Line follower Robots are nothing but self-thinking robots which can take decision itself
using programmed brain without any guidance from human being. There are two types of
Robots in one type robots perform action as we give signals remotely whereas second
type of Robots are self thinking robots which can detect obstacle and move forward. Line
Follower robot is a machine that follows a line, either a black line on white surface or a
white line on black surface. Nowadays, every robot is done with the help of the
microcontroller, and hence the circuit is too big and tough to understand and makes it
costly. For these reasons, in this project we have developed an intelligent Line follower
and obstacle detector robot with a simple concept with simple circuitry.[2]
1
Design and simulate a Line follower and obstacle avoiding woldia university
robot
There are two types of line follower robots: one is black line follower which follows
black line and second is white line follower which follows white line. the line following
robot is the self operating intelligent machine that follows a drawn line on a floor area
and the path line can be visible as black on a white surface area; or a white line on the
black surface area. It is an autonomous robot which identify and tracks either on a black
line in white surface area or a white line in black surface area. Line following robot must
be able to detect a specified line and maintain track on it and do the assign jobs. For
performing job, the given path line must be followed by the designed and developed
robot for special situations. The developed system composed of input, process and output
parts. First read the black/white or white/black path on considered floor and take input
signal for transmission into microcontroller (Arduino) in a process that can be asked and
made the decisions. Microcontroller decided based on the received inputs that can change
(if needed) to be made directions or speeds of the robot. It converts the result to any
directions which can be sent to the line follower speed. the system sends the first or
previous adjusted control signals to speed and directions of line follower robot. To design
a line follower an ultrasonic sensor is needed, which is a device that can measure distance
to an object by using sound waves. It computes the distance between the object and the
line follower by sending a sound wave at a specific frequency and listening for that sound
wave to bounce back. It is important to understand that some objects might not be detect
by ultrasonic sensor. This can be applied for military purposes, delivery services,
transportation systems, blind assistive applications.[3]
pg. 2
2021 G.C
materials and avoid obstacles. Although the line following robot can follow the black line
and electrically operated without any human interaction.
1.3 Objectives
1.3.1 General Objective
The general objective of this project is to design, a line follower and obstacle avoiding
robot using Arduino controller.
In this chapter, the background theory of robot and also the previous project done by
other researchers will be discussed.
2.1 Backgrounds
According to the robot Institute of America, the formal definition of industrial robot is as
follows: A robot is a programmable, multi-functional manipulator designed to move
material, parts or specialized devices through variable programmed motions for the
performance of a variety of tasks [4]. Nowadays, after the massive developments we may
consider a new definition as follows: A robot is the embodiment of manipulative,
locomotive, perceptive, communicative and cognitive abilities in an artificial body,
which may or may not have a human shape. It can advantageously be deployed as a tool,
to make things in various environments [4].
2.2 History of Robotic
In 1709Jacques de Vaucanson’s most famous creation was undoubtedly "The Duck."This
mechanical device could flap its wings, eat, and digest grain. Each wing contained over
four hundred moving parts and even today it remains something of a mystery. The
original Duck has disappeared [5]. In the 18th century, miniature automatons became
popular as toys for the very rich. They were made to look and move like humans or small
animals [6]. ELEKTRO was first unveiled at the 1939 world’s fair. In 1960’sOne of the
first operational, industrial robots in North America appeared in the early 1960’s in a
candy factory in Kitchener, Ontario [7]. In 1964Artificial intelligence research
laboratories are opened at M.I.T., Stanford Research Institute (SRI), Stanford University,
and the University of Edinburgh . In 1965 Carnegie Mellon establishes the Robotics
Institute [7].
2.3 Related Works
“line follower and obstacle avoidance bot using Arduino” has been designed and
developed by Aamir attar, Aadilansari, Abhishekdesai, Shahid khan, Dipashrisonawale to
create an autonomous robot which intelligently detects the obstacle in its path and
navigates
according to the actions that user set for it. So, this system provides an alternate way to
the existing system by replacing skilled labor with robotic machinery, which in turn can
handle more patients in less time with better accuracy and a lower per capita cost.[8]
“Obstacle-avoiding robot with IR and PIR motion Sensors” has been designed and
developed by Aniket D. Adhvaryu et al has proposed that developed robot platform was
not designed for specific task but as a general wheeled autonomous platform. The design
of such robot is very flexible and various methods can be adapted for another
implementation. It shows that PIR sensors are more sensitive compared to IR sensors
while detecting human being.[9]
Obstacle Avoidance Robotic Vehicle Using Ultrasonic Sensor, Android and Bluetooth
for Obstacle Detection” has been designed and developed by Vaghela et.al has mentioned
that enormous amount of work has been done on wireless gesture controlling of robots.
[10] “Obstacle Avoidance Robot” has been designed and developed by Paul Kinsky,
Quan Zhou mentioned that robot with a few mechanical components to add two more
functions to the main body, namely the laptop holder and the camera holder. AT89S52
development board is designed, developed and tested in a large scale, which was
used to control the motors smoothly. the cameras with relatively low cost are fixed and
adjusted on the camera holder for good calibration of the computer vision. Users establish
the serial communication method between the upper laptop and the lower development
board with USB port. The laptop will send out a signal of the motor condition to the
development board.[11]
“Obstacle avoidance & light following robot” in this paper the system is controlled by the
Arduino Uno R3 module which is an advance version of a microcontroller and a part of
embedded system. It has four types of functions. The functions are light following,
obstacle detection and controlling from an android device through Bluetooth or Wi-Fi
module& capture the video clips of that area. The robot has two light detection sensors
which are prepared with LDRs and an infrared obstacle detecting sensor. The sensitivity
of the light sensor can be set by using the trim pots.[12]
“obstacle avoidance robotic vehicle using ultrasonic sensor, Arduino controller” This
project describes about an obstacle avoidance robot vehicle which is controlled by
ultrasonic sensor. The robot is made using ultrasonic sensor and it is controlled by
Arduino
microcontroller. Ultrasonic sensor fixed in front portion of the robot vehicle. This vehicle
is used for detecting obstacle and avoiding the collision. They programmed the controller
to be used with Android app.[13]
“Obstacle avoidance autonomous intelligent robot” Autonomous Intelligent robots which
will perform desired tasks in unstructured environments without continuous human
guidance. It has an infrared sensor which are used to sense the obstacles coming in
between the path of Robot. It drive two gear motors for each wheel.[14]
Potentiometer
Power supply Left front motor
Right front motor
Ultrasonic sensor
Motor driver
Right IR sensor
Arduino
Left IR sensor
Left back motorRight back motor
Servo motor
Schematic Capture
Schematic capture in the Proteus Design Suite is used for both the simulation of designs
and as the design phase of a PCB layout project. It is therefore a core component and is
included with all product configurations.
Microcontroller Simulation
The micro-controller simulation in Proteus works by applying either a hex file or a debug
file to the microcontroller part on the schematic. It is then co-simulated along with any
analog and digital electronics connected to it. This enables its use in a broad spectrum of
project prototyping in areas such as motor control, temperature control and user interface
design. It also finds use in the general hobbyist community and, since no hardware is
required, is convenient to use as a training or teaching tool. Support is available for co-
simulation of:
3D Verification
The 3D Viewer module allows the board under development to be viewed in 3D together
with a semi-transparent height plane that represents the boards enclosure. STEP output
can then be used to transfer to mechanical CAD software such as Solid works or
Autodesk for accurate mounting and positioning of the board.
System Benefits Integrated package with common user interface and fully context
sensitive help.
Figure 3.3 Proteus software diagram
3.3.2.1 Arduino
Arduino is a single-board microcontroller, intended to make the application of interactive
objects or environments more accessible. The hardware consists of an open-source
hardware board designed around an 8-bit Atmel AVR microcontroller, 8 analog input
pins, as well as 14 digital I/O pins which allows the user to attach various extension
boards. Arduino board is an open-source platform used to make electronics projects. It
consists of both a microcontroller and a part of the software or Integrated Development
Environment (IDE) that runs on your PC, used to write & upload computer code to the
physical board. The platform of an Arduino has become very famous with designers or
students just starting out with electronics, and for an excellent cause. Arduino board has
been used for making different engineering projects and different applications. The
Arduino software is
very simple to use for beginners, yet flexible adequate for advanced users. It runs
windows, Linux and Mac. Teachers and students in the schools utilize it to design low-
cost scientific instruments to verify the principles of physics and chemistry. There are
numerous other microcontroller platforms obtainable for physical computing. The
Netmedia’s BX-24, Parallax Basic Stamp, MIT’s Handy board, Phidget and many others
present related functionality. Arduino also makes simpler the working process of
microcontroller, but it gives some advantages over other systems for teachers, students
and beginners.
The Arduino Nano is essentially an Arduino UNO shrunk into a very small profile,
making it very convenient for tight spaces and projects that may need to reduce weight
wherever possible. Like the UNO, the Nano is powered by an Atmega328 processor
operating at 16MHz, includes 32KB of program memory, 1KB of EEPROM, 2KB of
RAM, has 14 digital I/O, 6 analog inputs, and both 5V and 3.3V power rails. (Note -
Arduino Nano boards before V3.0 used the ATmega168 which essentially has half the
specs.)
Figure 3.4 Arduino Nano controller
The Nano, unlike the UNO, cannot connect to Arduino shields but it has pin headers
which make it useful for breadboard prototyping or in PCBs with the use of a socket.
Often, Arduino Nano boards are the cheapest Arduino board option available making
them cost- effective for larger projects.
IR Transmitter or IR LED
Infrared Transmitter is a light emitting diode (LED) which emits infrared radiations
called as IR
LED’s. Even though an IR LED looks like a normal LED, the radiation emitted by it is
invisible to the human eye.
Figure 3.6 IR transmitter
IR Receiver or Photodiode
in1 & in2 Motor A input pins. Used to control the spinning direction of
Motor
in3 & in4 Motor B input pins. Used to control the spinning direction of
Motor
3.3.2.4 Dc motor
The DC motor is the motor which converts the direct current into the mechanical work. It
works on the principle of Lorentz Law, which states that “the current carrying conductor
placed in a magnetic and electric field experience a force”.
To find the distance to the object, simply divide the round-trip distance in half.
The robot uses the Ultrasonic distance sensor to measure the distance in front of it. When
this distance reduces to a particular level, the robot interprets it to mean the presence of
an obstacle in its path. When the robot detects an obstacle in its path, it stops, goes
backward for a few cm, looks around (right and left) by using servo motor then turn
towards the direction that shows more free space in front of it.
In this project we are using two IR sensor modules left sensor and right sensor. When
both left and right sensor senses white then robot move forward.
If right sensor sense black line then robot turn right side until both sensor comes at white
surface.
When white surface comes robot starts moving on forward again.
The Arduino pin out D4 is connected to the input pin IN1 of L298N
The Arduino pin out D5 is connected to the input pin IN2 of L298N
The Arduino pin out D6 is connected to the input pin IN3 of L298N
The Arduino pin out D7 is connected to the input pin IN4 of L298N
The Arduino analog input pin A1 is connected with the trigger of the sensor
The Arduino analog input pin A2 is connected with the echo of the sensor
Initializing
yes
Stop MD<SD
No
Backward
IR
Yes
forward
Stop R=0
IR L=0
No
U.S look right
IR R=O Yes
U.S look left Turn left
IR L=1
No
Forward
No
DR>=DL
Yes
Turn right
Turn left
IR R=1 StFopigur
No Yes
4.12-flow chart of the system IR F=1 e
4.4 Result Analysis and discussion
4.4.1 Result
Condition-1:-
In this case when ultrasonic sensor measures distance of greater than set distance(in our
project greater than 12) and both left and right IR sensors senses white surface(0 binary
number) , then the robot goes forward direction.
In this case when ultrasonic sensor measures distance of greater than set distance(in our
project greater than 12) and left IR sensor sense black surface(1 binary number) and right
IR sensor sense white surface(0 binary number) ,then the robot turning to the left
direction and after some delay goes forward.
Figure 4.14 the robot is turning to left and going forward
Condition-3:-
In this case when ultrasonic sensor measures distance of greater than set distance(in our
project greater than 12) and right IR sensor sense black surface(1 binary number) and left
IR sensor sense white surface(0 binary number) ,then the robot turning to the right
direction and after some delay goes forward.
Figure 4.15 the robot is turning to right and going forward
Condition-4:-
In this case when distance measured by ultrasonic sensor is less than or equal to set
distance(in our project <=12), then the robot stops moving and move backward direction
for some cm and calculate the left and right distance by using servo motor to move the
ultrasonic sensor +90 and -90 degree. If the right distance is greater than or equal to the
distance calculated in the left, then the robot turns to the right, goes forward, turn to the
left to follow the line and goes forward. If the left distance is greater than or equal to the
distance calculated in the right, then the robot turns to the left direction, goes forward,
turn to the right to follow the line and goes forward.
Figure 4.14 the robot avoiding the obstacles
Condition -5:-
In this case when distance measured by ultrasonic sensor is greater than set distance(in
our project greater than 12) and both right IR sensor and left IR sensor senses black(1
binary number) surface, then the robot stops moving and the robot is at final destination.
Figure 4.16 the robot is stop moving, arrive at final destination
4.4.2 Discussions
As we see from previous result analysis our robot moves forward direction, turning to
right and left to fallow the designing line(line path),avoiding obstacles and stop moving if
it is at final destination. We have used the Arduino software (IDE) to run the code, and
the program is run successfully. By using C language code, we have achieved some what
a desired result. We use proteus software to simulate our design circuit, and this shows
how the processed that controls the position and motion with required direction.
We use other components like motor driver and four DC motors. Motor driver used for
driving motors by controlling of direction and speed of the DC motors. DC motor uses
for moving of the robot from place to place. Arduino sends commands to this motor
driver and then it drive motors.
We use IR and ultrasonic sensor that give signal to the Arduino controller and Arduino
gives a signal to motor driver and dc motors are rotated. During the course of designing
this system there were series of problems encountered which came on the way of
achieving the desired goals of this project. we face a problem while we designing the
programming part especially writing a program on the Arduino IDE is a big difficulty,
because we are not familiar with C language. Another problem we face when we are
doing this project is shortage of time this is due to time management.
CHAPTER FIVE
CONCLUSION AND RECOMMENDATION
5.1 Conclusion
We concluded that as we see our project line follower obstacle avoiding robot works
successfuly. We made the obstacle avoidance base line following robot in simulation. As
we show from simulation result a circuit that enabled the robot to follow line based on the
light reflected off the ground and capable of following the lines by comparing light either
side of the different. And in varies positions of obstacle the robot can avoid obstacles by
itself and have shown a good performance. Designing controlling part of the proposed
system, as being the main task of our project, has been achieved with great success,
supported by simulation results.
Our project aims robot that intelligently senses the obstacle and the edge in its path and
navigates according to the behavior that we have set for it. So, what this system provides
is an alternative to the existing system by replacing human labor with robotic machinery,
which in turn can handle more patients in less time with better accuracy and a lower cost.
[3] Pakdaman, M.; Sanaatiyan, M.M, “Design and Implementation of Line Follower
Robot,” Computer and Electrical Engineering, 2009. ICCEE ‘09. Second
International Conference on, vol. vol.2, p. no., pp.585–590, 28–30, Dec. 2009.
[4] Embedded system (Raj Kamal, Pearson education, 2nd edition)
[5] Colak, I., Yildirim, D.,"Evolving a Line Following Robot to use in shopping
centers for entertainment", Industrial Electronics, 2009. IECON '09. 35th Annual
Conference of IEEE, pp.3803 - 3807, 3-5 Nov. 2009.
[7] Pakdaman, M.; Sanaatiyan, M.M., "Design and Implementation of Line Follower
Robot," Computer and Electrical Engineering, 2009. ICCEE '09. Second
International Conference on, vol.2, no., pp.585-590, 28-30 Dec. 2009.
[8] Amir attar, aadilansari, abhishek desai, shahid khan, and dip ashrisonawale, “line
follower and obstacle avoidance bot using arduino,” International Journal of
Advanced Computational Engineering and Networking, vol. 2, pp. 740–741, Aug.
1987.
[9] Aniket D. Adhvaryu et al, “Obstacle-avoiding robot with IR and PIR motion
Sensors,” IOP Conference Series: Materials Science and Engineering, vol. A247,
pp. 529–551, Apr. 2005.
[10] Vaghela Ankit1, Patel Jigar2, Vaghela Savan3, “Obstacle Avoidance Robotic
Vehicle Using Ultrasonic Sensor, Android And Bluetooth For Obstacle Detection,”
International Research Journal of Engineering and Technology (IRJET), vol. A247, pp.
29–32, 2005.
[11] Paul Kinsky,Quan Zhou, “Obstacle Avoidance Robot,” Worcester polytechnic
institute, vol. Vol. 4, pp. 279–285, 1991.
[12] Arnab Saha, Jeet Sanyal, Nousad Mondol, Santanu Ghosh, “Obstacle Avoidance
& Light Following Robot,” International Journal of Advances in Scientific Research and
Engineering (ijasre), vol. Vol. 03, no. Issue 4, May 2017.
[13] R.VAIRAVAN, S.AJITH KUMAR, L.SHABIN ASHIFF, C.GODWIN JOSE,
“obstacle avoidance robotic vehicle using ultrasonic sensor, Arduino controller,”
International Research Journal of Engineering and Technology, vol. Volume: 05, no.
Issue: 02, Feb. 2018.
[14] Ms. S.B. Burrewar , Mr. S.P. Joshi , Mr. A.P. Shingade , Mr. A.N. Shire, “Obstacle
avoidance Autonomous Intelligent Robot,” International Advanced Research Journal in
Science, Engineering and Technology, vol. Vol. 4, no. Issue 3, Jan. 2017.
Appendix
#include <Servo.h> //Servo motor library. This is standard library
//sensor pins
//Sensor Connection
int turnspeed;
int left_sensor_state;
int right_sensor_state;
void setup(){
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
Serial.begin(9600);
delay(3000);
servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
void loop(){
left_sensor_state = digitalRead(left_sensor_pin);
right_sensor_state = digitalRead(right_sensor_pin);
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
turnRight();
delay(turn_delay);
turnLeft();
delay(turn_delay);
}
moveForward();
delay(turn_delay);
moveStop();
while(true){
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);
if (distanceRight>= distanceLeft){
turnRight();
moveStop();
turnLeft();
else{ turnLeft
();
moveStop();
turnRight();
else{ moveForwa
rd();
distance = readPing();
int lookRight(){
servo_motor.write(50);
delay(500);
servo_motor.write(115);
delay(100);
return distance;
int lookLeft(){
servo_motor.write(170);
delay(500);
servo_motor.write(115);
delay(100);
return distance;
delay(100);
int readPing(){
delay(70);
int cm = sonar.ping_cm();
Serial.print("Distance: ");
Serial.println(cm);
if (cm==0){
cm=250;
return cm;
void moveStop(){
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
Serial.println("stop");
void moveForward(){
if(!goesForward){
goesForward=true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
Serial.println("Going forward");
void moveBackward(){
goesForward=false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
Serial.println("Move backward");
void turnRight(){
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(250);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
Serial.println("Turning right");
}
void turnLeft(){
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(250);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
Serial.println("Turning left");