Arduino Robot Car with Speed Sensors - Using Arduino Interrupts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we're going to be assembling a little robot car base and we'll learn how to use those speed encoder discs that come with these kits will also learn about using interrupts with the Arduino so there's plenty to do today so let's get started welcome to the workshop [Music] hello and welcome to the workshop today we're going to be working with a small robot car base now if you've been into robotics for any period of time I'm sure you've seen these things advertised on eBay and Amazon they're very inexpensive little bases that are available in both two-wheel and four-wheel configuration and they're really great to get started with robotics with now I've got two of these bases over here to show you first of all this one over here this kit cost me about 15 dollars and it comes with everything the wheels the base itself a battery holder the motor is a caster all the different parts and this point you'd be through in a little screwdriver which I thought was pretty cute another one that I got to freedom less money I picked this up with one of my local electronics stores for all of $12.98 this one here has all the parts the other one had it even has a mount for a servo motor and an ax sensor so that it's got the the pan and tilt mechanism as included with this as well - so amazing values for the money a one-part that's included with both of them that nobody seems to tell you what to do with however is this and this is a little sensor that you place on the wheel in order to measure the rotation of the wheel you can measure the speed and by doing that you can measure distance the amount the wheel is turned and get a lot of useful information but as I said there aren't very many instructions for using 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've got two of them here is that there's two different styles of motor mounts so I want to show you how the motor goes on to that and then we're going to add a few extra components onto that so that we can experiment with this speed sensor and I can show you how it actually works now the components you're going to add are as follows we're going to add an Arduino Uno and that's going to be the microcontroller that's the brains of the project's an l2 98n motor driver and we've seen this it's a dual h-bridge controller and it's going to of course drive the 2 DC motors and this device here which is a optical source sensor which is meant to be used with the little wheel that I showed you in order to measure rotation so there's a lot to do today so let's get ready and build ourselves a little robot car so let's take a look at some of the parts that you get in one of these little robot car base kits now I've got them laid out on the table over here now this is the base itself and as you can see the base has been drilled with a number of holes plus it has some cutouts over here and I'll show you what those are for in a few moments then we've obviously got some wheels these are pretty standard little wheels it's cute they actually have sort of rubberized tires on them so they work well on both carpet linoleum whatever you happen to drive over you can even use them outdoors to some degree a battery holder now this uses a four cell battery holder although I actually tend to replace these with five cell holders and that's because the motor driver I'm using the the h-bridge is 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 you can use the four cell holder that comes with this if you wish here's a rear caster that is used to balance the the base because we've only got two wheels these two blocks are used as part of the motor mounts and that's the difference between this and the other kit the other kit uses Plexiglas for this and I'll show you how both of them work we've obviously got all the nuts and bolts that we need these are the sensor disks that I was talking about earlier these will be used to sense the motor speed and I'll show you how we can wire for those speaking of wires these guys were nice enough to give me some wires decided to my motor not all the kits come with these but this one does and of course the motors themselves these are little six volt motors but actually have quite a bit of torque so now that we've seen all of the parts the next thing we want to do is plan out how we're going to lay our components onto our car base now when you're planning out the design of your robot car there's a number of things you need to take into consideration after all you've only got a small area on the base in which to place your components there are things that obviously you can't move such as the motors and the wheels and the rear caster you also need to consider balance you can't place all of your components on one side of the robot because it might tilt over but if you get creative you can place quite a few parts onto one of these robot faces I've got an example over here to show you now this is a experimenters robot that I've been working on here in the workshop and I'll be showing you how to build something similar to this in a future video and as you can see it's got quite a few components including components on the underneath of it and that's actually one of the keys in getting all of the components onto the robot by doing that you can place quite a few parts onto one of these robot designs and have a fairly stable design that isn't going to tilt over so now that you've seen the extreme let's to see what we can do with the robot base that we have right now now in our robot base I've right now mounted one of the motors just to illustrate where it goes I've left the brown paper on the base intentionally because I can use that to mark off where I'm going to drill any additional mounting holes so on our base we're going to end up with another motor over here and a caster and I'm going to place my motor driver on the bottom over here as well I'm going to place it up here I know that there's going to be enough clearance with the wheels in order for the heatsink not to scrape on the ground and the wires from the motor driver can go through this hole so I can pass them through to the arduino another thing i'm going to mount on the bottom is a small 9-volt battery and again I can pass its wires through here now this will be used to power the Arduino it's not the most ideal way of powering the Arduino but again for our simple robot it'll certainly suffice so now we're going to flip it over and see what we can place on this side of the robot now I need my speed sensors the wheels which will go over here or what I'm going to be sensing and I've got two different styles of these sensors here to show you now these just snap in into these little openings over here or you can place them going the other direction whatever makes you happy once you do place these in though you're going to want to actually mount them permanently now I can't put a hole over here because it's going to go underneath the motor but I can certainly put one over here so I'll make a mark on my cardboard and that way I know that I need to drill a hole over here now the other components I'm going to use this is the front of my robot this is their rear on the rear I'm going to place my battery holder now as I said I'm using a five cell holder and I'm going to place the holder over here so that it's at the back and there'll be some weight over there I'm going to counter that weight with the Arduino which I'm going to place at the front over here and I'm also going to SPRO a small little solderless breadboard onto the mix and the reason is it's just going to simplify my wiring so now that I know roughly where all of my parts are going to be I'm going to fine tune that and then I'm gonna drill holes in the base in order to mount these things once I determined the final position of my components I make marks on the paper and took everything over to my drill press I mounted the acrylic base onto a piece of wood to avoid cracking it while I was drilling it of course you could use a handheld drill and it would work just fine when everything was drilled I removed the paper to leave a clear acrylic base the next step wasn't to install the rear caster using the four mounting screws provided by the kit on the opposite side of the caster I installed my battery holder next I put spacers in for the l2 98n and the Arduino after that I soldered up the motors I then mounted the motors on the base on this particular base this requires 2 aluminum block which are provided with the kit the next step was to put the encoder discs onto the motor shaft and to put the sensors onto the opposite side of the base once I aligned everything correctly I used a couple of dabs of hot glue to fasten the encoders in place I decided it would be a good idea to test the motors with my six volt battery just to make sure everything was working and that the encoder disks weren't binding on the sensors the next step was to mount the L 298 and H bridge controller on the bottom of the base after that and mounted the Arduino on the opposing side I placed the small sonless breadboard on the top of the base beside the battery holder and finally I installed the wheels and my robot car base is complete so now that we put together our little card base I want to discuss this small disc that comes with every car now this is the key to sensing the wheel rotation now you'll notice the disc has a series of slots and at this particular disc has 20 slots although the actual number doesn't really matter the disc is meant to be used with a component called an opto interrupter and I've got one over here in the workbench an opto interrupter is simply an LED and a photo transistor mounted with a gap in between and so it can sense when something is passed in the gap and in operation the disc is inserted in this particular gap and as it rotates it will interrupt the light beam now the way that this works is that as the light beam is interrupted the photo transistor will sense and not sense the light and this will produce a series of pulses if the disc is rotated faster the pulses will come faster and we can use this to measure disc rotation and speed so I've got a little demonstration hooked up here on the workbench to show you how this all works so let's go take a look at that right now now for this demonstration I've placed the motor on the base and the encoder wheel on the motor I've placed the sensor in the proper position empowering the sensor with the five volts from my workbench power supply I'm using a six volt battery to power the motor now in order to demonstrate this I'm going to use a device called a logic probe if you're not familiar with logic probes they're very simple devices what they do is they can measure whether a signal is at a digital zero a digital one or whether it's alternating between it in other words whether it's pulsing so for example if I touch the ground lead right now the green light comes on and I get a sound that indicates that I'm at a digital zero if I touch the five volt lead I get my red light and a different sound that indicates that I'm at a digital one now if it touch the output right now it's sitting at a digital zero but let's connect the motor now now the motors fitting them into the output and as you can see from the flashing yellow light I'm getting a pulse from the output this is the pulse it's being produced as the sensor disc spins in between the optical interruptive and when I remove it the output right now is high the output in the stationary position simply depends on where the wheel came to rest now the observant ones among you will have noticed that in my last experiment I didn't just use an opto interrupter instead I had a small circuit board that had an opto interrupter on it and it has another component on it as well now here I've got an opto isolator by itself along with two different boards that have an opto isolator and some other electronic components the primary component is something called an LM 393 dual comparator chip and for that reason these sensors are sometimes referred to as LM 393 sensors although that's a bit of a misnomer now a comparator is 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 actual input signal 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 goes higher than the reference voltage it'll output a digital one in this fashion 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 okay now that we've seen how our sensor works it's time to hook it up to an Arduino and see how we can read those pulses to determine the rotating rate of our wheel 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 does illustrate a point what happened to me there was I was interrupted and it's time for us to talk about interrupts now interrupts are a very important programming concept if you don't know about them well it's time to learn about them because they're used in quite a few programs not just for the Arduino but for all types of computers in fact if you're using a desktop computer to watch this video and according to my analytics most of you do you're using interrupts all the time you're using them when you move your mouse you're using them when you type on your keyboard if you're watching on a phone or a tablet you're also using interrupts every time you swipe your screen or press something on the screen you're causing an interrupt now here is how interrupts work now a standard Arduino sketch runs as follows after setting up a number of items we go into a loop and inside the loop we do stuff and we continue to do stuff until the Arduino is powered down or reset now in a case of an interrupt the program starts the same way we set up some items and we go into the loop and we do stuff and continue to do stuff as the loop runs however if an interrupt is too generated we branch off and run a special piece of code called an interrupt service routine once we're done with that code we go back into the loop and continue to do stuff until another interrupt is generated now the Arduino supports Hardware interrupts some processors support software interrupts but they arduino isn't one of them Arduino has two different types of interrupts as well the internal interrupts are used with the arduino z' internal timers and external interrupts which can be called pin change interrupts or external interrupts are generated from external devices connected to the arduino now the external interrupts we are talking about are generally generated by a change of state which means when an item goes from a 0 to a 1 or a 1 to a 0 now Arduino have a couple of dedicated external interrupt pins now as to which pins are dedicated to interrupts depends upon which our dueño model you were using on the Arduino Uno pins 2 & 3 are used as interrupt pins internally Arduino interrupts are labeled as interrupts 0 interrupts 1 etc etc on the Arduino Uno pin 2 is mapped to interrupts and pin 3 is mapped to interrupt 1 now as we saw in the earlier diagram the interrupt service routine is special code that handles an interrupt now an interrupt service routine needs to be very short and run very quickly so that you don't miss any additional interrupts there are also a number of functions that will not work in an interrupt service routines functions dependent upon the timer this includes things like the tone function and the servo function alright now we've discussed interrupts let's actually put it into practice I've got a little demo set up here on the workbench to show you for this demonstration I've placed two of the motors on the base along with their encoder disks I've also placed the optical interrupter sensors onto the base as well now I'm powering the sensors with the 5 volts from the art you know and I'm using less solderless breadboards simply to distribute the power to the two sensors I've got the outputs of the sensors connected to the two hardware interrupt inputs on the uno so motor one is connected to pin number 2 which is interrupts ro and motor 2 is connected to pin number 3 which is interrupt one I'm driving both of the motors directly with my 6 volt battery and I've got two leads over here so I can independently turn the motors on or off so this one goes through one of the motors off and this one will turn the other motor on and off so I can put them on independently or both at the same time in this demonstration I'm 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 running for this demonstration so here's the sketch we're going to be using for our two motor demonstration our sketch requires a library called timer 1 the timer 1 library allows you to work with the internal timers in the arduino and this is very important for our sketch because the timer's themselves create interrupts which would otherwise interfere with us timer 1 provides a very easy method of working with this and not interfering with the other interrupts now if you don't have the timer one library installed or if you want to check to see if you do just go up the sketch and go into include library and then go to manage libraries this will bring up the arduino library manager once the library manager is loaded thoughthey research by typing in 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 the more info link which will bring up 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 the interrupts 0 and interrupt 1 on the Arduino Uno that I'm using if you're not using an Arduino Uno these may be on two different pins as you saw in the chart I displayed earlier so you will have to change these numbers accordingly next we'll define a couple of integers for the counters we'll be using to count the pulses counter 1 encounter 2 and we initialize them with a value of 0 next we have a float called disc slots and this is simply the number of slots in our encoder disk now my disk has 20 slots in it so I give it a value of 20 point 0 0 notice the use of the decimal point because of the float if you have a different number of slots on your disk just change this number accordingly next the interrupt service routines as you recall and interrupt service routine is the code that is run whenever an interrupt is received and we actually have three of them because timer one also creates interrupts as well the first interrupt service routine is for motor one and we're going to call this is our count one and the only thing in this routine is we simply increment the counter by one so every time an interrupt is received for I for motor one will increment its counter by one the same deal for motor number two every time we get a pulse in the motor 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 we stop the timer and detach the interrupt now the reason we're doing this is because we're going to use a number of serial print statements within this interrupt service routine and generally serial print statements aren't a good idea in an interrupt service routine because they take far too long and you could miss interrupts but by stopping the timer we avoid that problem and we need the serial print so we can actually see what we are doing so we're going to simply print motor speed 1 and then we're going to calculate the value of its rotation of the final float called rotation 1 which is going to be equal to the number of counts that we have had within a second / the number of slots on the disk now that will give us the 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're going to print that rotation value along with the word rpm after that and then we are going to reset the counter back to zero now for motor number two we are going to do precisely the same thing after that we will rename all the timer by attaching the interrupt service routine to it so we're actually attaching the routine that were within right now but that's perfectly valid now let's look at the setup in our setup we're going to start the serial monitor at 9600 baud you can run it at a different speed if you wish then we're going to initialize timer 1 now timer 1 can be initialized for a number of microseconds millionths of a second we're going to use 1 million microseconds which is one second now here is where we attack the interrupt service routines to the actual interrupts and we use the arduino attack interrupt statement and this statement over here is really the heart of working with interrupts now the first part of it is which interrupts are we attacking now interrupts number zero is the first one we're attacking and we could just put the value 0 here and that would work fine but it is considered proper programming practice instead to use the statement digital pin to interrupt and then define the actual digital pin you're using and the reason for doing this is we can then move this to other models of the arduino other than the uno so again you could replace this whole thing with a zero and it would work but it is considered proper programming practice to do it this way next we say which one of the interrupt service routines we're going to run when this interrupt happens so in this case it's motor 1 we're going to run the routine we saw earlier is our underscore count 1 and then when do we trigger the interrupt there are a number of cases and look we can do it this is rising this means that whenever the line goes from a zero to a one the interrupt will be triggered there are a number of other parameters we could have used but rising is what I chose to use over here so every time that we get a rising pulse that goes from zero to one on this line it will create an interrupt and it will run this interrupt service routine we have an identical statement for the next interrupt for motor number two which will run is our underscore count - and then we also have to attach an interrupt to our timer and you'll notice this is the very same statement we used at the end of the interrupt service routine for the timer which simply attacked as the is our timer one's routine - the timer interrupts after this we have the loop and for those of you who have done a lot of Arduino programming you might be surprised by what you see in it there's absolutely nothing in our loop and I just put that there to illustrate that you could be running other code at the same time because the interrupts are handling everything over here so now that we've seen the code let's actually put it in action alright let's take a look at our code in action I've opened my serial monitor and both motors are currently reading 0 rpm which makes perfect sense since neither motor is turning I'm going to activate motor number one now and as you can see we're getting a reading from motor number one notice there is a slight time lag and that's because of the 1 second timer that we're using for counting turn that off and now we'll put motor number 2 on and once again I'm getting a reading from motor number 2 let's throw them both and we're getting a reading from both motors so as you can see our speed sensors are working perfectly so now that we've seen how our sensors work it's almost time to put everything together onto our robot car base however in order to take that information and do something practical with it such as calculating speed and distance we need to know one other parameter and that is simply the diameter of the wheel that we are using now if you were fortunate your robot car base may have come with a spec sheet that included the diameter of the wheel I however was not that fortunate with either those kits but it's a simple matter to measure wheel diameter so let's do that right now I'll just take up my calipers and place them around the wheel and I get almost sixty six roughly sixty six millimeters is the diameter of the wheel that I'm using right now and so knowing that I can use that to calculate distance in order to calculate distance I need the circumference of the wheel and the wheel circumference will indicate the distance the wheel will travel in one rotation the circumference is the diameter multiplied by PI which is roughly 3.14 once I know the circumference of the wheel I can also use that to calculate speed because speed is distance divided by time and I can use centimeters per second or inches per second which are practical units for a small robot car like this so now we have everything that we need to know to get our robot car up and running all that remains really is to wire everything up and to write some code so before I show you the schematic I just want to show you a few things I did when I wired up my car just to help you out and wiring yours up for one thing I made a lot of use of this ribbon cable this stuff is very very useful it comes in male to male male to female and female to female varieties these are the male to female variety and they're great for hooking up the sensors and also for hooking up the L 298 and you just peel off the number of conductors that you need and it's a quick way of making Kent connection cables I also made use of just a regular 20 gauge hookup wire and I've hooked up the power from the Arduino to the little solderless breadboard and I've used a breadboard basically this to distribute the the 5 volts from the Arduino to the sensors and also to the L 298 n now the L 298 n remember there's a jumper on there that you need to remove in order to power it from the Arduino instead of powering it off of its own motor power supply that's particularly important if you're only using a 6 volt supply because it wouldn't have enough voltage to power the logic circuitry on the L 298 and also on this particular type of L 2 98 and module there were a couple of little jumpers that were connected to the enable line you have to remove those ones as well so there were three jumpers to pull now as you can see I've got a 9-volt battery mounted underneath here and I held it on with a couple of tyre apps now of course changing the battery is going to be a little more difficult because of that I might have to remove and replace the tire apps but it holds it pretty securely and I passed the tire apps not sure if you can see that through the sides of the sensors you remember the holes I drilled in there so it's actually holding down the optical interrupter sensors and the battery at the same time I also used tie wraps over here on the motor wiring just to keep it from interfering with any of the moving parts you don't want anything to interfere at the wheels or the sensor wheels and so by doing this I kept everything out of the way I wired my 9-volt battery supply and passed the wires under the Arduino here and I did that for the same reason so that when everything is connected it's not going to hook it's not going to hit the wheel over here otherwise use hookup wire again to run the connection from the L to 98n to the solderless breadboard the jumper cables to run the the connections from the sensors up to the Arduino and you can test everything out once you've done this even before your write code now to plug it in and you can see I've got some lights going on on the Arduino you can also see as I spin these wheels I get the lights flickering on the sensor devices and that pretty well lets me know that those are hooked up correctly plus the l2 98n also has a power in the light that's hooked up that displays when the battery is hooked up so that's a pretty good indication that the wiring is correct so now that we've seen how I've laid out the wires here let's take a quick look at the schematic here the components we'll be using for our robot an Arduino Uno an l2 ninety eight and eight bridge motor driver two speed sensors along with the two motors and two batteries we'll start by connecting the five volts and ground from the Arduino Uno to the VCC and ground connections on the two sensors after that we'll take the five bolts and ground from the Arduino Uno and connect them to the l2 ninety eight and five volt input be sure to remove the jumper on the l2 ninety eight and to allow it to be powered by an external power source after that we'll take our battery which could be seven point five or six volts and connect it to the l2 98n then we'll connect the 9-volt battery to the Arduino Uno well wire motor a to the connections on the l2 98n and then we'll wire motor B to the l2 98n will take the output a sensor a and connect it to pin 3 of the Arduino Uno the output a sensor B will then be connected to pin two of the uno will then connect the l2 98n to the Arduino Uno as follows will connect the enable a from the l2 ninety eight and to pin ten of the Arduino Uno note that your l2 98n might have a jumper here which needs to be removed input one from the l-29 t8n will connect to pin nine of the uno input two will connect the pin eight input three will connect the pin seven input 4 will connect to pin 6 and finally the enable beeline from the L to 98n will connect the pin five of the Arduino Uno 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 be using with our robot car let's go over it now it starts off similar to the last sketch we use we define two constants for motor a and motor B and we assign them to the pins that our sensors are connected to so 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 interrupts ro the left motor after that we define a constant called step count it's a float and it has a number of slots in our disk now I've got 20 slots in my disk so I define this as 20 if your disk has a different number of slots you'll need to change this number to match similar for the next variable it's another float called wheel diameter which of course is the diameter of our wheel this is in millimeters and minus sixty six point one millimeters again if you have a different size wheel just change this number to match then we do two integers for the pulse counters as we did before counter a and we initialize it to a value of zero and counter B which is also initialized to zero now these are both integers but they have one other statement in front of them called volatile now volatile is a statement that we passed the compiler in the eye arduino ide and the reason we do this is that if the compiler looks at this code it may see counter a encounter B are not used to gain in the code and at that case it might eliminate 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 cause the code not to work so by giving it the statement volatile we tell the compiler these variables will be used please reserve space for them after that we'll define our connections to the motor driver so the enable a 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've got enable be going to pin 5 input number 3 to pin 7 and an input number 4 to pin 6 again if you decide to use a different processor or different connections on your uno you can finish these numbers just remember that these to enable lines have to be connected to a pin that's capable of pulse width modulation then we do the interrupt service routines which were identical to the ones that we saw in our last sketches we've got is our count a and is our account B and in each one of these interrupt service routines we simply increment the counter every time that a pulse is received now I've got a function that converts centimeters to steps you can give it the number of centimeters you want the car to travel and it'll come back with a number of steps that the sensor needs to count to get there so this function outputs an integer so it's defined as an integer it's name is CM two steps and it takes one input and it's 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're going to pass back when the function is finished then we define a float called circumference the circumference of the wheel as you'll recall is the wheel diameter multiplied by PI we then 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 every step represents how many centimeters and that's a circumference divided by the step count which we defined earlier in cases to being 20 then we'll get the result now f underscore result is the floating result will calculate the result by dividing the number of centimeters we've asked for by the CM per step value we've got over here and this will return a float now we need to get an integer back so we used what's called a cast statement so our result is going to equal an integer of f results now one note I've put over here is that this is not rounded our dueños cast statement does not do any rounding so if for example you get a result of 5.7 the result is going to be 5 not 6 rounding unfortunately is something that the Arduino does not do natively and although there are ways of doing it I chose not to do it in this code simply because it will complicate things further so now we've got a result as an integer we just return the result which sends the result back and exits the function okay now we have a number of functions to move the car I'm going to go through the first one which is move forward the other three are almost identical only the direction of the motors is going to change so we have a function called move forward we start off with void because it doesn't give us back any result move forward takes two inputs the number of steps we want to move forward and the speed of the motor which is indicated by the variable M speed they're both integers and M speed can be equal to zero to 255 now in this function I haven't taken any steps to make sure that we don't pass a value below zero or above 255 so you could make this function improved by adding code to do that I just chose not to do it because it would just complicate things for this demonstration so the first thing we do is we set our counters down to zero then we'll set the motors in the direction we want and this is the only difference between the four functions that I have here by the way in this case both of the motors are being set forward so we just write the input one two high input two two low and put three too high in put four low which will set both motors going forward then after that we are going to do a while loop which will run while the motor is running and it'll count the steps so here while the steps are less than 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 earlier demonstration the motors don't turn at exactly the same speed so it is possible especially on the long run for one motor to achieve the distance while the other motor is still catching up just this make sure the while loop runs while both of them are trying to catch up with the number of steps so in the while loop we say if the steps are less than counter a the value of the number of steps we've asked for then we'll do an analogue right to enable a at the speed we've been asked to do and if the steps have exceeded that we'll do an analogue right of zero which will stop the motor and we do the exact same thing for motor number B now once we're finished we go and we stop the motors because it's quite possible that this else statement will never be issued and if we don't do this the motors will continue to run forever and ever so again we do analog rights to both motors to stop them and then we reset the counter now I have a function to move in Reverse which does exactly the same thing and the only difference is we set the two motors into reverse another function to spin everything right again the exact same thing except that set motor a in Reverse and motor B to go forward and that will cause the car to spin in the right direction and then finally spin left which is the opposite we'll set motor a forward and motor B reverse otherwise these functions are all identical the while loop runs exactly in the same way now we've done undefined' all of our functions we're going to go into the setup routine and we're going to run everything in the setup routine now we attach the interrupts to there interrupt service routines identically to the way we did in the last sketch we use digital pin to interrupt to define which interrupt we're getting again we could replace this with a one and we could replace motor-b with a zero and that would work as well but this is the proper way of doing it and every time we get an interrupt on the motor a sensor it's going to run the interrupt service routine called ISR underscore count a and an interrupt will be triggered on a rising pulse the exact same thing for 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 now I've put this as you'll notice in the setup routine and that means this is going to happen just once and finish and you'll notice a game there is nothing in the loop now if I take in all this code here and placed it in the loop instead the car would continue to do this forever and ever and ever but I just wanted to do all these sequences once and just stuck so here you can play around and randomly define sequences so I've told the car to move forward at half a meter which is 50 centimeters at a top speed of 255 then I put a delay wait one second I told it to move reverse and this time instead of giving it centimeters I've given it the number of steps just to illustrate that you can do it either way so now reverse 10 steps at 255 wait a second move forward 10 steps at a slower speed 150 wait a second now reverse again and this time I'm doing C n 2 steps and I'm giving it a value of 25.4 centimeters now those of you who aren't familiar with the metric system 25.4 centimeters is approximately 1 foot and so it's going to reverse at 200 speed for about a foot and then it's going to wait a second and then I got it to spin right 420 steps and delay a second spin left for 60 steps delay a second and then move forward by one step and then stop now the reason I did the last one for one step was Vista proved to myself that my logic was correct over here when I to all of these functions and on my while loop I gave it a greater than and not a greater than an equal sign and that proved out that the does indeed run for one count if I given it an equal sign it would actually run for one extra step and be inaccurate and so now that we've seen the code we can just upload that to our car and watch it in action and so we finally come to the moment we've all been waiting for the sketch is uploaded onto the car and we're going to put it down on the floor and test it out now when I power up this car of course it's going to run its sketch and I might not be ready to actually have it run but since it's in the setup routine it's only going to run once so I'm going to let it run through and then I can run it again any time just by hitting the reset button on the Arduino so I'll put the power on this and it's going through its routine and that last little one was that little one step that we had to do at the end so now that it's ready we'll put it down on the floor and hit the reset button and watch it go okay I'm hunched down here on the floor with my car I'm going to put it on the floor and hit the reset button and let it go reset [Music] awesome car seems to work okay so that about wraps it up it's been a very long video and if you watch it to the end I really appreciate it but we have learned quite a bit not only have we learned how to assemble a little robot car base we've learned how to use those little speed encoders and we've learned about interrupts with the Arduino and that's a very important programming technique that you can use with a number of your programs 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 onto this car one feature that I've been asked for a lot is a remote control feature using either radio waves or Bluetooth and so we're going to add a remote onto this car and I'll show you how that is done we'll also use the ultrasonic sensor that we used in a previous video in order to make a collision avoidance system and I'm going to add line following capabilities to the robot car as well so if you've built one of these along with me please don't take it apart because there's more to come now the best way to find out about these videos is to subscribe to the channel so if you haven't done that already please do that I'd really appreciate it and also you will find all of the code in a detailed article about what we've done in this video on the drone bot workshop comm website there's a link in the description below right to the article so please check that out as well until then take care of yourself and I hope to see you soon again in the workshop bye for now [Music] [Applause] [Music]
Info
Channel: DroneBot Workshop
Views: 370,524
Rating: undefined out of 5
Keywords: robot car chassis, car chassis kit, arduino smart robot car, interrupts, tutorial, LM393, Speed Sensor, Sensor Disk, arduino, robot, chassis, L298N, Robotics, arduino robot, beginners, DC Motor speed control
Id: oQQpAACa3ac
Channel Id: undefined
Length: 49min 12sec (2952 seconds)
Published: Fri Dec 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.