YTread Logo
YTread Logo

Arduino Robot Car with Speed Sensors - Using Arduino Interrupts

Jan 02, 2022
today in the workshop we are going to assemble a little

robot

car base and we will learn how to use those

speed

encoder disks that come with these kits we will also learn about

using

interrupts

with the Arduino so there is a lot to do today so let's get started welcome to the workshop hello and welcome to the workshop today we are going to work with a little

robot

car base now if you have been in robotics for any length of time i am sure you have seen these things advertised on ebay and amazon they are little bases very inexpensive that are available in two and four wheel configurations and are really great for getting started with robotics now i have two of these bases here to show you first off this one right here this kit cost me about 15 bucks and it comes with everything , the wheels, the base itself, a battery holder, the motor is a wheel, all the different parts and this point you would pass it on a little screwdriver that I thought was very nice another one i got the freedom less money i bought this at one of my local electronics stores for around $12.98 this one here has all the parts the other one had it even has a mount for a servo motor and axis sensor to make it I got the pan and tilt mechanism as included with this too - such amazing values ​​for the money a part that is included with both and no one seems to tell you what to do however it is this and this is a little sensor you put on the wheel to measure the rotation of the wheel you can measure the

speed

and by doing so you can measure the distance how much the wheel rotates and get a lot of useful information but like I said there aren't many instructions to use it well I intend to change that today so what we're going to do is put together a robot car base the reason i have two here is there are two different styles of motor mounts so i want to show how the motor goes to that and then we are going to add a few extra components on that so we can experiment with this speed sensor and i can show you how it actually works now the components you are going to add are as follows we are going to add an Arduino Uno and that will be the microcontroller that is the brain of the project is a l2 98n motor driver and we've seen that it's a dual h bridge driver and of course it will drive the 2 dc motors and this device here which is an optical source sensor that is meant to be used with the little wheel I showed you guys to measure rotation so there's a lot to do today so let's get ready and build a little robot car so let's take a look at some of the parts you get in one of these little base kits robot car now i have them laid out on the table here now this is the base itself and as you can see the base has been drilled with a series of holes plus it has some cutouts here and i'm going to i'll show you what they're for in a few moments so we've obviously got some wheels these are pretty standard little wheels it's nice they actually have sort of rubber coated tires on them so they work well on both linoleum carpet be whatever happens you can even use them outdoors to some extent a battery holder now this uses a four cell battery holder although in reality I tend to replace them with five cell holders and that's because the controller of the motor I'm

using

, the H-bridge, it's going to drop one point four volts and I want to give the full 6 volts to my motors, so by having seven and a half volts instead of six, I can allow for that voltage drop, but it can use the four cell stand that comes with this if you wish here is a rear wheel used to balance the base because we've only got two wheels these two blocks are used as part of the motor mounts and e That's the difference between this and the other kit the other kit uses plexiglass for this and I'm going to show you how they both work obviously we have all the nuts and bolts we need these are the sensor discs I was talking about earlier these will be used for detect motor speed and i'll show you how we can hook up for those who talk about wires these guys were nice enough to give me some determined wires to my motor not all kits come with these but this one does and of course the ones The motors themselves are little six volt motors, but they actually have quite a bit of torque, so now that we've looked at all the parts, the next thing we want to do is plan how we're going to place our components on the base of our car now when it's ready.
arduino robot car with speed sensors   using arduino interrupts
When planning the layout of your robot car, there are a number of things to keep in mind after all, you only have a small area on the base in which to place your robots. speakers there are things that you obviously can't move like the motors, the wheels and the rear wheel, you also have to consider the balance, you can't fit all your components on one side of the robot because it could tip, but if you're creative, I can fit quite a few pieces on one of these robot faces. I have an example here to show you.
arduino robot car with speed sensors   using arduino interrupts

More Interesting Facts About,

arduino robot car with speed sensors using arduino interrupts...

