Paul McWhorter arduino lesson 15
You know, learning a little bit more, every lesson. A little bit more about programming a little bit more about circuits, a little bit more about programming, a little bit more about components, and it this point: you’ve got a really good idea for the the basics that you need to do some pretty sophisticated programming. So we’re going to start looking at additional components: we’re gon na start looking a little bit more on the circuit side of things, and so what we’re going to do today is we’re going to start looking at making things move where we can get get things moving With the Arduino, and so the component that we’re going to be looking at is called a servo and the way a servo works. Is it moves? The position of this little horn can be controlled by the commands that we send to this from the Arduino. The important thing to understand about a servo is is that you need to understand that it is not a motor you can’t just turn it on and have it go in a circle. It typically has a limited range of motion and that limited range of motion is usually from about zero degrees to 180 degrees, nominally that’s kind of what they say. They are most of them, don’t actually go to through that full range, and so we’ll talk about we’ll talk a little bit more about that in a minute, but this opens up all types of exciting new possibilities on your project to where you could get input from The user and you could use that to control where this thing went to, or you could have a sensor and have the sensor control what this thing did.
So you could have it. You know you could imagine something where you could have like a little window and if it got hot then this thing could open the little window or close it and again you know there’s a lot bigger ones that you could use, but we are we’re playing around With a small one, this small one is the one that comes in the sparks on inventor kit and for this one there are basically it’s a small one and it can be driven directly from the Arduino. There are three lines on most servos. There is on this one: the color code is there’s, a black, there is a red and there is a white, and what those are is the red is where you put the power to the servo for this one. It wants five volts, the black is for ground and the white is the control line of where you send the commands to make the thing actually move now. This is really really really important. These small ones like this one it comes in the spark phone kit. You can power those directly from the Arduino. The Arduino has enough power to power. A small one like this, but the thing is, is that these servos draw a lot more current in a lot more power than what you think they would, because it look like it’s doing that much just spinning back and forth. But it turns out these things really really draw a lot of current.
You want to make sure that you’re not trying to drive one from the Arduino power supply that is trying to draw more current than what the Arduino can provide, and some of them that are just a little bit bigger than this are too much to try. To put on the Arduino several things happen, one is you can damage the Arduino. You can burn out your Arduino. Another thing is, is that maybe it doesn’t burn out the Arduino, but it doesn’t run very smoothly and you get a bunch of glitchy type of behavior out of it. So you want to make sure that you have enough power for the servo that you’re using in some cases what you can do is you can still use the control line hooked up to the Arduino, but on the power line you could power it from something that Could provide more current like an external voltage supply, you could you could power it making sure you’re always using the right voltage for the particular servo that you’re that you’re running so sometimes the Arduino can control larger servos, but it just needs to be doing it through The control line, and not through the and not trying to draw power the power from the Arduino one thing. I always remind you any time you start getting more power supplies or getting your circuits more complicated, make sure all of your grounds are hooked together. The ground from the Arduino needs to be hooked to the ground from the circuit that needs to be hooked to the ground from the power supply.
So when you’re doing these things, you know usually, when I’m looking at students work, one of the common problems that I see when they’re having problems is they don’t have all their grounds hooked together, so make sure that you get your ground. Your circuit really grounded your circuit really grounded well. So this is a pretty pretty exciting little component that we can start working with. Another kind of warning I got to give you is that me, and so much of the stuff that you do with Arduino, Xand, electron and engineering is like everything works perfectly. Just all these components are like cheap and they work great, the servos, not so much. The servos thing like I have more problem with servos than just about anything else. I’Ll get one and it doesn’t work or I get one and it’s jittery or I get one and it doesn’t work the way it’s supposed to and so just to understand, there’s a lot of there’s, a lot of kind of, not so great servos out there, and Particularly these little cheap ones, a lot of times, you’ll get one that doesn’t work very well and one of the things to be aware of is when you buy one of these little guys. It says that it can go from 0 to 180. So if I hooked a pin up to this, this would be like it would. You could position it supposedly from 0 to 180, ok from 0 to 180, but what I found is is that they rarely ever go over the full range that they advertise and one of the things that’s a problem is, is that if you try to overdrive it, If you try to drive it past the point that it wants to go, it starts chittering and it starts drawing a whole lot of current.
That could damage your Arduino or it could damage your servo, and so what you’ve got to do is usually when I get a servo. The first thing I do is I write a little program just to see for that servo how far it wants to go and like. If you look at this little guy here that I have, you can see that he’s not going over the full hundred and eighty degree range and that’s because I’m not driving him to go over the full hundred and eighty degree range. Because when I was playing with it, I saw that he didn’t want to go all the way to zero, and so I didn’t try to drive him always all the way to zero. So when you get a servo it’s good to play around with it and see its natural range of motion that you can get without it starting to starting to jitter, the other thing is: is that don’t try to turn this with your hand that that can be Very bad on a servo don’t force drive it. You know only drive it electrically. Don’T come in and start trying to spin it by hand, because that can cause a problem with some of with some of the some of the servos, and so Sarah is one of the trickier little components to work with if you’re having trouble with one. You know. Sometimes what you need to do is you just need to kind of toss it and get another one, because a lot of times some of these things just don’t work very well, okay.
So what are we going to do? We’Re going to start playing with this thing and I guess a couple of things. What the objective of this lesson is is that we want a circuit where we have a potentiometer, and then we want the Arduino to read the voltage off. The potentiometer is a voltage divider. I think it was lesson 10 things that we went through how to use the potentiometer as a voltage divider and how to make Arduino Reed’s from it. So what we’re going to be doing is we’re going to be reading the position of the potentiometer by reading the analog voltage off of the center pin and then, as you spin, this thing around that that number that you read the voltage will go from zero to Five volts, the number that you read will be between zero and ten twenty three with ten twenty three representing representing five volts and 0, representing zero volts and so you’ll be reading a number on an analog pin between zero and ten twenty. Three then, what I want you to do is to take that number and have that then control where the where the servo is pointing. So basically I want the servo I’m, not the servo, to track the potentiometer. So if you have the potentiometer all the way to the left, the servos all the way over, if you put the potentiometer the servo smoothly tracks where you set the set the potentiometer so that’s the assignment.
What the good news is is that you pretty much already know how to do the servo or if you don’t, go back and look at. I think it was lesson 10 and then you’ll see how to use the I’m sorry, the potentiometer go back and look at circuit at Arduino lesson number ten to see how to use the potentiometer and what we’re going to learn today is we’re going to learn how To use this servo and so we’re going to probably just jump right in here, I guess I need to go back in pay for the code I’m, using for the code that I’m going to be using here. We use this schematic. This is on my website. At top tech boy comm – and this is again lesson 16 – and this is the schematic of how I’ve hooked up the potentiometer and the center leg going to pin a zero. The bottom leg going to the power real here: the power real going to five volts and then this leg going to ground and then ground going to ground. So again, all my grounds hooked up and then reading off of this Center Kent and so that’s. How you do the the potentiometer side of things I couldn’t draw the servo over here, so I’ll, just kind of have to tell you how I did the how I did the servo being on the on the servo. I am hooking the control line of the servo for this one it’s, the white line I’m hooking that to pin nine okay, the servo is hooked to 10 9 and to be used as the as the control line.
The thing to understand about that, though, is, is that you can use any of these digital pins. You want as long as it’s one with a squiggly line, because you’re going to be doing, one of these analog writes to it and so because we’re doing analog writes you need to make sure it’s a pin. You can analog right and those are the ones with the squiggly lines. Bottom, nine is a good choice and that is the one that I’m going to use. Okay, so I’ve got on this servo I’ve got the control line, which is the white line hooked up to ten nine I’ve got the 5 volts to 5 volts I’d. Go through a rail here that I establish from the from the Arduino and then ground to ground, and with that then you’re going to be nominally set up to start working with this thing. So let’s see if we can go in and let’s see. If we can start writing some code, let me get me a new Arduino, integrated development environment up here, so that we can get going ok. The good thing is, is you start getting into some of these components like servos and getting into some of these more sophisticated devices? There’S what’s called libraries and libraries are code that someone else has written either the people that develop the Arduino or the people that develop the servo or component product whatever it may be. They write libraries and then, when you use those libraries it makes it a lot simpler and more intuitive to work with the component.
It just makes it really easy to send commands to it so you’re not having to work down there at a very, very low level. You’Re able to work up where it’s a little bit easier, but to use those libraries you have to begin downloading the librarian. I don’t think we’ve done this before the way you load. The library is you’ve, got to know the name of the library and you start to load a library by the the pound sign, and you say include that is telling arduino to load a library. And then you have to tell it the name of the library. This is servo, dot, h and then close it and then what are we doing? We are loading, the servo library, okay, now another way you can do it. Let me just let me take this much of it off and you believe it or not believe it or not on these loads. I messed up here. You don’t put a semicolon. It just ends like that. So on, your library includes one of a few places. No semicolon: okay there’s. Another way, you can load it a lot of times. What you can just do is you can come in here and if you look under tools and you can look under sketch I’m sorry yeah under scanner, brer e and you can see the servo library is right there and I can just load my servo library. There include servo H, which I said, and let me put my comment back up there: okay, loading, the servo library all right now.
What we need to do is we need to set up some variables. Well, one thing that we’re going to have to do is we’re going to have to write to this thing. We’Re going to have to make writes telling it where to go to. So we need a variable to hold that number, that we’re going to write and what we’re going to write is we’re, going to write position so I’m, going to put a variable called position and I’m going to set it initially equal to zero. Okay declare and initialize. The POS variable okay, so that gets us a variable to use the other thing is we need to keep track of our pins and remember: we’ve got the servo hugged up so we’re going to say servo pin, and where did we hook that in my cert, in My circuit, we hooked it up to nine, so we need to tell it tell our we know Arduino that that servo is what can i okay, then I’m, going to have a potential, not not quite yet. What I’m also going to have is I’m going to. Basically, every time you write the servo, you need to pause just a little bit of time to give it time to move and so I’m going to have a servo delay so that I can put this in just to slow it up a little bit. So you’re not trying to overdrive it, okay, 25 and then okay and then then that’s probably going to be my variables now.
Let me tell you what we’re trying to do in this. Remember how I told you that, on these on these servos, you don’t want to overdrive them right that they say they go from zero to 180, but typically, they don’t really go from zero to 180, and so, if this one only wants to go to 25, you Shouldn’T, try to drive it to zero because then it starts shaking and it starts drawing a lot of current. It can burn the servo out or it can damage your Arduino. So what you want to do is for the servo you’re using you want to kind of find what the natural range is, and so what I want to do with this first little program is: I just want to go out to the user, and I want to Ask the user over the serial port. What position would you like to set it to and then I want to move to that position and what I can do is I can try to go to zero and if it starts shaking, I shouldn’t go to zero. So I’ll come back and I say: well, maybe I can go to 10 or maybe I can go to 20 and kind of find that range of how small of an angle it can go to without a problem and then at the same time I want to Find at the high end will it go to all the way to 180, or does it only want to go to 175? So I sort of need the low end and the high end that this thing naturally is happy, is happy running at and so I’ve got to write a program that will go out and ask the user for a number you type the number in and then it Goes to that value and then you can just sit and just sort of observe it and see what range that it seems to be that it seems to be happy at going in and so that’s.
Why? I need to write this program, so position is going to be the value that you get from the user that you’re going to set you’re going to try to set it to that position. Servo pen it’s not that’s, where I am getting the V that’s, where I’m sending the command to the servo of how foot position to go to and then servo delay is how long I’m going to wait after I write it, I think that’s pretty good on my Variables now this is the new part. Okay, when I put this servo that I’m going to be working with with a library, I need to now create an object for Arduino to work with, and that object is going to be a servo object that I got to name the object. I going to tell it what do I want my servo to be called, and I create that object when it’s a servo object with this library with it with the command servo? And you do this up at the top of the program like where you’re declaring your variable servo, and since this is a pointer, I could call it. I could call it my servo or I could call it. You know little servo, but I think because I’m pointing I’m going to call it my pointer okay. So what this tells Arduino is create a servo object called my pointer. You could call it whatever you wanted to that. You tell Arduino it’s a server with a servo with the uppercase s.
Okay, so now I have created the servo okay I’ve created the object called servo to work with an Arduino right now, let’s come down to the set up, and what are we going to need to do well, we’re going to be working over the serial monitor. So we need to do a serial dot begin. Okay and we’ll work at 9600, just because that seems to work pretty well and then what we need to do is we have to tell the Arduino. We told the Arduino hey, I’ve got a servo, it’s called my pointer. Now we have to tell it. How do you communicate to my pointer? Where is my pointer connected and the way you do that is, and you tell it that my pointer dot the catch you’re telling it that this object that’s a servo called my pointer. My pointer is attached to the Arduino at ten nine right. No, it is attached to pin nine, but I don’t put in pin nine in here that’s why I put this up here: servo pen, okay, it sat at my my pointer, which is a servo that I created here, is attached. The server at servo, pin and servo pin is nine okay, so now Arduino knows that I have. This object called that this object that’s a servo that we’ve named my pointer. Where is it attached it’s attached to the circuit at servo pin which is pin 9 okay? So hopefully that makes sense, I think, that’s all we need to do right now in our void setup now in our void loop.
What are we going to do? We’Re going to go, ask the user for input and then we’re going to put the servo at the angle that the user indicated so to get input from the user over the serial port. What do we do? Three things number one: we prompt the user for input after we prompt them for input. We wait for him to put it in and then after he puts it in, we read it. So the first thing we do is we are going to prompt him and we will do that with a AC r, VL dot, println it’s, all in one line, so I’ll put a bell in what? Where would you like to position position, the servo? Okay? So what are we doing? Prompt user for input? Okay, now after we pump to them for input? What do we do? We wait for it? How do we wait? We wait in a while loop or the condition is serial dot available, equal, equal, zero, so what’s that saying, if there’s, nothing on the serial port, that means he hasn’t put the data. Yet, while we’re waiting for data, we sit here in this loop between here and here, and we do absolutely nothing. It just sits there. Is there anything on the serial port? No. Is there anything on the serial port? No. Is there anything on the serial port? No, as long as there is nothing on the serial port, it just sits in loops as soon as he enters the number.
Then there is something on the serial port serial available is not zero and it drops out to the next set. So this just hangs until the person puts the data in then. What do we want to do? Well, we want to get that and where do we read that number into well that’s the position number that’s going to tell us where to go and we’ve already set that up so we’re going to say position is equal to? How do I read an int off of the serial port that was parse that was I’m sorry serial dot, parse and thing like that? So it goes out and it gets that number off the serial port and it puts it into the variable POS. Okay, so that should do that so now we know what the position is. So how do we get the servo to go to that? Well, we talked to the servo what’s, our servo name, my pointer. So we go my pointer. Why? Because that’s what I named it! Okay, my pointer that’s, what I named it dot. What do we do to it right and what do we write? We write our position. Okay, so this number position we’re going to write to the servo called lap pointer and that’s. What it’s going to do? Okay, let’s get Bette back up here and put some comments and read user input right POS to servo. Alright, I think this is going to work. Okay, look at this.
This is happy, so let’s go to our serial, monitor and it’s. Asking me: where would you like to put the servo well let’s, just say 90 degrees kind of in the middle okay? We were probably already about there. Let’S say 45 degrees. Okay, there, it goes let’s, say 130 degrees there. It goes okay. What was the purpose of this program? It was to find that range of motion that this is comfortable going without it, starting to shake and vibrate so let’s just say if I go to zero okay, now what I can do is I can feel this thing is really shaking and straining, even though that You can’t see it. I can see that’s not good, so I’m going to go to like 30 degrees to get it off. Of that you wouldn’t want to leave it there. Okay, so now it’s come up and it’s gone away from that let’s say see. If I could go down to five degrees, okay, it’s still it’s, still straining at five that’s too far, let’s go back to 30. Okay! Let me see if I can go to ten still, not happy okay at 15, it seems pretty happy so let’s see you see. If I go to 25, it goes there. I go back to 15, it goes there, so it seems out let’s see if I can go to 20. You see it seems like down in that region. 15 is about the low end of where you can go with with it still being happy and so make note that the low end, the low end is 15 degrees, so I’ve written that down now let’s.
Look at the high end. If I go to 180 okay, it is straining so let’s go to 160. Okay, 175. Okay, it’s still straining a 160 okay. Let me try going to 170. You know it seems pretty happy at 170 and if I go to 165, you can see it moves and if I go to 170 it moves, and so you see – 160 170 it’s happy it’s still in that range and so I’m going to say for this particular Sensor that I mean for this particular servo I’ll the one down here, but that particular servo is doing pretty good from 15 to 170. So, basically, I can go from 15 degrees, which is over here to 170. Now the thing that kind of irks me you pay for 0 to 180 and you get 15 to 170 it’s like you’re kind of getting cheated out of 25 degrees. But I guess that’s what happens when you buy a three dollar servo, that if you spend 30 on a servo, you’ll, probably get one that works a little bit a little bit better than this. But now we sort of see that the range of motion and that this servo is happy working in so let’s let’s play around with our program a little bit and let’s kind of take it up to the next level. I sort of know 15 to 170, so let’s see if I can just kind of sweep back and forth between those two.
So how would I do that? How would I smoothly sweep over a range? Well, I could do that with a for loop. I could say 4 and what would I want to loop on well I’m wanting to loop on what the position? Okay, so I can go from position is equal to. Where do I want to start so Europe? No, because 0 is too far. Let’S start where I want to start at 15, so for position is equal to 15 and then to position. Keep looping as long as position is less than or equal to what 170 was what seemed to work pretty. Well, ok and then what we would say is position is equal to position plus 1. Okay, and this is a for loop. So we start a clause and then we come down here and we end the clause and everything in between here. It will do starting the first time through position. Pos is going to be 15 and then 16 17 18 it’s going to keep looping as long as POS is less than or equal to 170 once it is not equal to one less than or equal to 170, which means when it gets to 171 it’ll drop Out and then move on what’s the rest of the code, and then we tell it POS as POS plus one okay. So what do we do inside of this loop? Well, we will want to do a my pointer dot right and what do we want to write physician, send servo to us? Okay, and we got to be good and remember.
Our semi cannot once and then after we write it, it’s always good to delay. So we’re going to delay a little bit so that we’re not trying to drive it too fast and I’m going to say 25 I’m saying the 25 milliseconds is good. So we set that up a servo delay, and the cool thing about this is: is that if you have lots of delays in there and you want to tweak them, you just do it one place up here and then everywhere you use servo delay, it’ll it’ll change. It so let’s just say this is going to pause a mo moment. Okay, this should go from 15 to 170. It should be happy in there, but once we go there, we want to sweep back so it’s going back and forth. So we need a second look that first loop is going to take it all the way over to here and now we need to take it back. Well, I like to copy and paste, and so I’ll just copy and paste this and then kind of fix the things that needed to be fixed, and so here is the second loop, but here instead of going from 15 to 170, where do we want to go? That’S right, we want to go backwards. We want to go from 170 to 15, but you got to think you want to loop as long as position is less than fit know in this case you’re going backwards, it would be greater than or equal to 15.
So you would want to go 170. 69. 68, so you would go as long as you’re more than 15 and because you’re coming backwards, this that should not be post. That should be poss position, position position, plus 1 positions position plus 1, but we don’t want position to go plus 1. We want to go to go from 170 down to 15, so position beat should be what position should be position: equal position, minus 1? So on this one, it’s counting backward, so it counts forward to 170 and then backwards from 170, and then the rest of this should be work right. I think this is going to work. Look at that this is going to work. Okay, and here it goes. Okay, it’s sweeping up to 170 and then sweeping smoothly down from 170 and, looking at it like this, you can see that you’re really getting cheated out you’re, not getting. You know, you’re, not getting 180 degrees. Out of this thing that you’d hope to that that’s up. You know that’s not that’s, not such a hot deal, but the rather limited range of these. These cheap, not cheap guys, but nonetheless that is really something that I think is pretty cool, and you can imagine that there’s all types of things you could do what’s a way that we could make it run a little faster. Maybe well, you could say, take out the delay, but you really need a delay in there right.
You really need that delay in there, because you don’t want to overdrive it. What what can we do? What, if we said position is position plus 2 and then let’s? Let it go slow on the way back, so it’s going to go faster this way and slower that way, let’s see if that happens, yeah look at it go and then nice and slow, smooth back and then faster coming this way and slower going back, let’s say: If we can even exaggerate that a little bit more, you see real fast in one direction, slower in the other, because we’re jumping five steps at a time going in the forward direction. Five steps at a time, so you can play around with this and there’s a lot of different parameters that you can play with to kind of make it do different things and sort of do what you do, what you would like it to do? Okay, but what was the objective that we wanted here? We wanted this thing we just kind of been playing around with it to see what its range of motion was. What we really wanted to do is we want it to track the position of the potentiometer and I’m going to download that first program. So this thing will stop spinning back and forth. Ah, okay, stop it there for a second alright. So what do we want? This thing to actually do we want it to not be set by a position from the serial monitor.
We want its position to be set by where this potentiometer is and remember, we’re reading this potentiometer the center leg of the voltage divider over here at Penn zero. When you read a voltage from the potentiometer, you are going to be reading for the circuit. The way we have it set up here, hooked up to 5 volts you’re going to be reading a voltage from 0 to 5 volts. Ok! But what number are you going to see at the at the analog input pin 0 to 5 volts is going to correspond to a number between 0 and 10 83 okay, so the number we’re going to be working with is 0 to 10 83 and then that Needs to map to now you got to think about this. You don’t want 0 a read of 0 on the potentiometer to take you to zero because that would be over driving the potentiometer. And if somebody came in and didn’t know all this stuff, you and I know about not overdriving potentiometers and they turned it all. All the way to the left – and it would put it in that position to where it would be shaking and the user without knowing any better it could burn. I could burn your stuff out, so you got to make sure that he cannot set this at lower than what we determined the low end was 15. So when I read a value of zero from the potentiometer, what now you should I write to the servo? Not zero, I should write 15, okay, think about that, and similarly, on the high end, when I’m, all the way to the other end and the potentiometer is reading 1083.
I don’t want that to write to 180. I want that right what 170? Okay guys! This is not something you can just wing and if there’s nothing else, you learn in these classes. I hope that you learn how to do how to apply the math that you learned in a math class to a problem like this. What is our independent variable? What is our variable on the horizontal axis? It’S zero 1083 it’s what’s coming off this potentiometer? I can set this to zero. I can set it to ten. Eighty three. This is the independent variable it’s, the horizontal axis. What is the axis that I’m, going to the dependent variable? Is this it’s, this it’s, the vertical axis? The vertical axis needs to go from where 15 to 170, when the horizontal axis goes from 0 to 10 83. So this is going to be some math, so let’s look at this. I want you to be able to read this, so let me see if I can, okay, so controlling a servo with Arduino, and I wish I was one of these guys that had that real neat precision engineering looking handwriting – but I am NOT one of those guys And so you will have to live with my poor handwriting. I do it as neat as I can controlling the servo with Arduino number one. What you need to do is figure out the minimum and maximum range of travel for your Arduino servo for mine.
It was 15 170 don’t come in and use 15 170. If I plug this servo in it’s going to be different than that one, and so for the particular servo that you’re using you need to find out what that safe range is for this one plugged in it’s 15. 170. What you need to do now is map that range onto the values you will get from the potentiometer. So let’s do our math here: okay, let’s do our math okay, so the independent variable is what I control. That is the position on the potentiometer that is going to create a number between what and what, between 0 and 10 23 okay, so that’s, this horizontal axis, the potentiometer reading pot reading, is going to be between 0 to 1023 over that range. What values do I want to apply to the servo? Well, at the low end, I want to apply 15 and at the high end, I want to apply 170. So when the potentiometer reads zero, the x value I want and the position or the servo to go to 15 when the potentiometer reads: 10 23. I want to put the servo at 170, so we have two points and we have a line, but what we need is we need the equation of that line so that for any reading on the potentiometer, I can calculate the POS value the position value to put On the servo, so how does that work? We find the slope of the line.
What is the slope let’s get back to math class? You should been doing this for years. Just you guys, it’s, like you, learn all this stuff in math and as soon as you walk out of math it’s, like you forgot it, you walk back into math. You could do these all day long boom boom boom boom. I see you, I see you working these working things working these, but you won’t crowd a math class and you come into engineering class and it’s, like you, never saw a line in your life. Okay, so know how to take those math skills that you’re learning in math class and bring them into the real world. How do you find the slope of the line? M is y2 minus y1 over x2 minus x1. What is our y value? Its 117 170 and 15, so y2 minus y1 is 170 minus 15 X. 2 minus X 1 is 10 23 minus 0. 1. 70. Minus 15 is 150. 5. 10. 20. 3. Minus. 0. 1023. I hope you understand this. I hope you get this in your head. Okay, then, how do we we’ve got the slope of the line? How do we get the equation of the line? Well, what do we have? We have the slope and we have a point so let’s use the point slope form of the line. What is the point? Slope form of the line. Y minus y1 is M onto X, minus x1.
I could choose from my point either this one or this one. This one has a zero in it a smaller number, so I think that one will be easier. So I will use that one, the other one would work I like to try to find the easy way so Y minus y1 is Y. Minus 15 is equal to M, which we already said was 155 over 10 23 onto X, minus x1, and what is x1? X1 is 0, so Y minus 15 is equal to 155 over 10 23 X minus 0. I can move the 15 to the other side, and I have Y is equal to 155 over 10 23 X plus 15. Now let’s turn this into the variables that we’re using. I always do it in XampY, because you remember back from math class, but our. Why was POS so our Y becomes, our y becomes POS in our X was pot reading, so POS is equal to 155 over 10 23 times pot reading plus 15. Okay. This is the equation that we use to calculate the position that we’re going to write to the servo from the number that we read off of the Arduino okay offered the potentiometer on the Arduino, so let’s see. If we can make this little guy work and I’ll see, if I can kind of leave this equation on here, so that you can continue to see our nice math work as we continue on with this ok, you know what I can probably back this off.
A little bit now, so you can see the whole thing. Okay, all right! So hopefully you have your circuit put together and you can just code a long sight of me so let’s see here. Let me get these things set up. Okay, this is going to be kind of amazingly simple in our loop. What do we want to do? We want to go out and read that value off of the potentiometer? How do we do that? We say our variable. What did we set up? We better set up a variable up here and we’re, going to call that variable, int and we’re going to call it pot reading we’re not going to give it an initial value, declare variable for potentiometer, leading, okay and so pot reading. How do I read it? Well, we do an analog read uppercase. Are we got to tell it where we’re going to read? Where do we read? We read not servo pen, but we read pot pen which we have not set up yet so we need a pot pen when we set up these variables. We did not have a potentiometer in the circuit pot. Pen, for me, is a 0, so integer pot pin is equal to a 0, not o upper case. A 0 won’t work, the other way: okay, connect potentiometer to 10a, a 0 okay. So now we do the our analog read where pot 10, which it knows, is a zero okay. What we’re doing is reading both tension? Mixer, alright, so we have our pot reading now.
We’Ve got to calculate our position. How do we figure out the position we want to go to based on this? Well, we just did. The mat position is equal to what well it’s equal to 155 divided by 10 23 and guys when I do division. I always do point because if you don’t put a decimal in there and you do integer division strange things can happen and they are usually not good, so always put 155 point divided by 10 23 point. That tells it to give me a full floating point number in here, okay times what pop reading that I just took: okay plus 15, because that is what the math said now we know position what we do. We write it so I say my pointer that’s, the name of my servo is equal to or my pointer dot right and we write that position okay, so this is calculate position and this is right position to serve okay. One thing to understand: is your equations? Not going to be the same as this, you need to do this for your own part and you’re, probably not going to be at 15 and 170, so you need to go through the math for your particular part, okay, and when I come around to grade this, For those of you in my class I’m going to be looking and making sure that you didn’t just lift my equation, but that it really is right for your for your servo okay, I’m, going to be checking you and checking you twice on that.
Okay, if you’re doing this on your own, be good because you’re, not cheating anybody but yourself if you don’t, really go in and I’ll have a know how to do this. Okay, I think this is going to work. Let’S, take a look at it. Okay, so now, as I twist this knob, it should gently go from 15 all the way to 170. So, if I’m, all the way to the left it’s over there at the start and look at this, I can just very smoothly let’s go back a little let’s come back a little. You see. I can put that wherever I want just by turning this all the way to 170 go back a little bit come back. That is just super cool and I like the little arrow that’s on there, because you can kind of see you know you can kind of see where it is a little better. You can see the thing moving is I turn this well. I just think that this is just super super super cool. Okay. I just I just really like. I really really like playing with this thing. Okay, but one of the things that briefly want to kind of touch base with you on is. Is that what I want? You to do now is I want you to go out and kind of create something with this. I want you to just hook up some simple little thing. Maybe you make a paper box that you open the window on or or something that you that you hook this to, but I want you to do something and you know we for those of you in my classroom.
We’Ve got the 3d printer. You can print something up on the 3d printer. That does something you can make something, but what I want you to do is go out and be a little bit creative in what you create. With what you’ve learned with using a servo and a potentiometer, or hook the servo drive it from the serial port or drive it from something else? What do you remember? The color sensor put the color sensor on there and, depending on the color of the card, the cards, the color of the card, that the sensor sees make the servo do something interesting. So so the problem is: is that so many times in engineering, quad osseous? Is it then, we get so focused on the the nuts and the boats bolts in the code that we don’t really think the 4y4? Why do we learn this stuff? Okay, the reason we learn it is we want to do cool stuff, so I want us to spend more time thinking about the cool things that we can make and not just sitting here, making things move for no reason and one of the things that I find That I find interesting, those of you that are in my class know that for most of these projects we have a contest, and you know whoever whoever does the best wins. The contest wins a stereo or some some cool piece of electronic gear and in the kind of thing that I see is, is that sometimes the kids that are the best coders and have their their program and their circuit all up and running in five minutes they’re Through their project is kind of dull and boring, and sometimes it’s that kid that that’s, maybe a little bit slower at getting his code working a little bit slower kind of, like maybe sometimes the last one to finally find his mistake and get the code working.
But it’s that kid that just blows me away with the creativity of the and I’ll tell you guys being. I was a. I was an engineer for 30 years in electrical engineering and the successful electrical engineer is not the guy necessarily that can code the best. But the guy that can make things appealing and user friendly and attractive to a user, something that that draws the users from they want to sit and play with it. And so I just really want you to not lose that creative aspect of things. And so I just encourage you to think creatively and how you can use the servo as part of a project that would do something neat and cool, and so we’ll have three or four days to work on this, probably the rest of the week. And then I will come around and we will. We will be looking at just how creative you can create a project based on Loosli something associated with the servo and the in the potentiometer. Again, if you’re not using the servo, make sure that you aren’t trying to drive one from the Arduino unless you’re sure the Arduino can give you the juice. You need to do that: okay, palma quarter! This was lesson number 16. I believe hope you will tune in shortly for lesson number 17.
Paul McWhorter arduino lesson 15 Video
Paul McWhorter arduino lesson 15 news
Posted on Friday August 19, 2016Books You Should Read: Basic Electronics Hackaday … Continue Reading »
Paul McWhorter arduino lesson 15 Social