Arduino Tutorial 40: Controlling DC Motor Speed and Direction with Pushbuttons

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy comm and we are here today with lesson number 40 in our epic in our legendary new and improved series of Arduino tutorials what we are looking at today is the homework that I assigned you in Lesson number 39 and what that homework was was to take your motor controller the basic motor controller which you built in Lesson number 39 lesson number 37 I guess it was and then - this time around control that motor with two switches what you were supposed to do is the motor was supposed to start in the off position if you press the button once it's supposed to turn on slowly every time you press that button it was to go faster and faster and faster if at any time you press the button on the left it was to go slower slower slower if you start at zero and press the left button it's supposed to run backwards and then each time you press the left button it goes faster faster faster in the negative direction so your assignment was to start with the motor off and then to smoothly transition between full forward speed and full reverse speed by pressing either the up or the down button did you do it okay let's ask did you really do it I will ask you and part of your grade would be should I be grading your homework first of all did anybody even do the homework did anybody even try the homework I hope so how I would grade it would be like if I come in in the first time I press this button does it turn on and does it turn on to the slowest possible speed if I then press the other button does it turn back off and if I press the other button again does it reverse to the slowest possible speeds I want to see smooth operation now the natural thing would be to have some concept like motor speed and every time you click the up button increment motor speed by 10 and every time you click the down button decrement motor speed by 10 but what is the problem the problem is around zero the motor doesn't run you've got to get to about a hundred before it turns on and this is the analog right you know the analog right between 0 and 255 if you're not riding a speed to that speed pin of a hundred it's not going to run and you don't want to leave your user sitting there poking that button trying to get it to turn on so one of the things that we have to do and I hope you did and I hope you will comment and let me know how you did is you've got to start by doing your paperwork alright and one of the things that I see over and over among my students and the people that I'm trying to teach is they jump right in and they start trying to build the circuit and code things before they have a roadmap before they have a template before they have a game plan in the old days we would call it a flowchart sort of a picture of how this thing is gonna work and so let's jump in and do that and you can do it along with me but what I want you to see is I want you to see about how I think about a problem like this ok so let's start with what we were given we were given that we would have two buttons alright we would have two buttons and this would increase the forward speed of the motor and this would either decrease the forward speed or increase the negative speed so we have this concept of motor speed which I will call M speed and the easy thing to do would just be every time you press the button increment motor speed every time you press the left button decrement motor speed and it would work for most of the cases but in fact it would not work in these important middle cases and so let's draw a graph here and kind of think of what we want I always like to draw a graph I like to start with a picture and like I just if there's anything I want you to learn is if you can make it work on paper you can make it work in the circuit in the Arduino but if you try to jump in and make this work in the circuit before you have it working on paper the chances of it working are very small so where do we start and let's just say that this is our M speed this axis up here is our M speed motor speed and what's something that we know about motor speed well that we can go full blast in the forward direction which we can call that like 255 and then we can call this down here - 255 now we understand that we can't write an analogue right of minus 255 so we know what this means conceptually is 255 in the negative direction and this is 255 in the positive direction so let's start here at this conceptual zero when the when the program starts running we need to be at zero now let's just imagine that we pressed this positive increment button well what you would do is you would think your tendency would to think ok add 10 to M speed but if you're at zero that's not going to work what you got to do is if you're at zero you got to come all the way up to 100 now that's for my motor maybe for your motor it would be something different now once you're at 100 you should be able to increment by 10 and then you should be able to step all the way up to 255 so this is going to be you know not to scale we're just going to kind of skip the ones in the middle and then when you get here to 250 if you add 10 it's going to go to 260 but you've got a peg it at 255 you've got to not try and write a number greater than 255 and so what are we starting to talk about here some if statement so what I would need is like an if statement if M speed is greater than 255 set M speed equal to fifty-five right so don't let it go above 255 so as you come up if you keep pressing that button you've got to hold it at 255 similarly if we're coming in the negative direction if we're at zero and hit the decrement your tendency would be to put it at minus ten but the problem is at minus ten it's not going to turn the motor on so you've got to come to minus a hundred and then at that point with each additional push of the button you could increment by ten and then it's going to be moving smoothly so this is pretty clear and then similarly when you get to minus 250 and then you subtract ten it's gonna go to 260 but you've got to kind of nail it and hold it at minus 255 so I would need another if statement if M speed is less than minus 255 if you start trying to wander off down here you want to set m speed equal minus 255 so that's going to keep my values between 255 and - 255 with those two if statements but we still got a deal with this problem here so we see that if I try to increment to 10 I've got to go all the way to a hundred if I try to increment to minus ten I need to go to minus 100 similarly if I'm at a hundred in I decrement I don't want to decrement to Nani I want to decrement all the way back to zero so what does this mean this means that I don't ever want to have a value between zero and 100 I want to go all the way to zero or I want to go all the way to 100 so let's let's handle this going up problem and the going up problem is if M speed 10 you want to set in speed equal to 100 right so if you try to go to 10 no you got to jump to a hundred also if M speed is equal to minus 10 you want to make em speed jump all the way to minus 100 so for these two special cases of ten jump to a hundred and then minus ten jump to - Andre so that will take care of those but this is the other problem the other problem is that if the other problem is if you come in and you go to minus 90 you really don't want to be at minus 90 you want to be at zero okay if you come here and you want to go to 90 you really want to go all the way to zero so this would be you're sitting at a hundred a hundred and you decrement to 90 then you want to go all the way to zero and similarly if you're at minus 90 you're at - a hundred and you go to minus 90 you want to go all the way to zero so so far I've got one if statement - if statements three if statements for if statements now I've got to handle this these two here and the problem that you can see maybe you didn't see but I'm going to go ahead and tell you the problem that you can see if you go to 250 and you increment you're going to be at 260 and then you pin it down to 255 when you're coming down you're going to be 55 45 you're going to be not at a hundred you're going to be at a hundred and five and then when you decrement you're going to be at 95 so the two cases you worry about is if M speed is Nani or 95 because coming down you could end up at 95 then wanna make em speed equals zero so there's two you have to worry about on the way up you got on the way down when you didn't go past it you got to worry about Nani if you go past it and come back to 255 because of this other rule then you've got to worry about the 95 similarly here if M speed is equal to minus 90 or minus 95 for the same reason you want to go ahead and set it at zero and here what you did was you're gonna set M speed equals zero does this make sense please stop and put questions if this doesn't make sense okay I think that you understand that you don't want to go above 255 so you understand that if statement you don't want to go below minus 255 you understand that if statement if you are at zero you don't want to increment by 10 you want to go to a hundred so you understand this one this was the one that M speed is zero okay so if you if you are at 10 you go all the way to 100 if you're at minus 10 you go to minus 100 you understand those it is the non e that would be coming down from a hundred you would end up at 90 go all the way to zero or if because of this strange other condition if you were at a hundred and five you would end up at 95 and in that case you would want to come back to zero okay so we are going to have one two three four five six if statements that would handle these kind of special cases so let's go in and let's start working on our circuit now and I will be honest with you this is a little I think this is a lot more complicated than you thought it was going to be when you were when we were just talking about it and so let's start by putting let's start by putting these switches in here remember on these switches that the pins that are pointing towards each other the pins that are pointing towards each other are already connected together so they need to be in a common call the pins that are pointing towards each other need to be in a column common column so I am going to put those up here I'll put one switch there get it plugged in good these switches actually fit in a breadboard better than most but they're still kind of hard the pins that are pointing towards each other need to be in a common column if you do not understand that go back to lesson number lesson number 34 and I show you the easiest way to use these push-button switches all right now here we know that one pin of the switch needs to go to a digital pin and so I'm just going to come down to pin 8 because it's available and then the other pin needs to go to a ground the other pin of the switch needs to go to a ground and so I'm going to come down to this common rail this ground rail which we set at the second to the bottom row all right you see this little this little negative rail we set that up if you don't have the motor setup you need to go back to lesson number 37 which where we showed you how to do this all right now I'm going to hook up the other switch the left pin is going to come down to I need a little longer wire for that one the left pin is going to come down to pin 9 on the Arduino okay so the to switch pins are our pins 8 & 9 which you can see there and then the other one I similarly need to ground the other pin of the second button I need to put into that ground rail alright I think I have everything hooked up and so now the first thing that I want to do is I just want to see if those buttons work right I want to get the buttons working because there's a lot of code here and what I want to do is I want to start by getting the buttons working because you can't go in and try to debug the motor if your basic button functions aren't working and like I say lesson number 34 we show you basic button function if you're not following along I guess I should say if you're not understanding what I am saying lesson 34 will show you how to set up the buttons let's come to a nice code view this should be pretty good this code here I am starting with where we left off in lesson number 37 and so how do you go to lesson number 37 to catch up well you come to the most excellent top tech boy calm and you look for Arduino tutorial number 37 and that will show you how to hook up the circuit and it will show you the code that we're using as a starting point so if you haven't done that you need to go and get caught up with where we are okay so with that you can come back here and again get a code view and now we're gonna incorporate the buttons well what did we add we added two buttons so I need an int and then I'm just gonna say button pin 1 and what was button pin one that went to pin 8 and then I'm gonna have an int and I'm gonna have button pin 2 and that went to Arduino pin 9 well if I have a button pin 1 I need a button 1 value and that's I'm gonna read I'm not going to put something in there I'm gonna read it and then I'm also going to need a button to value if I'm adding two buttons I need to do some more pin modes so I'm gonna do pin mode and I'm gonna do I've got to keep looking at this B one B will be p1 and be p2 so be p1 is and put because I'm going to read from it and then pin mode bp2 is an input because I'm going to read from it now if you remember in less than 34 I showed you how you can use these switches without an external pull-up resistor and the way you do that is even though these are inputs when you do the input it brings the pull-up resistor in now I want to turn those two pin highs those two pins high even though they are input pins so I will do a digital right and then I will do button pin 1 make it a high and then I will do a digital right then I will do a button pin 2 and it will be a high and so now I should be ready to read off of those I should be ready to read b1 Val and b2 Val so how do I want to start my program well one thing is I do need to clean this up remember in lesson number 37 we were kind of kick starting the motor I want to take that kick start business out and then I want to take this delay out and then I'm left with the three basic commands that will run the motor dyrone and der two are the direction pins and if we have them like this it runs one direction if der one is high and door 2 is low it'll run like this if der one is the other way in door 2 is the other way it will run the opposite way slowly try again der one is load dirt two is high it won't runs one direction Derwin Hyder to low runs the other direction and then you do an analogue right on your speed pin of motor speed and that will set the speed but for right now we want to read those two buttons and so I am going to say always making sure I am looking at the right thing V one Val and B 2 Val so I have button 1 Val I am going to read it so it is an analog read of button pin 1 and then I'm going to do button to bow is equal to analog read button pin to so I'm going to get those two values and then I'm gonna print them out just to make sure that I'm reading them correctly in this configuration we expect if the buttons not push for it to be sitting at a1 and when we press the button we expect to see it at a zero so let's do some serial print serial dot print and I want to start by just saying but ten one space and then I want to do a serial dot print and what is button one well it's going to be the one vowel and then I'm going to do a serial dot print and then I'm going to do a string space button to space as a string and this will be the last thing so I'll do the print line to take it to a new line serial dot print L in and I'm going to do B to eval and so this should show me whether I have the buttons working because nothing's gonna work if the buttons don't work so let's go ahead and download that yes it downloaded and now let's call up a view that you can see these things spinning by okay so it is saying something very strange here let me go to a better view what if I come over here can you see it yeah okay so you see this is very strange look at that button button 1 + button two are giving me crazy ah okay let's look and see we made a horrible mistake here when you read from a button were you screaming at me that's digital read that it's not analog read that is digital you see why we check our buttons we check our code as we go actually I did not do that mistake on purpose that was a real mistake but what I want you to see is how to debug and you don't want to write the whole program and then the thing doesn't work we're getting the buttons working so what we would expect is we would expect a one on both of those so we download it again and look at that a 1 and a 1 now the real question is when I press the left button does button 1 go to zero and in fact it does look at that boom beautiful now if I press button 2 does button to go to zero boom what do we know button 1 and button two are working so now I can move forward with confidence in what I am doing ok so let's sort of think the first thing that we want to do is we want to read the button values which we have done now before we worry about the simple cases the simplest thing remember we set up a variable up here motor speed motor speed needs to start at zero so when you start the program motor speed is zero the motors not going to be running now before we start dealing with all of those special cases what we need to do is we need just to do the simple thing and that is if button one if button 1 thou equal equal zero what does that mean that means someone pressed the button and what is that that's the decrement that's the slower one so in that case M speed is equal to in speed minus 10 now we will come in later and we will adjust for all the special cases but to start with let's just say if you press the decrement button speed goes down by 10 now we want to delay because if I press that button and it's going real fast it might read 50 button clicks and so we've got to slow it down so I'm going to put a delay and how much delay time I've got a define delay time of the top now and so that is a new variable integer delay time and let's just set it at a half of a second so it doesn't make too many readings too quickly okay so if button one is pressed I'm gonna subtract ten and I'm gonna wait so it doesn't just sit and keep reading a bunch of a bunch of button presses so that looks good what else do I need to do well if if button to Val equal equals zero in that case what do we want to do well the simple thing before correction is we want to make in speed equal m in speed plus ten does that make sense so left button we decrement speed right button we increase speed so those are the two nominal things we also want to put the delay in so when I press the button it doesn't read a whole bunch before I can get my finger off of it is also probably would help to debounce the switch a little bit if you have a bounce problem with the switch all right now we have to take care of those special cases you never want to analog write a value greater than 255 so we've got a peg M speed so that it doesn't go beyond 255 so that is the first case if M speed is greater than 255 then what do we want to do we want to peg it to 255 so to p.m. speed we just say you go back and you equal to 55 okay what is the other one if speed is less than if it's below minus 255 that's the same problem on the other end then what we want to do is we want to say in speed be equal minus 255 if I'm thinking about this right so now we've got those two outside ones taken care of now what were the other ones so I think we said that there were several of these that we had to take care of so let's look at the other ones are around zero and we've got to think about what to do in the cases around zero so first of all what we know is if M speed 10 so that is you're at zero and you tell it to speed up it's not going to run at ten right so if you're at zero and you tell it to speed up you're going to have to make him speed equal to 100 like that similarly if M speed L minus ten what is that case that is you're sitting at zero and you tell it to go backwards it can't run at ten you're going to have to give it a better value so in speed is going to be equal to minus 100 so that takes care of those now the other one is when you are making that transition when you're making the transition let's get back over here on the way down remember if you're making the transition from a hundred or a hundred and five down you want to go all the way to zero so if you're eight ninety or ninety five you want to go all the way to zero if you're at ninety or ninety five you want to go all the way to zero so let's put those in if in a speed equal equal 90 or which is the bar bar which is above the slash above the backsplash or in M speed equal equal 95 what do you want to do turn it off man you can't run that slow you got to go all the way to zero M speed:0mph revs:917rpm [Music] like that alright so we've taken care of all of the special cases I do believe now what I want to do is to check this now again we always want to be doing I want to check this and what we always want to be doing is sorry okay what we always want to be doing is kind of checking our code so let's go back to this view where you can see I think I have a better one than that where you can see the code is it okay yeah you can see the code there now we know our buttons work so we really want to be looking at button speed or yeah motor speed so we'll come in here and we're just going to print out instead we are going to be printing out motor speed and then we will print L in in speed so before I write these things I want to make sure that my motor speeds looks good and so let's download that Oh No what happened if B Val equal equals zero ah big if were you guys yelling at me little if hopefully I didn't do that over and over up I did didn't I yes were you guys yelling at me hopefully you were okay hold your breath no denied I forgot my semicolon the real problem is you guys are not holding your breath another one what is this oh man we were not putting in the things that we needed to put in our semicolon okay this time for real every last one of you hold your breath oh this did not need to close there that was craziness okay right you see we had a closed parenthesis where we didn't need one I hope I was not doing that all right let's try it this time gonna work this time yes okay so now we start with a motor speed of zero is that what we expect yes if I press the right button and let's see I'm gonna get further out of your way so you can see me I'm going to press my right button and I should see motor speed it's gonna try to go to ten but then the special case should send it to a hundred okay boom a hundred now if I press it again what 110 if I press the decrement it should go to a hundred decrement takes it to 100 another decrement should take it to zero boom now if I go a hundred and then I just keep pressing hundred and ten and then let's just go all the way up to 255 we want to make sure that it pegs at 255 okay now it's pegging it to 55 now when it comes down 45 35 each button is changing it by one okay it's doing very well okay coming on down now this is the thing you see this was that special case you see how I'm at 105 when I decrement it it's not gonna go to a hundred it's gonna go to 95 which which is why we had to have both of those if statements and it goes to zero food let's check on the decrement if I hit decrement now it should go to minus 100 whoa that was crazy it went to minus 255 so I've got a crazy mistake in there so somewhere I have got a crazy mistake in here and that thing just absolutely pegged out let me hit a reset here and see if I can get that thing to come back okay it's at zero and then if I hit decrement one time it goes all the way to minus 255 so let's see what the problem is we say motor speed is equal to motor speed minus ten if I hit button one so I did that and so my motor speed is at minus ten it'd be to Val is zero okay okay so let's look at these special cases my M speed would be this was the mistake I said - if M speed is less than minus 55 that should be minus 255 that's certainly one mistake there and then M speed if M speed is 10 M speed is a hundred if M speed is minus 10 M speed is minus 100 that looks good if M speed is 90 or 95 you go to 0 if M speed is minus 90 or minus 95 that you go to zero and so that looks pretty good let's try and see if that fixes that but that was kind of still I'm a little bothered okay so I'm gonna hit the negative button one time and it - 100 - 110 - 120 coming back that's looking good okay - 100 now it should go to 0 goes to 0 - 100 let's take it all the way to minus 255 let's see if it pegs at minus 255 looking good okay it pegged it - 255 now it comes back to 245 and now we'll see if making the transition around a hundred works ok at 115 he goes to 105 one more 95 goes to zero boom ok guys you see if you can get your butt and working and then your motor speed working can you imagine those two problems that I had those two mistakes if we gone all the way and then wondered why our motor was not running our motor was not running correctly we would have never figured it out ok so now we have a good motor speed at this point we have a good motor speed at this point but what we have to see is we have a positive motor speed and we have a negative motor speed so let's think first of all if in speed equal eq be in speed be equal 0 if motor speed is zero what do you want to do well you want to analog right and you want to analog right to speed pin to speed pin and what do you want to right you want to analog right zero because if motor speed is zero you want it to be off so that's one case if motor speed is zero you want it to beat off okay now if motor speed is greater than zero what do you want to do well you've got to tell it which direction and this one is pretty easy it's going to be the forward direction and so it's going to be pretty much this code that we had down here which I will copy and then I will delete it and I will put it here so this is the forward direction where dur one is low and r2 is high I'm just simply going to write speed pen M speed and remember M speed is good now I've done all those special cases so I have it taken care of if M speed is zero we're good I have it taken care of if M speed is greater than zero we go in the forward direction and we go a speed of M speed now if what is the other case M speed is less than zero what do we want to do well we're gonna do almost exactly this so I will copy all of this first thing we got to do is take care of the direction if motor speed is less than zero we want to run backwards so to run backwards what do we do we set dur one high and we set dur two hello now remember em speed is negative you cannot analog write a negative number that negative was to tell us we wanted to go backwards we took care of that we're going backwards so on M speed we're going to write the absolute value absolute value absolute value of M speed does that make sense we took care of the less than zero the negative here and now we just write M speed so now I need to come back over here to my little motor my excellent little motor and then we are going to make sure let's see I think I'm going to keep it on this view for just a second make sure your power supplies on and everything is hooked up according to your earlier lesson lesson number 37 was your basic motor controller now let's download Oh what is it what is it complaining about ah I didn't put a smile on were you yelling at me I hope so okay yes okay and now if you look at motor speed the motor speed is zero why is the motor speed zero the motor speed is zero because I'll scoot this over a little bit the motor speed is zero because right we started in the program we said start off with a motor speed of zero now if I press this let me make sure that that's not hitting my little wires get my little wires out of the way so we don't have any obstructions now if I press that increment button once it should run in the forward direction if it can really run at a hundred I played with this motor so maybe I've gotten it messed up a little bit but let's do that and okay good news it's it's trying to run I'll give it a little a little help okay this motor is just not going to quite run at a hundred maybe it is with little help but if I make it go a little faster okay at a hundred and ten it's pretty happy running okay let's decrement down to a hundred okay now if I decrement it should go to zero and look it goes to zero now if I decrement again it should go to minus 100 and it should run in the opposite way okay I give it a little more help - 110 okay and at minus 110 it is running counterclockwise counterclockwise that - a hundred stop and then when I go this way increment it's running it's running positive okay so what is this is working all right this is working it's just when I first started working with this motor I could start it at a hundred and now kind of in this position or under these conditions it won't quite it's more like a hundred and ten or a hundred and twenty but let's go ahead and see if this will work I'm gonna increment it up man it is starting to run faster and faster and faster maybe I should go to the full view here okay so I've got faster faster faster faster faster and now we are all the way up to full speed we are all the way up to full speed boom this thing is really really really working now let's start decrementing it'll start by going slower and I can noticeably perceive it going slower and then we're going to get back down to 115 it is barely running 105 it is barely running and then I go to 95 it goes to zero and it stops and now if I decrement again it is trying to run in the opposite direction give it a little help there okay and then it's going faster and faster and faster in the negative direction until it's going in the negative direction a mad amount at 2:55 and then in that negative direction I can start slowing it down boom did you see this this thing is working like a champ okay guys I think when I gave you this assignment it probably seemed a lot easier than what it turned out to be because there were a lot of special cases that we had to take into account a lot of special cases that we had to take into account let me know your comments down below did you guys get this working if I've been giving you if I'd been giving you a grade would you've gotten an A an A+ or a hundred would have been the first button push it would have run okay on mine my algorithm was right but it didn't run on the first button push because really I should have tested a little more carefully and it doesn't quite mine doesn't quite run at a hundred and so I would give myself probably a 92 on this because I should have determined that value a little bit more I don't ding myself because it doesn't run the first time because that's part of learning is you're gonna make mistakes and and probably one of the most important things you're gonna learn is how to find your mistakes but did you guys get this working on your own did you do it with a strategy like I did it did you get it working but you had to push the button a whole lot of times before it before it actually came on leave me comments down below and let me know how this thing worked for you okay this has been kind of a a little bit more of a tedious lesson but I hope you guys went through it I hope you'll leave your comment down below think about giving us a thumbs up think about subscribing to the channel make sure to ring that bill so that you get notifications when when the next lessons come up palma quarter from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 38,055
Rating: 4.9822221 out of 5
Keywords: Arduino, L293D, DC Motor, Motor Control, Pushbutton
Id: fxgcnJJlTd4
Channel Id: undefined
Length: 45min 34sec (2734 seconds)
Published: Tue Dec 10 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.