This is an experimenter's robot that I've been working on here in the shop and I'm going to show you how to build it. something similar to this in a future video and as you can see it has quite a few components including components at the bottom and that's actually one of the keys to placing all the components on the robot. some parts on one of these robot designs and it has a pretty stable design that's not going to tip over so now that you've seen the extreme let's see what we can do with the robot base that we have now on our robot base in this At this time I have mounted one of the motors just to illustrate where it goes.
arduino robot car with speed sensors   using arduino interrupts
I intentionally left the brown paper on the base because I can use it to mark where I am going to drill additional mounting holes etc. our base we're going to end up with another motor here and a wheel and I'm going to put my motor controller on the bottom here as well I'm going to put it up here too I know there will be enough clearance with the wheels so the heat sink doesn't scrape the ground and the wires from the motor controller can go through this hole so I can run them to the

arduino

another thing I'm going to mount on the bottom is a little 9 volt battery and again I can run your wires through here now this will be used to power the Arduino.
arduino robot car with speed sensors   using arduino interrupts
It's not the most ideal way to power the Arduino, but again for our simple robot it will certainly suffice, so now let's flip it over and see what we can fit on this side of the robot. now I need my speed

sensors

, the wheels that will go here or what I'm going to be sensing and I have two different styles of these

sensors

here to show you now these just fit into these little openings here or you can put them the other way whichever I'll be happy to do it once I put th Although you'll want to mount them permanently, now I can't put a hole here because it's going to go under the engine, but I certainly can put one here, so I'll make a mark on my cardboard. and that way i know i need to drill a hole here now the other components i'm going to use this is the front of my robot this is its back on the back i'm going to put my battery holder in now like i said i'm using a five cell support and I'm going to place the support here so it's at the back and there will be some weight there.
I'm going to counterbalance that weight with the Arduino that I'm going to put on the front end here and I'm also going to SPRO a little solderless breadboard in the mix and the reason is it's just going to simplify my wiring so now I know roughly where all my parts are going to be I'm going to fine tune that and then drill holes in the base to mount these things to once I determine the final po. the position of my components i made marks on the paper and took everything to my drill press i mounted the acrylic base to a piece of wood to keep it from breaking while i was drilling it of course you could use a hand drill and it would work just fine, when all it was drilled, i removed the paper to leave a clear acrylic base, the next step was not to install the rear wheel using the four kit-provided mounting screws on the opposite side of the wheel, i installed my battery holder, then put spacers for the l2 98n and the Arduino after that I soldered the motors then I mounted the motors to the base on this particular base this requires 2 aluminum blocks which are provided with the kit the next step was to fit the encoder disks onto the shaft of the motor and place the sensors on the opposite side of the base once I lined everything up correctly I used a couple of drops of hot glue to hold the encoders in place I decided it would be It was a good idea to test the motors with my if x volt battery just to make sure everything was working and the encoder disks weren't stuck on the sensors, the next step was to mount the L 298 and H bridge driver to the part bottom of the base after that and mounted the Arduino on the opposite side, I placed the small soundless breadboard on top of the base next to the battery holder and finally installed the wheels and base of my car robot is complete so now that we put together our little card base I want to talk about this little disk that comes with every car now this is the key to detecting wheel rotation now you'll notice that the disk has a series of grooves and in this particular drive has 20 slots although the actual number doesn't really matter the drive is designed to be used with a component called an optoswitch and i have one here on the workbench an optoswitch is s implement an LED and a phototransistor mounted with a gap in the middle and it can detect when something happens in the gap and in operation the disk inserts itself into this particular gap and as it rotates it will break the light beam.
Now the way this works is that as the light beam is interrupted the phototransistor will detect and not detect the light and this will produce a series of pulses if the disk spins faster the pulses will come faster and we can use this to measure the rotation and speed of the disk, so I've got a little demo hooked up here on the workbench to show you how this all works, so let's take a look at that. Now for this demo I put the motor on the base and the encoder wheel on the motor. I placed the sensor in the proper position, powering the sensor with the five volts from my workbench power supply.
I'm using a six volt. battery to power the motor now to demonstrate this i am going to use a device called a logic probe if you are not familiar with logic probes they are very simple devices what they do is they can measure if a signal is at a digital zero a digital on e or if it's toggling between them, in other words if it's pulsing, for example if I touch the ground wire right now the green light comes on and I get a beep that I'm at a digital zero if I touch the five volts. lead i get my red light and a different sound indicating i'm on a digital one now if you touch the output right now it's sitting on a digital zero but let's connect the motor now now the motors by putting them on the output and as you can see on the yellow light blinking i am getting a pulse from the output this is the pulse that is being produced as the sensor disk rotates between the optical interrupt and when i remove it the output right now is high the output in the hover position just depends on where the wheel stopped.
Now the observers among you may have noticed that in my last experiment I not only used an optoswitch but I had a little circuit board that had an optoswitch and has another composition onent on it as well now here I have an opto isolator by itself along with two different boards that have an opto isolator and some other electronics the main component is something called a dual LM 393 comparator chip and for that reason these sensors are sometimes called LM 393 sensors, although that's a bit of a misnomer, now a comparator it's an analog chip it's actually another form of another analog chip called an operational amplifier or op-amp and it can be used for a number of things including interfacing analog signals to digital signals now unlike an analog to digital converter, a comparator only outputs one bit, the way a comparator works is as follows, a comparator has two inputs, the input signal is real l and a reference voltage, the reference voltage is compared to the input signal if the input signal is lower than the reference voltage, then the comparator will output a digital zero if the signal is higher than the reference voltage, will generate a digital zero no, this way a comparator can be used to clean up a messy analog signal and produce a nice pulse which is exactly what we want for our speed sensor.
Ok, now that we've seen how our sensor works, it's time to connect it to an Arduino and see how we can read those pulses to determine the speed of our wheel's rotation, but before we do this, there's an important computer concept that we need to discuss, ok, I apologize, that was pretty silly, but it actually illustrates a point of what happened to me. there was i was interrupted and it's time we talked about

