Arduino Tutorial 39: Using a Joystick to Control DC Motor Speed and Direction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy comm and we are here today with lesson number 39 in our epic in our legendary new and improved Arduino tutorials and what we are going to learn today is we are going to learn how to control a DC motor with a joystick where we control both speed and direction from the joystick if you remember that was your homework assignment from lesson number 38 hopefully you did it on your own but if you struggle with it you can follow along with me love to hear comments down below were you able to do this by yourself did you do it the way I did it did you do it a different way you know let's get a dialogue going about different ways of looking at this but I will kind of give you a simple and straightforward way to do it and then we can all do it together so I need you to pour yourself a nice huge mug of iced coffee that would be strong black coffee poured over ice no sugar no sweetener none needed this is delicious and refreshing and actually probably pretty good for you I need you to get out your a Lego super starter kit for the Arduino what you don't have one check the description down below there's a link you can pick it up on Amazon for $35 it does help a little if we're using identical hardware okay I should tell you that this lesson is a follow on really in lesson number 37 we showed you just how to hook let me move over here to what we are looking at and get out of your way in lesson number 37 we showed you how to hook the Arduino up to a power supply and up to a motor controller and then up to the motor and in this case we are controlling speed and direction just from the code so we can program the speed that we want we can program the direction that we want and by changing the program we can get the motor to do what we want if you have not done this yet you need to go back to lesson number 37 because that will give you the hook up diagram and it will the schematic and it will also give you the code that we are starting with you can do that on the most excellent top tech boy website and I think I can show it here top tech boy comm website let's see we want to okay this is it top tech boy comm and you want to search for Arduino tutorial 37 and that will have a video on how to do it it also has our schematic we always mention that one thing that's not showing here you want to run a ground wire from the Arduino ground over to this ground rail here so that all the grounds are hooked together that wire was not shown in that diagram but we explained it in the video and then here is the code that we are starting with you can click on that code and then you can copy it and then you can open up your arduino ide and you can paste that in and then you will be starting in the same place that we are starting i will get a better code view here let's see let's see what is a good code view this is a good code view okay now you can see my code and you can see the project and i think we are ready to go alright so now this is the assignment that we have we want to take this joystick and we want to connect it into the arduino when the joystick is in the neutral position just up and down we want the motor to be stopped as we move forward with the joystick we want the motor to begin to move in the forward direction in the further we press it forward the faster we want it to go as it comes back to the neutral position it will go slower and slower and slower until it stops then we want to do the opposite in the reverse direction as we pull backwards we want it to begin to go slowly in the reverse direction faster faster faster until when it's all the way back then we're going full-speed reverse and so the joystick is controlling both speed and direction of the project now the key thing is that you can imagine there's math and there's no way that you can do this really very well without doing the math and so we're going to have to go through the math but in order to do the math there's a number that's very important and that is what does the potentiometer read when it is in this neutral up position now we know it one extreme it's going to be zero and at the other extreme is going to be 10 23 and we would expect it to be around 512 in this up direction but we need to verify that because the math is going to depend on that and you need to do the math based on your joystick not mine so repeat what I'm doing here and then just go through the math for what yours is so the first thing that we will want to do is hook this up we'll come back over to this view and so let's get this out of the way I'm going to hook the joystick up I don't need all of them because I'm just going to use the Y value and then also have your little ribbon cable from the from the Arduino kit and the outside wire on mine is black and so I'm going to hook that black to GND on the joystick you see that GMD we're going to put black to GND and then we're gonna hook the next wire which is white to 5 volts and then I'm going to use the purple wire and I'm going to connect the purple wire to the D our why because we're only going to be using the Y value on the joystick so they're right so purple to vry black to ground and white to 5 volts now I need to hook that up the other side up to the Arduino well the black was ground and so I need to come over to GND on the Arduino I have an available ground there and then I need to put the white to 5 volts on the Arduino as such I keep hitting the fan with my finger okay so let's see where we are black wire I'm going to turn this over to do it a little better still it will still be the same but just get them going the same direction okay so the white wire will go to five volts you got to kind of fight with these wires they're stiff a little bit so white will go to five volts and black will go to GND on the arduino now I'll try to show you white to 5 volts black to GND and now remember the purple wire is that Y value and that purple wire I'm going to bring to a 1 the analog read pin one so now I have the joystick plugged in so now we need to see what okay now we need to see what it reads in the neutral position I'm sure that you guys are out there screaming for me to turn the motor off but I want to leave it all plugged in ok so now let's see if we can go back to the code view and let's see if we can get this thing coded alright so what did we do we added a joystick so we connected it in so what are we going to need we're gonna need an int and we're gonna need a J pin where did we hook the joystick up to we could hook it up to pin capsule a1 to analog pin 1 alright the analog pin 1 was the purple wire that goes to the Y on the joystick alright if we're going to have a j pin we're gonna need to read a j-val so int j-val we don't put about you in there to begin with because we will read that from the joystick then when we come down we are doing a pin mode for the speed pin which makes sense kid mode for those two pins that control the motor direction Durr one in der two hooked up hooked up in the lesson number 37 using this you can see that the der one and Dirk hit to the two Direction pins were three and four and so that will continue to work in this again less than 37 explained all that to you and so when we look at our pin modes from our earlier work we're setting the speed pin to an output and then Derwin and or two to output what we need to have is now pin mode and we need to have Jake and and that needs to be an input like that let me make sure that's what we did yeah that looks pretty good and then down here in this code remember in lesson 27 we were seeing if we could kind of kick-start it with a little bit higher value we're gonna take that kickstart out because that was just playing around and we're gonna take this delay out and so we have our basic motor control commands here that if dyrone is low and dirt 2 is high it runs forward and then if we switch them it runs backwards and then we have to do an analogue right to our speed pin to tell the motor how fast you run so these are our core commands to run the motor but now what we really want to do right now is we want to find out what that neutral position is on the joystick so we're going to say j-val is equal to analog read of j-pin and then we're gonna need to print that serial dot print L in and we're gonna print j-val so we need to see what that value is if we're gonna be able to do our math and so let's download this okay looking good let's call up our serial monitor let's see if I have a view that will show the serial monitor hopefully okay not getting it there alright ok you can see it here and what you can see is is that it is reading 512 and so that looks good that looks encouraging that we are reading 512 off of that and so what that is saying is is that when the joystick is in this neutral center position it's at 512 and it'll go from there to zero and it will go from there to 1023 and so based on that we need to do all of our math ok so let me move this out of the way here and we need to go in and we need to get our math bunch okay so in doing the math this is really what you guys need to learn you need to learn how to take the words and the concepts and you need to draw a picture and then from that picture you need to be able to figure out how to from that picture you need to be able to figure out how you are going to how you are going to write your equations and so let's just think of a set of accesses these things usually start with a graph and so let me draw a graph and the idea is you want to take a number that you read and you want to turn it into a number that you write and you do that mapping in such a way that the project does what you want it to do or what you were assigned for it to do and so what are we going to be reading we are going to be reading j-val right j-val and that's going to go between 0 and where 10 23 okay so this is 10 23 and this is 0 also there was that kind of magic number in the middle what was that magic number in the middle that magic number in the middle the neutral position was here and this was at 5:12 this was an x-value of 512 well what do we know if the but if the joystick is at that neutral position of 512 what do we want that to read we want it to read 0 so what we have here is we have a point that is the point J Val is 512 and then our motor speed is going to be 0 so we're going to label this in speed so when the joystick is in the middle at 512 j-val is 512 we want an M speed of 0 now if you pull that all the way back and you're reading 10 23 now on J foul what do you want for M Smith M speed you want full speed and that is going to be what what is full speed that is 255 so what is this point if I read 10 23 I want to write a M speed of 255 now this is in this direction where we're pulling back we're pulling back and we're going from 512 to 1023 now if we push forward we are going to go from 5 12 to 0 well at 0 what do I want to do I want to go to 55 also because that's going to be full speed now what's going to be different this is going to be backwards so we won't have to deal with that okay so if J val is less than 512 i want to do this and i want to do it in the negative direction if j val is greater than 512 i want to do this and i want to do it in the positive direction so you can see right off the bat that your code it's going to have an if statement right your code is going to have an if statement and it's going to look something like this we're going to say if j val is less than 5 we're going to do these things and then if j-val is greater than or equal to because we need it defined for the 512 somewhere I could have put the equal here or the equal here but we want to make sure every case is taken care of 512 I'm going to do these things well what do I need to do I need to set the direction I need to set the direction well in this case what were our two values our two values that control direction right there was dur 1 and there was dur - well we want to make dur one go high and we want to make dur to go low and then if we want to go in the opposite direction we would set dur one low and we would set derp too high okay if you do not understand this go back and watch lesson number 37 because we explained all that there so that is taking care of the direction now we need to find the speed okay and that is finding the equation of this line well I've got the point 512 zero I've got the point 10 23 255 I can come up with an equation for this line I also have another line 512 and then this would be the point J Val is zero and I want to write 255 okay so now I've got to come up with the equation I've got to come up with two equations I got to come up with two equations for a line the one on the left and the one on the right let's start with the one on the left what do I know M is equal to y2 minus y1 over x2 minus x1 okay now if I call if I call I can call this point two in this one or I can call this two in this one it doesn't matter you just need to do it consistently as you do the slope and so let's say the this is 255 minus zero over zero minus 512 and so that would be Y 2 is 255 minus y1 which is 0 over 0 -5 0 -5 12 and then that is going to be equal to minus 250 5 over 5 12 and so that's the slope of the first line okay let's find the slope of the second line okay and so this is going to be M 2 is going to be 0 minus 250 5 over 5 12 minus 10 23 so we call this y2 minus y1 over x2 minus x1 and then what do we get well I'm going to round 5 12 minus 10 23 is five eleven but I'm going to round it to 5 12 and that way I just get it's just off by a fraction of a percent but I get the same slope in the positive direction so this would be minus 255 and I'll put this is approximately equal to 512 - 10 23 will say minus 512 and therefore M 2 is going to be equal to 255 over 512 okay does this make intuitive sense that these two lines have the same slope but just opposite sign okay now we need to do the equation for the line what do we know we know Y minus y1 is equal to in onto X minus x1 don't forget the parentheses all right so what I'm going to do is I'm going to call this my x1y1 know what yeah x1 y1 so this is going to be Y minus 255 is equal to M what is M minus 250 5 over 5 12 minus 255 over 5 12 times parentheses X minus x1 is 0 do you see how I kind of try to pick points you can pick either point in doing these things but you pick the one that makes it a little bit easier so now I have y minus 250 v is equal to minus 250 5 over 5 12 times X now I need to move the 255 over and I'm going to have Y is equal to minus 250 5 over 5 12 times X and now I move that over plus 255 I add 255 to both sides ok so what this means is I have an equation and for any j-val between 512 and 0 I can calculate the M speed because I have the equation of this line in fact let me write it up here that is y is equal to minus 250 5 over 5 12 times X plus 255 so now I've solved half of the problem now I've got to do the same thing for the other side I have the slope and so now I'm going to say Y minus y1 well I'm going to use this point 5 12 and 0 so Y minus 0 is equal to Y minus 0 is equal to M in this case 255 over five 12 times X minus x1 and what is x1 it's five twelve okay so now what do I get Y is equal to 255 over five twelve times X and then minus 250 five over five 12 times 512 is just 255 okay do you see these equations are almost the same because they're really just reflecting across each you know reflecting across that Center dotted line so now I can write this equation up here in this region I would use the equation y is equal to 255 over 512 times X minus 255 okay so now I have the math done on both sides of the equation all right I had the math done for both moving the joystick forward which is this one and then moving the joystick back which is this one so now we need to come over and we need to see if we can start coding this thing up all right so let me get this as best I can these wires begin to be very hard to manage and I'm going to need to get a little better view let's see you need to see a good coding window here all right the good thing is we already did a little bit of the coding we did a little bit of the coding already trying to get this where you can see everything so we are going to look here so we've we've got the speed pin which is the pin that controls the speed of the motor again lesson 37 we've got our dur pins that control the direction and then we have M speed which is going to be this thing that we're calculating now we just for the demonstration set it to 90 but now we will be using these equations to calculate M speeds so I'm not going to set that at 90 we will calculate what that should be so let's think of what we want to do well we still want to do these same on these same pin modes and then we still want to do our analog read and then really to control the motor there's three lines you've got to set derp one endure to low high or high low in that sets the direction and then you've got to write the Imps right to speed pin the the motor speed but it's going to depend on where we are so if j-val is less than what if it's less than five twelve what do we do well we're going to set the direction and so I will come here and I will just copy these again this was all lesson 37 that shows you that sets the direction in the forward direction okay and now what do I need to do and again we are for less than 512 we are here so we use this equation so now I'm going to calculate in speed based on that equation that we just did and that was equal to minus 255 point don't forget your point in there it won't work 255 divided by 512 point and then times what well times J Val because we're taking j-val and we're turning get into em speed and then what we do is we add 255 point and that was just this equation that we just derived on the left now what do we do with that well we now need to analog right to where speed pin that's where the speed pin is connected to and then we write the M speed that we just calculated and don't forget your semicolons up here all right so that's the case for when j-val is less than 512 the other case would be if j-val is greater than 512 so if we say if j eval and you never want to say greater than 512 in less than 512 you got to tell it what to do at 512 so either the first or the second if needs to have an equal so we'll just put it on the second one so if J val is greater than or equal to 512 then what do we do what we do the other equation ok we do the equation on the right that we found but what we also need to do is we need to first set the direction and this needs another closed here ok we need to set the direction so in the other way it was low high so to change the direction we need to make this high and we need to make this low and then still we're gonna write analog right speed pin but now we got to calculate we have to calculate that M speed and in this case M speed and writing it right M speed is going to be equal to and we said it's 255 point divided by 512 point remember on that side it was a positive slope and then times X well what is our x value our x value is j-val okay and then we subtract 255 point all right this looks pretty good and so let's put this in and let's see what happens I need everyone to hold their breath okay it downloaded but now the question is what is going to happen when we try to run this with the joystick let me go back to the full speed I mean let me go back to full screen here okay so you see this we had it set up like this and so what I need is I need as I gradually move this forward I need it to start running slowly at first and then faster and faster as I move the joystick forward so I'm beginning to move it forward move it forward and look at that it's starting and it's running real real real slow because I'm barely moving it and it's barely running and now full-speed okay and then look stop and this is going in for me it's the counterclockwise direction okay now if I pull it back look it's going clockwise slowly and then full-speed boom do you see this this is working I am controlling this with a joystick forward backwards and I can control the speed in the forward Direction very smoothly do you see how slow I can make that go how slow or slowly in the reverse direction boom okay guys you cannot do this without doing the math the only way that you can make projects like this work is doing the math and so what I really want you to learn is I want you to learn how to think about a project what was it that I told you I told you the joystick in the neutral position would be off as you move it forward it would go faster asked her backwards faster and faster in the opposite direction what was the key to making this project work okay the key was drawing this picture that makes a graph out of the words that we talked about when you make the graph out of the words that we talked about then you change it from a hard thinking problem into a simple math problem it also shows you why you need to be able to do the equations of a line okay guys I would really appreciate hearing from you guys I would really appreciate getting getting your feedback and let me letting me know were you able to do this project on your own did you do it on your own but do it in a different way if you got hung up how did you get hung up once you saw this lesson once you saw this lesson lesson number 39 does it at least make sense how we did this when you come across problems like this in the future are you going to be able to do them okay I really really hope so okay this is going to be your assignment for next week okay for next week what I want you to do is I want you to have a motor controller so you're going to have this this same kind of basic circuit you're going to have the same basic circuit that we had in this lesson in lesson number 37 but instead of using the joystick I'm going to want two buttons okay I'm going to want two buttons and it's going to start in the off position but then if you press one button it will start going faster faster faster faster in one direction okay until it reaches full speed and then when you press the other button it is going to slow down until it stops and then it's going to reverse direction until it goes full speed in the reverse direction so you're going to go from full forward to full backwards one button should move you towards full forward the other BIRT button should move you towards full back and so instead of using a joystick you're going to control this motor with two buttons okay that's your assignment for next week and then we will take a look get that you guys if you like this lesson give me a thumbs up I love to hear your comments down below be sure to subscribe to the channel if you subscribe make sure you ring the bell if you don't touch the Bell you will not get notifications for this channel and then we will get back together next week and go over this this latest homework assignment palma quarter from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 33,571
Rating: 4.9874411 out of 5
Keywords: Joystick, DC Motor Control, L293D, Arduino
Id: wSjZIHMDczk
Channel Id: undefined
Length: 33min 41sec (2021 seconds)
Published: Tue Dec 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.