Arduino Tutorial 33: Understanding How to Control Servos with a Joystick

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul McWhorter from top tech boy comm and we are here today with lesson number 33 in the legendary the new the improved series of Arduino tutorials this is going to be a good one this is going to be a more complicated project so I need you to pour yourself a doubly strong EE tall cup of iced cold coffee and I need you to get out your Arduino a Lego super starter kit if you don't have one check out the description below for a link 35 bucks you get a whole lot of components and then I need you to get out your high tech servos which hopefully you ordered because I warned you to last time if you don't have the high tech servos you can go to the link below and the description and you can get those so what are we going to do today let me get out of your way okay I'll get a little further out of your way today because we got a lot of stuff going on what we're going to do is we are going to control two servos from the joystick so what you need for this project is you need your breadboard you need the ribbon cable from the e Lego kit you need your Arduino you need the joystick you need the button for the joystick the buzzer and you need your two high tech servos again the servos that come with these little kits allow you to learn on them but they're not real good ones they're kind of jittery and don't work very well hey I'm going to give you another tip on a servo when you have a servo never sit and turn it like a toy it is not a fidget toy if you sit and spin it from the horn or spin it from the little output shaft you will damage it so don't sit and fiddle with them they are precision instruments so make sure that you take care of your equipment all right we are going to jump right in and get started on this thing so the goal is is that with the joystick if I move it left and right it will control this servo and if move it up and down it will control this servo and then we are going to mount this servo on top of this servo as such so that we can switch this way or we can switch this way and then we would have a super laser pointer at that at that point I will simulate my laser with this blue ruler but it's gonna be kind of hard for you to actually see the dot where a real laser would be pointing so you remember in lesson 32 we showed you how to hook the joystick up so let's get started with that and if hopefully you already left your joystick hooked up and you could skip a little bit of this video but for those of you tuning in from the start we get our ribbon cable and we're going to put it this way and we're going to put the black wire to ground we are going to put the white wire to the 5 volts we are going to put the gray wire to the V R X which is that X potentiometer we are going to put the purple wire to V R Y which is the Y potentiometer on the joystick and then the blue is going to go to SW the switch alright so that is hooked up now we're going to hook this to the Arduino and we have a little problem ok the little problem that we have is that we've got a lot of other stuff to hook up and so what we need to do in this particular case is I'm trying to think of the best way to arrange this perhaps like this we have a lot of grounds that we're going to need and we have a lot of 5 volts that we're going to need and so I'm going to create a rail I'm going to connect to 5 volts on the Arduino yes good 5 volts on the Arduino and I'm going to come over and I'm going to connect it to the second row of the breadboard now that entire row tube becomes a 5 volt rail that we can go grab things off of when we need and similarly if I can find a suitably short black wire like this one we're going to come from ground and we are going to create a ground rail in Row 1 okay so Row 1 is hooked to ground and Row 2 on the breadboard is so to the most excellent I lost my train of thought the Row 2 is Row 2 is 5 volts Row 1 is the ground alright now we have to hook this up and this is going to start being a little bit more of a challenge because we got to get all the wires where we want them so I am going to come in and the black one the first one was ground so I will put that in yeah you know what I think though what I am going to do I'm just thinking about how to get all these wires connected I this will work ok so so the black wire goes to Row 1 which is ground if I can get it to go in right black wire into Row 1 then white wire into Row 2 which is the 5 volts and now I have to go over to a 0 which was a little bit what I was afraid of but I can get that so the gray wire is going to be that potentiometer on the X which goes into a 0 and then the next wire which is purple is going to go into a 1 and so let's look at this to make sure that you can follow okay do you see how we have the gray in a 0 and the purple in a 1 so that's all hooked up and now I just have one more thing to hook up on the joystick and that is the blue wire going to pin 2 okay the blue wire going to pin 2 and then these wires are not going to be used so I think I'm kind of hooked up pretty good there but guys this is so such a kind of you know intense project what I'm going to do is I'm going to go on over here to the code and this is the code that we developed better view better view here this is the code that we developed in Lesson number 32 which I believe was a previous lesson yes lesson number 32 and if you go to W can go to the YouTube channel and go to the previous lesson which was less than number 32 and you can see how we developed this code to control the joystick or you can go to top tech boy comm you can go to our new Arduino lessons the new series you can go to 32 and you can copy and paste this code really it's not good to copy and paste it's really not a good idea to copy and paste but at least you can get caught up with where I am since I'm not developing this let me go ahead and explain it remember X pin we connected to remember X pin we connected to a 0 Y pin we connected to a 1 the s pin the switch pin we connected to 2 that's just what we just did down here right just did it down here then X val is what we're reading from that x potentiometer y value is what we read from the Y potentiometer s Val is the value of the switch when we do the digital read on the switch if this is going too fast for you I'm sorry go back to the earlier lesson and I explained it in detail we set up our pin modes for X pen Y pin and s pen and then in order to be able to do this digital read with a built-in power supply and a pull-up resistor again go back to the earlier lesson if you don't understand this digital right s pin high even though it's an input we make it high then we read the X valve for X pin read the Y valve from yn read the S valve from s pin and then we delay by DT so it doesn't go too fast and then we print it all out so let's download this ok and since this was earlier developed code it works now let's come over and let's get our joystick where you can see it let's put the handy little knob on it it's kind of a square hole so you've got to get it just right okay so you can see this I'm gonna go left I expect X to go down okay let me make sure I have my serial monitor open or is that crazy serial monitor okay there it is all right now it's like so as I move the joystick left look at that X goes down all the way to zero back to the up position I'm going to move the joystick right and look at that it goes to 1023 I'm going to move the joystick up and why this time goes to zero Y comes back to the neutral and then Y goes to 1023 so this is working as expected if I move it left it goes to zero right it goes to 1023 on X and then Y changes when I move it this way if I press it forward it goes to zero as such if I pull it back it goes to 1023 and it is all smooth in between all smooth and between then the button push if I hit the button it goes to zero if I let the button out it goes to one press the knob down zero one this is working properly okay so I guess next up we need to connect these darn servos okay so we've got the joystick part of the project working let's hook up these servos these are a little different color code than the ones that came with the than the ones that came with the e Lego kit but you can imagine here it's kind of like you might imagine the red pin goes to power so I will go red to my rail which is row two right I would run out of power things if I didn't make this real so red goes to Row 2 which is my power rail and then I am going to take the black the black wire plug in plug in black these things are darn hard to get in today the black is going to go to Row 1 which is my ground ok Row 1 goes to ground I mean the black goes to Row 1 Row one is ground and now the yellow is my control the yellow is my control so I need to move this bad boy over here and I'm going to make it pin 10 just because I can't I don't know for sure but it might be smart to use a squiggly for a servo I don't know that for sure but it might be you need to use a squiggly for a servo on the digital pin okay let's do similarly with this one this time the red is power I will use a white wire wouldn't it be neat if I had all the right colors that I could color code but I am running out of wires here because we are looking at a lot of stuff so on the second servo red has a white wire and is going similarly to Row 2 on the breadboard which is power now I will take I don't I do have a black wire I love using the right color wire so black goes to black on the servo black wire goes in and then that goes to Row 1 which is ground and now I have a control I will use a green wire to connect to the yellow for control and then that is going to go to pin 9 on the Arduino and none on the is this not a horrible mess very quickly you will see why people use PC boards rather than bread boards because you can see that these wires are going every which way crazy alright but with a little luck that might work let me think okay you know what we got to do now we got to do a little math before we do any coding because we got to get the servos working correctly with the potentiometers on the joystick and if you tuned in to maybe lesson 12 I went kind of went through this somewhere back there on no it was it was an earlier I mean it was a later lesson where we were talking about the servos and the servos were probably somewhere around lesson 30 okay we talked about how how to do this but what you can see is is that what are the values that we are going to have that we read from the potentiometer we can read from 0 to 1023 we can read from 0 to 1023 that's like your x value because that's what you read and you want to turn that into a Y value and so this is like your read value that's what you read from the potentiometer and then what you want here is write value what you're going to write to the servo so you read from the potentiometer on the joystick and then you're going to write to the servo so if you have a zero what angle do you want the servo to be yet if you read zero from the potentiometer you want the servo to be at zero if you read 10:23 from the potentiometer you want the servo to be at 180 at 180 so now we have two points so we can make a run we get the equation of that line and when we get the equation of the line then we can program it so that as we smooth Lee move the joystick the servos smoothly moves in I know you guys hate math but if you would just learn how to do that somewhere out there in the world we need some people that know how to do math and so what is this this is the x value which we call what read value and it goes from zero to so let's say this is 250 500 this is 750 and this is a thousand and then 10:23 would just be a little bit past that and then this is going to go from 0 to 180 all right so what's one point that we have we have the point 0 0 what's the other point we have we have the point 10 23 180 so 10 23 up here to 180 and now we have a line we have a line like that I need an equation for that line to get the equation for the line what's the first thing I need I need slope M in math class is what it's y2 minus y1 over x2 minus x1 but we're not doing y's and x's we have what we have right value to write value 2 - write value 1 over what X is read value read value 2 - read value 1 all right now what does that become well write value 2 is what 180 and then - right value 1 which is 0 so I have 180 minus 0 over the read value 2 - 3 value 1 read value 2 - read value 1 is 10 23 - 0 isn't life wonderful when you go through the origin and you have a nice easy slow like that ok now how do we find the equation of the line well with the point-slope form of the equation we have I mean yeah the point slope y2 minus y is equal to M ding-ding we have M onto with parentheses X minus x1 but we're not using Y so we're going to rewrite this with our variables this is going to be right value to a right value Y this is why I wrote this wrong y minus y1 I'm sorry okay this is y minus y1 is M onto X minus x1 so this becomes right value - right value 1 is equal to M on to read value - read value 1 all right so this is right value where you can see it right value - right value 1 well what was right value 1 it was 0 that's kind of nice is equal to what M do I have M yes it's 180 over 10 23 which we just calculated times what read value - read value 1 so that's read value - what's read value 1 0 wife is so easy when your line goes through the origin so now we have an equation right value is equal to 180 divided by 10 23 times read value and this is the equation that we need to program into the Arduino so we're going to look at the potentiometer and we're going to read a read value we're going to multiply it by 180 divided by 10 23 and then we get the right value and so let's just think if I read 10 23 that would be 10 23 divided by 10 23 is 1 times 180 I would write a 180 and boom the servo would move just where I want it now understand I'm going to need to do this for the X servo and I'm going to need to do it for the X potentiometer and then the Y servo and the Y potentiometer so I'll have to program this equation twice what's slightly different variables but I think with that most excellent little math lesson you can figure it out and this is getting to be a scary tangled mess it will be amazing if this thing actually works all right I'm gonna take another shot at coffee okay the good news is we have the potentiometer this is just too ugly that I cannot even operate this way all right good news is we have the potentiometer already working I mean the joystick already working the two potentiometers on the joystick already working so now we begin to code we have added oh one more thing one more thing circuit wise if it wasn't complicated enough if it wasn't complicated enough we are going to add this a buzzer this is the active buzzer and the active buzzer is going to plug into the breadboard I'm going to take one pin of the active buzzer the positive one on the buzzer make sure that you notice that there's a plus by one of the pins and that does matter and I'm going to take that two pin 7 and then I need my most trusted I'm desperately looking for a black wire but don't have one so I will use a blue wire and I will take that from the other lead of the buzzer and I will hook that to Row 1 which will be my ground all right and I cannot help it I'm going to come over here and plug the buzzer in and just make sure it works a little auditory excitement here ah there it goes okay very good I can make it buzz but now I go back to pin 7 all right just a little excitement there shake things up a little bit throw the buzzer on with just positive voltage I just want to make sure that I did in fact have the right buzzer the buzzer with the black tape on the bottom in the e Lego kit is an active buzzer which means you just put a voltage on it and it works all right we have added some things here and so we have got to do some more pin modes up here and add some more pins so we now have an int and I will call it X servo pin and what was my X servo pin that is going to be that one and so it's this servo here in that servo is indeed plugged into pin nine okay is it pin night it is pin night man on this part this is where you've got to really be careful it's pin nine okay that's pin nine and now I also have the Y servo pin and that we did in fact that's this one we did in fact put that on in 10 and those are both squigglies just in case that matters 9 and 10 very good very good all right and then also we're gonna have an int and we are gonna have a buzz pin and buzz pin was equal to 7 plus pen was 7 I believe those are the new pins that we did so with new pins comes oh man I'm sorry I don't want to start this video over so let me just switch over here now and let me just show you what I did real quick here you can see that I had these pins already from our earlier work and then I put in or I had these pins from the earlier work and I put in this X servo pin equal 9y servo pin equal 10 buzz pin is equal to 7 and then we will need to do pin modes but before we do pin modes I need to think do we need any other variables yes we do need a variable we need right we need to calculate an angle which we called the right value bright value for servo X and we're going to calculate that and then I need an int and I need a right value for servo Y all right that gets back to this calculation that we did right this calculation where I need to calculate the right voltage using this well the thing is I'm going to have a right I'm going to have a right value for the X servo and a right value for the Y servo so I need two variables there now the buzzer I do indeed think does not need anything and we already have X Val that we are reading off of the X potentiometer and we have Y Val that we are calculate we are calculating from our we read Y Val from the Y potentiometer which is y pen and then we calculate write value why we read X Val from X pin and then we calculate write value X does that make sense hopefully you're seeing kind of what I'm thinking alright we need to do some pin modes we need pin mode and what did we add we added we added X s pen 4x servo pin XS 4x servo pin is an output we're riding to it in mode and then it was y servo pen where the Y server was hooked to we're going to control that that is going to be an output and then we have pin mode and then I do believe we called it pen buzz yeah and that is going to be an output I think that's good alright so now we come down here and we read expel right we read expel well I don't want X Val what is it that I really want I want the right value right I want the right value in which right value do I want I want right value for X is that like what I call it right value for x yes and that's equal to because of our most excellent equation it's equal to 180 divided by 10 23 it is 180 divided by 10 23 and because you want to force this to be floating point math make sure that you put your periods in one of the biggest errors I see is if you don't put your periods in it is going to make that integer math and will round to zero I'm gonna put parentheses just because I'm a math teacher and always like parentheses and now I'm going to times it by what the read value the read value well what is the read value well it's X valve that we just read okay so now I need to do the same thing with Y Val and so I really want the right value for y this is the angle that we are going to send to the servo and that begin is going to be equal to 180 point divided by 10 23 point and I will use my parentheses alright and now this time it is times what times y Val and so this is the angle this is the angle that we are going to send to the Y servo so now as I move the joystick as I move the joystick x val is going to change and that's going to calculate a right value in X and then as I changed the potentiometer this way that is going to change Y Val and then that is gonna allow me to calculate a right value for the y servo okay I hope you understand this is very logical but I hope you're following with what I am saying so now I have and then I still read this s Val now I have I in fact have everything that I need in order to would send values to those servos all right but if I'm going to use those servos I am going to have to turn the servos on and do all the nonsense associated with the servos which we have to remember we have to remember this that ah what we want to do and let me just get a little cheat sheet here I have a I don't remember all the commands all the time right off the top of my head and so I am going to see if I can get this right here okay this will have the commands that I need just so that I don't mess you up all right so if we're gonna use the servo we have to load the servo library do you remember how we do that we hit pound and we say include saying to load a library which library do we want capital s er vo dot H that is the library now we have a library alright so we can give servo commands the other thing that we have to do is we have to create an object right we have to create an object and so we're going to use the servo command from the servo library and then we're going to create the these the object and so what am I going to call it I'm going to call my object X servo okay X servo what do I also need do I have one servo no I have two servos so the other one is going to be what why servo right so now I have two servo objects all right now Arduino in my though I don't want those I'm sorry I put those in the wrong place so get that nonsense cut it all right that goes at the very very top of the code I thought I was at the top but I wasn't so I'll control V paste it so this goes so now I have X 0 and I have Y servo now I want to go down to the void setup and what I need to do there is I need to tell Arduino where those two servos are attached and I do that in the void setup and what do I have I have X servo and X servo is connected to pin 10 which is my I'm sorry X servo X servo attached and where do I attach it to X pin and let me make sure that that is the right pin XS pin that is the servo pin so I'm attaching the X or servo to the X servo pin as such what else do I need to do I need to connect Y servo that I just created dot attached and where did I attach it why servo pin like that alright now I've loaded the library I've created the two servo objects I've told Arduino where to attach them and now I do believe that I am ready to write these values that I created in what values that I create I created I'm going to do an X servo dot right and what am I going to write 2x servo well I'm going to write 2 servo the right value X which I just calculated so that's the number between 0 and 180 now I need to why servo dot right I need the Y servo right Y or right value Y and I need to end with semi-colons so let's think about what we are doing here in case you have gotten lost I have a joystick okay I have a joystick the joystick has two potentiometers what I read from those two potentiometers first I read X Val and then I read Y Val X Val is moving it like this y val is moving like that I have X Val and Y Val now I need a number between 0 and 180 to write to the servos I calculate that the right value of x is based on x val and that is the value that is going to be written to the X servo similarly I calculate the Y right value right value Y and I write that to theme-wise servo this will be a miracle if we get this working okay we're gonna take a shot of coffee before trying to download this help me by holding your breath and then oh why does it not like Y Val ah simple you forgot to put the semicolon there so put the semicolon oh oh you forgot to put the semicolon there alright and some of you are not holding your breath everyone hold your breath aside and it downloaded okay so we downloaded it alright man and this time we're going to go back up to full view here like this and I am going to get my joystick where you can see it with the servos now it would be a miracle if this actually works but I am going to and this is hard to get a pretty view but man we're beyond we're way beyond trying to be pretty at this point we are way beyond trying to be pretty at this point so we're gonna watch this one and let me get a little piece of kooky if you don't have Pookie please get some it is the duct tape of the electronics world and so I'm going to make a little Pookie arrow here like that so you can make sure that you see that now when I go left and right I expect that to move oh man look at that that one oh did you see that oh did you see that watch that watch I can move it nice and ever so gently look at that you see that is because we do Matt and because we do math we can make this thing work beautifully and perfectly do you see how smoothly we can move it that's because the math really works now look at this other one what do we expect if I move it up and down we expect the other servo to move look at that to the left and to the right getting position now I can move them by going at the 45 sigh oh we had a collision battle of the servos boom look at that we can go that way and they'll go the same direction this way they'll go the same going the other way we can make them go opposite man do you see this do you see this yes we have this thing working all right now what do we do though we wanted to make a pointer we wanted to make a laser pointer kind of thing and so I'm gonna get a little more Pookie be turned by this Pookie it is amazing stuff I don't even know what it's really called but it's something you use in ham radios it's like you can if have outdoor connectors you can put this stuff on it and it will like seal your connectors against the apocalypse if you if you need that so this we're gonna put there and then this we're gonna put on top of it like that like such alright now here we're gonna get this nonsense out of the way okay and then what am I gonna get another little piece of cookie okay another little piece of cookie and I'm going to stick my trusty blue ruler you know I had a student a math student and she had this beautiful ruler and I was always admiring her ruler and I told her you know that is just a lovely ruler and you know what she did she went and got me one even though it is a freebie that somebody gave her I thought it was very nice that she actually went and got me a pretty six inch of clear blue ruler okay so now this is our Raygun our laser of death okay and let's see let's see if we can actually man this is ugly isn't it let's see if I can go to a different view let's see if I can have a different view boom do you see that it is aiming right at you and then here's the joystick and I'm gonna go left and right and look at that you see I'm pointing it very carefully left and right look at that you know what I see I see right off the bat that I need to put another piece of paper under here and you see how the base is not the base is not stable and you see that answer is answer is more pookie so I'm going to try to secure the bottom servo to the table or at least a piece of paper on the table with more cookie and I hope that works okay and when that doesn't work see the problem is more tape okay and now if I just put the Pookie here maybe that'll hold okay now here is the joystick we go left our laser of death aims left if we go right our laser of death aims right and don't pull on the wires there okay do you see that if I move it smoothly it moves smoothly now let's look we can go down we can go up we can knock it over if we're not careful okay boom guys this really moves incredibly smoothly you can just see that I need to do a little of my fusion 360 work design some of the mechanical connectors and then print it out on the 3d printer and you could have just like a super cool little laser game where you were pointing a laser using this and if this was my it's electrically extremely accurate the position is very very accurate but mechanically you can see that this is just kind of glued together so it's not like a real a real strong a real strong setup but we forgot one more thing that we needed to do and that was oh yes we forgot our friend the buzzer I almost forgot so I need to come over here and then as I am doing all of this nonsense I need to come in and then say if and then what was that silly buzzer pin that silly buzzer pin was buzz pin know the value that I read did I ever read from that let me see I don't think I've set up my buzz pen yeah I put buzz pin-high digitalwrite s pen I need to digital right buzz pin-high here no no no okay switch pin right right right guys okay this is it buzz pin goes to the buzzer all right yeah you're not even seeing I'm sitting here talking like an idiot I apologize when I do that but I don't want to go back and start all the way over okay buzz pin is what we're going to write to that's where the buzzer is hooked to it's s pin that we read from okay and then we read it here s Val is digital read of s pin now I know if the if the pin is is if the switch is down or not remember hear like watch on this over here watch as I'm going on these values that I'm printing x value I value in switch State remember that if I pull if I press this button switch state goes to zero and that is this s Val here that I am reading so then what I need to say is I need to say if s Val equal equal what if s Val equal equals zero then what I want to do is I want to did JIT whole digit all right buzz in hi like that alright and then I think what I would do is instead of an if I would do a while I would say while no I'm gonna do it I'm gonna say if lift if s Dow equal equals zero and what do I do I'm going to say digital right buzz in hi okay and then else if it's not that then I will do what I will did did chit pull right buzz in low so as long as I'm holding that pin that switched down it should buzz honest I think let's see if we can even download this alright it looks like we downloaded it okay and it looks like I'm getting values and so now let's see I'm going to switch back to our dramatic view here where it looks like this crazy thing is about to fall over more mechanical support call in reinforcements where is my Pookie all right you see that poor thing so we are going to put something here like a remote control and then we're gonna put something here like a calculator and try to hold that okay now I'm going to try to point it right at you using my controls point it right at you and I'm gonna point it right at you I'm gonna fire you see that fire boom I got you Oh someone up hi incoming boom Oh down low mmm left right boom man it's a mechanical disaster but it is a electrical programming Arduino success okay so you guys see this is really it was kind of fun to do but this was really a very a very complicated project right it really was very complicated I have got to get a reasonable view here okay this was a really complicated project because we had an input device from a joystick and then we controlled two separate devices an X servo in a Y servo and then we even used the button on the servo to control the buzzer and so I think that that was pretty darn exciting okay I have over 100,000 subscribers and I would be surprised if I have had a single viewer make it all the way to the end of this video so to show me that you have made it to the end of the video I want you to type in the secret word the secret word for today wait for it the secret word for today is Bosco okay this is Bosco he is my guard in Africa don't let the trim physique and the overcoat fool you he is a very very good guard you do not mess with him and it looks kind of funny it looks like tropical and this guy's wearing a coat well the funny thing in Africa like in the tropics where where my home is the temperature year-round goes from like 68 to 78 that's like the entire temperature range like here in Texas will go from zero to a hundred and ten okay from zero to 110 as our dynamic range the dynamic range there is 68 to 78 so when it's six d82 them that's like zero here and they'd like pull out the heavy parkas and the coats and the blankets and then 78 is just like dying hot okay but I digress show me that you're actually watching this by topping Bosco and just Bosco and then I'll know that God made it to the end then the people that don't make it to the end they're wondering why people are topping Bosco but I really to be honest with you wonder if anybody is gonna make it to the end of this video because it was a lot of stuff that we did it was kind of long with okay Paul Marquart from top tech boy calm think about giving us a thumbs up think about subscribing to the channel think about sharing this with other people I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 43,753
Rating: 4.984127 out of 5
Keywords: Servo, Joystick, Arduino
Id: KCdoRDDLWgo
Channel Id: undefined
Length: 49min 13sec (2953 seconds)
Published: Tue Oct 22 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.