interrupts

now interrupts are a very important programming concept yesyou don't know them well it's time to learn about them because they are used in quite a few programs not only for Arduino but for all types of computers in fact if you are using a desktop computer to watch this video and according to my analysis most of you are using interrupts all the time, you are using them when you move the mouse. you are using them when you type on your keyboard if you are viewing on a phone or a tablet you are also using interrupts every time you swipe your screen or press something on the screen you are causing an interrupt now this is how interrupts work now a standard

arduino

The sketch is runs as follows after setting up a number of items we go into a loop and inside the loop we do things and continue to do things until the Arduino shuts down or restarts now in case of an interrupt the program starts the same way. we set up some items and go into the loop and do things and continue to do things as the loop runs; however, if too much of an interrupt is generated, we branch off and run a special piece of code called an interrupt service routine.
Once we're done with that code, we loop back and continue doing things until another interrupt is generated. Arduino now supports hardware interrupts. Some processors support software interrupts, but the Arduino is not one of them. Arduino has two different types of interrupts. Also, internal interrupts are used with the arduino z' internal timers and external interrupts, which may be called pin change interrupts or external interrupts, are generated from external devices connected to the Arduino. Now, the external interrupts we're talking about are usually generated by a change. state, which means that when an element goes from 0 to 1 or from 1 to 0, the Arduino now has a pair of dedicated external interrupt pins.
Arduino Uno pins 2 and 3 are used as interrupt pins internally. Arduino interrupts are labeled interrupt 0, interrupt 1, etc. the interrupt service routine is special code that handles an interrupt now an interrupt service routine must be very short and run very quickly so that no additional interrupt is lost t here are also a number of functions that will not work on a interrupt service timer dependent routine functions this includes things like the tone function and the servo function ok now we have discussed interrupts let's put it into practice i have a little demo set up here on the workbench to show you this demo I have placed two of the motors in the base along with their encoder disks I have also placed the optical switch sensors in the base as well now i am powering the sensors with the 5 volt art you know and am using less solderless breadboards just to distribute power to the two sensors.
I have the outputs of the sensors connected to the two hardware interrupt inputs on the one, so motor one is connected to pin number 2 which interrupts ro and motor 2 is connected to pin number 3 which interrupts one. I am driving both motors directly with my 6 volt battery and I have two wires here so that I can independently turn the motors on or off so that this one goes through one of the motors off and this one goes on and off the other motor so that I can turn them on independently. standalone or both at the same time in this demo i am not making any attempt to control the speed of the motors so now let's take a look at the arduino sketch that will be run for this demo so here is the sketch that we will be using for our demo of two motors, our sketch requires a library called timer 1 the timer Library 1 allows you to work with the internal timers in the arduino and this is very important for our sketch because the timers themselves create interrupts that would otherwise interfere with us.
Timer 1 provides a very easy method of working with this and not interfering with the other. break now if you don't have the uno timer library installed or if you want to check if you just upload the sketch and go into the include library and then go to manage libraries this will open the arduino l library manager once the library manager library is loaded though they investigate by typing timer 1 and you will see the timer 1 library now, in my case it is installed, if you don't have it installed click on the more info link it will show a button over here that allows you to install the library now that we have the library installed let's continue with our code first we define a couple of constants that define where the motor sensors are connected so motor one is connected to pin 2 and motor 2 is connected to pin 3 now , these are interrupt 0 and interrupt 1 on the Arduino Uno I am using, if you are not using an Arduino Uno they may be on two different pins as you saw in the graph I showed earlier, so you'll need to change these numbers accordingly, next we'll define a pair of integers for the counters that we'll use to count pulse counter 1 encounter 2 and initialize them with a value of 0, next we have a float called disk slots and this is simply the number of slots in our encoder disk now my disk has 20 slots so i give it a value of 20 point 0 0 notice the use of the decimal point due to the float if you have a different number of slots on your disk just change this number as appropriate below the interrupt service routines as you may remember and the interrupt service routine is the code that is executed every time an interrupt is received and we actually have three because timer one also creates interrupts, the first interrupt service routine is for engine one and we're going to call this is our count one and the only thing in this routine is that we just increment the counter by one so every time an interrupt is received for I for engine one it will increment its counter by one, same deal for engine number two every time we get a pulse in the motor we will increment counter number two now we go to the interrupt service routine for timer 1 and the first thing we do with init is stop the timer and disconnect the interrupt now the reason why we are doing This is because we are going to use a number of serial print statements within this interrupt service routine, and in general, serial print statements are not a good idea in an interrupt service routine because they take too long. time and you might lose interrupts but by stopping the timer we avoid that problem and need the serial print so we can see what we are doing so So we'll just print out the speed of motor 1 and then calculate the value of its final float rotation called rotation 1 which is going to be equal to the number of counts we've had in one second / the number of slots on the drive now which will give us speed and rotations per second, but we want our p.m. so we're going to multiply the whole result by 60 again, notice the use of the decimal points because we're using floats, then we'll print that rotation value along with the word rpm after that and then reset the counter back to zero now for the motor number two we're going to do the exact same thing after that we're going to rename the whole timer by attaching the interrupt service routine to it so we're actually attaching the routine that was inside it right now but that's perfectly fine now let's look at the setup in our setup we are going to start the serial monitor at 9600 baud you can run it at a different speed if you want then we are going to initialize timer 1 now timer 1 can be initialized for a number of microseconds millionths of a second we are going to use 1 million microseconds which is a second now this is where we attack the in service routines interrupt to actual interrupts and we use the arduino attack interrupt statement and this statement here is really the heart of working with interrupts now the first part is what interrupts are we attacking now interrupt number zero is the first one that we are attacking and we could just put the value 0 here and that would work fine but it is considered appropriate programming practice instead of using the declaring digital pin to interrupt and then defining the actual digital pin you are using and the reason for doing this is so we can move this to others non-uno arduino models so again you could replace this Todo with a zero and it would work, but it is considered good programming practice to do it this way.
Next we say which of the interrupt service routines we're going to run when this interrupt occurs, so in this case it's engine 1. we're going to run the routine we saw earlier is our underscore account 1 and then when we activate the interrupt there are a number of cases and look we can do this this is increasing which means every time the line goes from zero to or not the interrupt will fire there are a number of other parameters we could have used but the rise is what I chose to use here, so whenever we get a rising pulse going from zero to one on this line, it will create an interrupt. and will execute this interrupt service routine.
We have an identical statement for the next interrupt for engine number two to execute is our underscore count, and then we also have to attach an interrupt to our timer and you'll notice this is the same. The same statement we used at the end of the interrupt service routine for the timer which just attacked since it's our timer routine one - the timer is interrupted after this we have the loop and for those of you who have done a lot Arduino programming you might be surprised by what you see on it there is absolutely nothing in our loop and I just put it there to illustrate that you could be running other code at the same time because interrupts are handling everything here so now that we've Seen the code, let's put it into action, ok, let's take a look at our code in action. to activate motor number one now and as you can see we are getting a reading from motor number one notice there is a slight delay and that is due to the 1 second timer we are using to count turn it off and now I will put motor number one 2 and once again I'm going to get a reading from motor number 2 let's launch both and get a reading from both motors so you can see our speed sensors are working perfectly so now that we've seen how our sensors are working it's almost time to Putting it all together at the base of our robot car, however, to take that information and do something practical with it, like calculate speed and distance, we need to know another parameter and that is simply the diameter of the wheel which is us.
Now If you were lucky, your base robot car may have come with a spec sheet that included the wheel diameter. I didn't have such luck with either of those kits though, but it's a simple matter to measure the diameter of the wheel, so let's do that now. I'll take my calipers and put them around the wheel and get almost 66 about 66 millimeters is the diameter of the wheel I'm using right now and knowing that I can use that to calculate distance in order to calculate distance I need the circumference of the wheel and the circumference of the wheel will indicate the distance the wheel will travel in one rotation the circumference is the diameter multiplied by PI which is about 3.14 once I know the circumference of the wheel I can also use that to calculate the speed because speed is distance divided by time and I can use centimeters per second or inches per second which are handy units for a small robot car like this so now we have everything we need to know how to get our car up and running robot, all that's left is to wire everything up and write some code, so before I show you the schematic, just q I want to show you some things I did when I hooked up my car just to help you and hooked up yours for one.
I made a lot of use of this ribbon cable. This material is very, very useful. It comes in male-to-male, male-to-female, and female-to-female varieties. These are the male to female varieties and they are great for connecting the sensors and also for connecting the L 298 and they just strip away as many conductors as they need and it's a quick way to make Kent patch cables. I also used just a regular 20 gauge breakout wire and I connected power from the Arduino to the little breadboard with no soldering and used a breadboard basically this to distribute the 5 volts from the Arduino to the sensors and also to the L 298 n now the L 298 n remembers that there is a jumper there that you have to remove to power it out of the Arduino instead of powering it off of its own motor power supply, that'sparticularly important if you're only using a 6 volt supply because you wouldn't have enough voltage to power the logic circuit in the L 298 and also in this particular type of L 2 98 and module, there was a couple of little jumpers that were connected to the line of enable you need to remove those as well so there were three jumpers to pull now as you can see I have a 9 volt battery mounted under here and held it down with a couple of tire applications now of course changing the battery will be a little harder because of that you may have to remove and replace the tire appliqués but it holds it pretty securely and i got past the tire appliqués not sure if you can see that through the sides of the sensors remember the holes I drilled there, so it's actually holding the optical switch sensors and the battery at the same time.
I also used tie wraps here on just the motor wiring to keep it from interfering with any of the moving parts, you don't want anything interfering with the wheels or sensor wheels, so by doing this I kept everything out of the way. I connected my 9 volt battery supply and ran the wires under the Arduino here and did it for the same reason so that when everything is connected it doesn't snag, it doesn't hit the wheel here; otherwise use the breakout wire again to run the connection from L to 98n to the breadboard without soldering, the jumper wires to run the connections from the sensors to the Arduino and you can test everything once you've done this even before your writing code now to plug it in and you can see I have some lights on on the Arduino you can also see that as I turn these wheels the lights are flashing on the sensor devices and that lets me know that they are connected correctly as well as that the l2 98n also has a power. on the it's connected light that shows when the battery is connected so that's a good indication that the wiring is correct so now that we've seen how I've arranged the wires here let's take a quick look at the schematic here the components which we will use for our robot, an Arduino Uno, an l2 988 bridge motor driver, two speed sensors along with the two motors and two batteries, we will start by connecting the five volts and ground from the Arduino Uno to the VCC and ground connections on th The two sensors after that we will take the five pins and ground from the Arduino Uno and connect them to the l2 ninety eight five volt input make sure to remove the jumper on the l2 ninety eight and allow be powered by an external power supply, after that, we'll take our battery, which could be 7.5 or 6 volt, and connect it to the l2 98n, then we'll connect the 9 volt battery to the Arduin o Uno, connect well the motor to the connections on the l2 98n and then we will connect the motor B to the l2 98n we will take the output a sensor a and connect it to pin 3 of the Arduino Uno, the output a sensor B will be connected to pin two of the uno and then we will connect the l2 98n to the Arduino Uno as follows you will connect the enable a of the l2 98 and to pin ten of the Arduino Uno note that your l2 98n might have a jumper here that input one of the l should be removed- 29 t8n connect to pin nine of input one two will connect to pin eight input three will connect to input 4 of pin seven will connect to pin 6 and finally the enable straight line from L to 98n will connect to pin five of the Arduino One again your l2 98n may have a jumper that needs to be removed and this completes the wiring so here's a sketch we're going to use with our robot car let's go over now it starts out similar to the last sketch we used we define two constants for the mo motor A and motor B and we map them to the pins that our sensors are connected to so that Motor A is connected to pin three on the Arduino Uno, which is interrupt one and that's the sensor for the right motor.
Motor B is connected to pin number two and that is the sensor for the left motor interrupts. After that, we define a constant called step. count its a float and you have a number of slots on our drive now i have 20 slots on my drive so i set it to 20 if your drive has a different number of slots you will need to change this number to match similar for the next variable is another float called wheel diameter Of course, what is the diameter of our wheel, this is in millimeters and minus sixty-six point one millimeters. If you have a different size wheel, just change this number to match, then we make two integers for the pulse counters like we did before. and we initialize it to a value of zero and counter B which is also initialized to zero are now both integers but have another statement in front of them called volatile now volatile is a statement we pass to the compiler in the arduino ide eye and the reason why The one we do this is if the compiler looks at this code it can see that the counter from a encounter B is not used for win in the code and in that case it could remove these variables to save some space in the code now usually this is a good thing but in the case of this particular code it would make the code not work so by giving it the volatile declaration we tell the compiler that these variables will be used, reserve space for them after that we will define our co connections to the motor controller so enable one line goes to pin number 10 input 1 to 9 and input 2 to number 8 and that's for motor number a for motor B we have enable go to pin 5 , input number 3 to pin 7 and input number 4 to pin 6 again if you decide to use a different processor or different connections on your one you can terminate these numbers, just remember that these to enable lines must be connected to a pin that is capable of pulse width modulation so we do the interrupt service routines which were identical to what we saw in our last sketches we have is our account A and is our account B and in each of these interrupt service routines we just increment the counter each time a pulse is received now I have a function that converts centimeters to steps you can give it the number of centimeters you want the car to travel and it will come back with a number of steps the sensor needs to count to get there like so that this function outputs an integer so it is defined as an integer its name is CM two steps and it takes one input and it is a float called cm or centimeters now the way the function works is as follows we define an integer called result this will be the final result that we go back to when the function is finished we define a float called circumference the circumference of the wheel as you remember is the diameter of the wheel multiplied by PI, then we divide that result by 10 because our wheel diameter is in millimeters and we want our results in centimeters and there are 10 millimeters per centimeter after that we define another float which is the number of centimeters per step so each step represents how many centimeters and that is a circumference divided by the number of steps we defined above in cases of 20 so we will get the result now f underscore result is the float result will calculate the result by dividing the amount of centimeters that we have requested for the value of CM per step that we have here a and this will return a value or float now we need to retrieve an integer so we use what's called a conversion statement so our result will be equal to an integer of results f now a note I've put here is this is not rounded Our owners cast statement doesn't do any rounding, so if, for example, you get a result of 5.7, the result will be 5, not 6, rounding is unfortunately something the Arduino doesn't do natively and although there are ways to do it I chose not to. do it in this code simply because it will complicate things further so now we have a result as an integer we just return the result which sends the result back and exits the function ok now we have a series of functions to move the car I am going to go through the first one which is forward the other three are almost identical only the direction of the motors will change so we have a function called forward we start with empty because it doesn't return any result forward take two inputs is the number of steps that we want to advance and the speed of the motor that is indicated by the variable speed M both are integers and the speed M can be equal to zero to 255 now in this function I have not taken any steps to make sure that we don't pass a value below zero or above 255 so you can improve this feature by adding code to do that, I just chose not to because it would complicate things for this demo so the first thing we do is set our counters to zero then we will put the motors in the direction we want and this is the only difference between the four functions i have here by the way in this case both motors are they're running ahead so we just write input one two high input two two low and put three too high put four low which will make both motors go forward and then we'll do a while loop that will run while the motor is running and I'll count the steps here while the steps are less than the counter. a and the steps are less than counter B, we will run this loop now, the reason I use both counter a and counter B is because you may have noticed in our demo above that the motors do not rotate at exactly the same speed , so it's possible especially in the long run for one motor to reach the distance while the other motor is still catching up just this make sure the while loop runs while both are trying to reach the number of steps, e.g. what in the while loop we say if the steps are less than counter a the value of the number of steps we have requested then we will do an analog right to enable a at the rate we have been asked to do and if the steps have exceeded that we will do we do a zero analog right which will stop the motor and we do the exact same thing for motor number B now once we're done we go and stop the motors because it's very possible may this else statement never be issued and if you don't do this the motors will keep running forever and ever Now, we do analog rights to both motors to stop them and then reset the counter.
Now I have a function to move in reverse that does the exact same thing, the only difference is that we put both motors in reverse. Another function to rotate everything correctly. again exactly the same except put motor a in reverse and motor B to go forward and that will make the car turn in the right direction and finally turn left which is the opposite we will put motor a in forward and motor B in reverse otherwise, these functions are all identical, the while loop is executed in exactly the same way. attach the interrupts to the interrupt service routines identically to how we did in the last sketch we use a digital pin to interrupt to define which interrupt we are getting again we could replace this with one and we could replace motor-b with a zeroy that would work too but this is the correct way to do it and whenever we have an interrupt on the motor a sensor will run the interrupt service routine called ISR underscore count a and an interrupt will be triggered on a rising pulse exactly the same for the motor B and this is identical to what we did in the last sketch and now we come to the part where we actually move the car.
I've put this as you'll notice in the setup routine and that means this is going to happen only once and it'll be over and you'll notice that in a game there's nothing in the loop now if I take all this code here and put it in the loop in Instead, the car would continue to do this forever and ever. but i only wanted to do all these sequences once and got stuck so here you can play around and define sequences randomly so i told the car to go half a meter which is 50 centimeters at a top speed of 255 then i put a delay , wait a second I told him to move back and this time I Instead of giving him centimeters I gave him the number of steps just to illustrate that he can do it either way so now go back 10 steps to 255 wait for a second forward 10 steps at a slower speed 150 wait a second now go back again and this time I'm doing C n 2 steps and give it a value of 10 inches now for those of you who aren't familiar with metric, 25.4 centimeters is about 1 foot so it's going to back up at a speed of 200 for about a foot and then it's going to wait a second and then I made it turn right 420 steps and retard a second left turn for 60 steps re after a second and then went one step forward and then stopped now the reason i did the last one for one step was Vista proved to myself that my logic was correct here when to all these functions and in my while loop I gave it a greater than and not agreater than an equals sign and that showed that it does run for a count if you gave it an equals sign it would actually run for an extra step and be inaccurate and now that we've seen the code we can load it into our car and see it in action and we finally got to the moment we've all been waiting for the sketch to load into the car and we're going to lay it down and test it now when you start this car of course it will run your sketch and you may not be ready to get it running, but since it's in the setup routine it will only run once so I'll let it run and then I can run it again anytime just by pressing the reset button on the Arduino so I'll turn this on and it will go through its routine and that last one little was that little step we had to do at the end so now that it's done we'll put it down on the floor and hit the reset button and we'll take a good look at it I'm hunched over here on the p iso with my car i'm going to put it on the floor and hit the reset button and let it reboot the amazing car seems to run fine so that ab out wraps it up it's been a very long video and if you watch it to the end i really do appreciate but we've learned quite a bit not only have we learned how to assemble a little robot car base we've learned how to use them little speed encoders and we've learned about interrupts with Arduino and that's a very important programming technique that you can use with several of your shows now this little car base has a lot of potential so i'm not going to take it apart instead i'm going to make some more videos and add some more features to this car.
One feature that I've been asked for a lot is a remote control feature that uses radio waves or Bluetooth, so we're going to add a remote control. on this car and I'm going to show you how that's done we're also going to use the ultrasonic sensor that we used in a previous video to make a collision avoidance system and I'm going to add line following capabilities to the robot car so if you've built one of these together with me please don't take it apart because there is more to come now the best way to know about these videos is to subscribe to the channel so if you haven't already then please do I would really appreciate it and you will also find all the code in a detailed article on what that we have done in this video in the communication of the drone workshop. website there is a link in the description below directly to the item so please check it out too until then take care and hope to see you again in the shop soon bye for now

If you have any copyright issue, please Contact