in this lesson we are going to see how we can
make a self-driving car so it can not only go forward and backward but it can go back
left back right measure the distance and it uses also ultrasonic sensor and infrared
sensors to make decisions to move and find a path this is very basic self-driving car but
you will learn many ideas from this project welcome to Arduino course by Robojax this course
is based on SunFounder three in one Arduino kit sold on Amazon everything that you learn in
this course is included in this kit the course comes with tons of components and projects all
with instruction and video for smart car and other projects that you can use for school or home
automation you can purchase it from sunfounder.com the kit also comes with full documentation
code download learning projects with full explanation wiring diagram and the
code that you can download a link to purchase the kit and documentation
is below the video in the description in this lesson we are going to see how a
self-driving car basic project works and we will see the code and demonstration now to open the
code click on file open your instant founder three in one click three in one kit Main go to card
project and here number eight self-driving car and then select and open the file this is the code
the code is exactly the same for the rest of the section pins for the motor for Ultrasonic sensor
for infrared and then the functions that you saw that were at the bottom of the code here they have
been moved at the top the only thing that I have added is the distance the serial print and also
in forward I put the serial print backward I put this text for serial print what new you see here
it says back left as you can see one motor here this is a when we the right motor is rotating
backward and the left motor is doing nothing that's why it rotates to the left and I print
this text then for the right is not doing anything left motor is going backwards so it goes to
the right and we print this text and then stop move also I added this text it prints
inside the setup initializing the serial monitors four Motors four pins for the motor
is defined as output and then for Ultrasonic two pencil defined as input and output and for
infrared we Define two pins for left and right both as input inside the loop then we detect we
read the infrared value if it is left or right to see Zero means abstracted or there
is something and one means path is empty here the fun begins at this part where we
have a conditional statement and then else if else if and else so first we check that
if there is if the spot on the right side is empty and on the left side there is an obstacle
then it goes to the back and left because the rate is empty we want to go to the back left so it can
rotate if the left is empty and the right is there is an obstacle it goes back and right so it can go
later on when it turns back the left will be easy to go now if the left and right has obstacle
then it goes backwards with a speed of 150 and else otherwise then this function starts from
there to here and it does all these tasks that you see if the distance is measured which is greater
than 50 then go forward we have 30 centimeter space go forward else if the distance is between
5 10 and 2 it means if the distance is less than 10 and greater than 2 then it means between
2 and 10 then go backward because it's very close to something just go straight backwards
for one second after that it goes back left so here the person who have written this
to just used left he could have used or she could have used right so the car goes to
the back for her back left for half a second else otherwise which the distance is not greater
than 50 and not between 5 and 10 which is which means something in between then it moves
forward again it moves forward and we have 100 millisecond delay I've added this so the code
you know we give the sensor enough time to read forward let's go and add some obstacle if less than seven so it goes backwards
and stops reverse and back left the reason for that is this code in here because
it says move back for one second and back left okay so now let's add obstacle it
goes you see it goes back right because when it rotates like that it goes
back right and now if this sensor detects some obstacle, it goes back left because
something is here, we want to go like that if it is greater than 50,
I'm putting it to a ceiling 120 there is no limit, so they put no limit on the code so regardless it's
working we are going to test it now here is a demonstration of self-driving
car I've created these obstacles and let me put it a little this way
so let's see if it goes here or there, I'm not sure which way it will end up going
so let's power that up connect the power amazing so now this is a bad spot went under the table okay so it cannot see that now let's see much chance let's see wow so let's see if I put this in front of it because there is carpet, I want
this to go somewhere let's see it came back it does not have any of these objects and amazing let's see if it's stuck or it finds the way let's see it seems that stuck
now here it doesn't know no it sounds let me put this, so it turns this
way because under the table is not a fun area and I put another obstacle so it can go back interesting the price like so many times before going now this is hitting heading
to the wall and that's the corner let's see again, going towards the table so so, as you can see this is
going and running perfect