in this lesson we are going to use the infrared or sensors so we can avoid obstacle and when we
detect our core detects an object it moves to the left and if nothing is detected it goes forward
and if it is on the left it goes to the right or if it is in front of it goes backward let's see
welcome to Arduino course by Robojax this course is based on SunFounder 3-in-1 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 welcome to Arduino course by Robojax
this is very important that before you come to this lesson you should have watched the video
that we learned how to control the car moving to the left right forward or backwards and also
you should have watched the introduction to infrared sensor so we don't have to spend
time and explain all this because we are just going to explain the obstacle avoidance in this
lesson now in this lesson we are going to use infrared with obstacle avoidance
if you go to Car Projects number five is play with obstacle avoidance module we have already seen
how to use this module and how to detect and we saw that it works about 20 centimeters
so to make sure that this must be aligned if they are not aligned it will not work
it will not detect properly so make sure that they are aligned in the same line
because when they are shaped they're a little bent and we are going to build something like
this so this one is detecting an obstacle on the right side this is on the left side
so how this works is that we saw that this can work up to 20 centimeter as the car drives this
transmitter is sending infrared and it will hit the object and comes back if the object is close
enough this will detect it so we know that an obstacle is on the right side on this side so
we can move the car either to the left or to the right so what we do is there is another one
we check if there is an obstacle and there is no obstacle if this doesn't detect so what it
means is this is empty so we move this way if the obstacle is on this side and this side is empty
we move to the right F in both sides there is nothing we just go at the middle and we continue
so, this might not work perfectly because some obstacle items might be at the middle,
and this will not detect it because this is detecting in two ways uh so in the next lesson,
we are going to put ultrasonic sensor here where we can also
measure the distance and obstacle and that will make it a better smart car
now let's do the wiring for this first I have taken from these 5 volts
a wire to this point where from here I'm taking it
to the motor to the motor driver and the same way I've got a ground to this point so
so this is my five volts and that is my ground on this module and here is the wiring I'm following
exact color that we have here the red is connected to a point which is connected to
five volts so the red is connected to Positive Black is connected to the negative for both of
these and for the right sensor for this it is connected to pin seven so for me here as well
and here I've connected the red to positive and the red from here to positive so this is
my positive where it is connected to 5 volts again and from here this is ground so this
point is ground and I've connected black wire from the this one to ground and black wire from
that one to ground in here now the right sensor the right infrared is connected to this point
exactly here the empty spot the 27 and this one is connected to pin 8.
now let's open the code click on file open the folder where you have
downloaded and inside that three in one kit main go to card project
and number five is obstacle avoidance module click and click select it and click open
here is the code the code has some error I fixed it so please pay attention while I'm explaining
it this part is the same as the main code then we have defined right IR pin 7 and left IR pin 8 the
infrared sensor on the right side is connected to 7 and the left side is connected to 8.
and then this this is for the right motor and these two are for the left motor we have seen
it before and here we using pin mode the left pin is defined as input and also the right pin
setup is done and then here inside the loop using digital right we read the left infrared and the
result which is zero or one zero mean abstracted there is something and one mean empty space
the result will be stored in this variable called left and then we are reading also the same way
for the right will be stored in a variable called write and then here we set the speed of our curve
which is from 0 to 255 so this is 150 for learning purpose so you under and here is the fun we read
this means left if one means empty so if we detect this will be high now this means
not high which means if this is low it will be true and the right is empty so the left side
is not empty and the right is true remember the right one mean empty if that is true then we move
uh back to the right so this is a function I'm not changing it the name how it is however it
is so this was incorrect that left and right was were incorrect I swapped them
and else f the left is empty and right is not empty then go to left so this means not then
we are driving the car with the speed that we have and here the motor will drive the car to the left,
ELSE IF and then another condition if we have something on the left if the
left is not empty and the right is also not empty there is something then move backwards
and here if otherwise so otherwise means there is nothing on the right nothing on the left then the
core will move forward and back left and right these are the functions that we have seen it in
the first lesson the only thing that I've added is this text so we can see the text it says go
forward so we know this function is running and then this is for backward go backwards and then
back to the left right this prints go left go right and that's it that was the whole code
now let me explain it here with a Serial monitor the code is loaded as you can see
we have no obstacle and this right and left sensors detect nothing so the core is moving
forward I have disconnected the motor so we can see and learn better now I'm getting this box
and using it as an obstacle and when I put it here you can see that the light is on and we are going
and the car is moving to the left when you see this printing it means the function is working
perfectly later on we are going to test it now I'm going to lift it and put it in front of
this as soon as I lift it as you can see it says going forward now let's put it on the left side
and it says right which means the car is moving to the right and proceeds that way now if I left it
again it goes forward there is no obstacle now I'm gonna put it in front of both of these let's see
both of these are detected this obstacle and it says going backwards go backward
so our core will move backwards and if I leave it goes forward and as you can see
the code seems to be working perfectly we are going to test it now practically on the floor
I have modified the code because we did not have any stop and when the obstacle is detected we stop
the car for one second I've added this and then it continues and then otherwise when it goes to
the left as where we have to stop and when the car goes backward also we have to stop for one second
if nothing happens if it goes forward there is nothing or there is no obstacle
then there is no stop now the stop is here at the bottom I've defined it and I've set
all the zeros this is for one motor we just send zero zero motor stops and this one also
zero zero the motor stops but we print this and in order to prevent any error or minimum
stop if the user defines uh here 1000 if they put anything less than one 500 then
we say if it is over 500 then accept the value that the user enter entered and stop the car
otherwise at least stop for 500 milliseconds which is half a second and then continue
I heard if we have opened the serial monitor I'm
putting the obstacle as you can see on the left it says stop and then it went
stopped and then it goes to the right and if I go to the right stopped and it goes to the
left and if I put it in front of it goes backward and then continues if I put it just goes backward
and it checks and stop backwards and stop because both of these conditions are true
and here I have created these obstacles and my hope is that the car will like this so My Hope Is
that the car will face this obstacle rotate to the right and it hits this obstacle and then turn left
goes there once it reaches there then it finds the path and goes until it reaches that obstacle
when it reaches that either goes back or turns left or right so let's see if it goes that way
let's power up the car
excellent
[Music]
thank you
[Music]