3 Ways to Use Acceleration in an Arduino Sketch

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I hope you're well thank you for joining me in this tutorial we're going to talk about using acceleration in your Arduino sketches to add some color to the motions and movements of whatever it is you might be doing so you might have a like 10 LEDs lined up that's the example we're going to use tonight and you might want them to blink let's say from right to left and you know it's easy enough to get them to blink at a constant rate but what if you wanted to see a little bit of acceleration so maybe they they slowly start to blink and then speed up towards the end or vice-versa they quickly jump off the off the line and then slow down so that's we're going to look at that type of acceleration and then you know other applications of acceleration some obvious ones are if you're using a servo and you want a quick movement you know to slow down and you want to kind of smooth some of those movements out to make them look more natural more realistic in fact the reason that I'm doing this video is because one of the gentlemen in the course he does he builds Christmas villages which are like miniature animatronic models and he's interested in using acceleration and stuff like that to make the animations look more lifelike so that's kind of what we're after so we are going to talk about three relatively easy ways to use acceleration in an Arduino sketch the first one is going to be constant linear acceleration the other is going to be exponential acceleration and then finally we're going to mess around with the square root function just to show another way of adding a little flavor to to some variables so let's go ahead and jump right in for this tutorial if you'd like to follow along you'll need ten light-emitting diodes 10 220 ohm resistors you're going to need one jumper wire you'll need your Arduino board I use the Arduino Uno and that's what I would recommend and you'll also need a breadboard finally you're going to need a sharpie marker to set up the circuit it's relatively simple you're going to start at pin number two on the earth we know and you're going to place a 220 ohm resistor in that pin and then the other end of that 220 ohm resistor simply connected to one of the rails on your breadboard one of the inner rails and then from that resistor you're going to take the long leg of your LED you're going to connect it to that resistor and then you're going to put the short end of the LED in one of the long legs on the breadboard so one of the red or blue generally is the color what you do next then is just go to pin number three cut the next resistor in pin number three attach that to the breadboard on a different line and then attached long leg of the LED there and then the short leg in the in the same rail so we're going to use as a common ground for those LEDs and then you just continue doing that from pin two all the way down to pin eleven and that will give you ten LEDs and then finally what you need to do is take a jumper wire and connect it from ground on the earth we know to that common ground rail that we've been using on the breadboard and that's it you can go ahead and plug here at we know in so here we are in the Arduino IDE and what I'm going to do is go to file examples control arrays I'm going to use this array sketch as the base sketch to modify in order to demonstrate these three different types of acceleration that we're going to be using just for ease of use sake we could say thank you to Dave and thank you to Tom for making this this sketch but for right now and they've got a great tutorial on Arduino but we're just going to just get rid of all these comments right now just to give us more space so let's just quickly talk about how this sketch works and we're gonna have to make some slight modifications so the first the first thing we come to at the top are the variables and we have a timer variable this adjusts a delay time between blanks and then we have an array and what this two array does is it specifies a bunch of different pin numbers so we're going to set this up so it matches our layout so we're using pins two through pins eleven so let's go ahead and change that so we have our array changed again this is LEDs pins 2 through 11 and then pin count is simply the number of pins we're using and we're using 10 pins ok let's go ahead and save that ok now that we have it saved let's go ahead and upload this to the board and take a look at what it looks like ok so what this sketch does as you can see is that it lights the LED from left to right so it lights one LED moves to the next one lights it and then back and forth along the road it's pretty neat but you know one thing about this is it moves that LED moves at a constant speed and what we're gonna try to do is demonstrate ways to make it move so that it moves slowly and then fast and then or it moves fast and then slowly so we'll look at a couple ways to do that so that's pretty much it up here we're not going to have to mess around too much up too much more up here but let's come down to void setup here we can see they're using a for loop to set the mode of the pins as outputs and you know on a quick aside if you're not familiar with arrays or if you're not familiar with for loops it's no big deal you can still probably follow along ok in this video but I welcome you to go to the open source hardware group website and sign up for the free are doing a course wraps for the beginners it is a 13 course module that can get you up to speed on all this stuff ok so here we are in void setup and what I would like to do is set up serial communication so I'm going to use the begin function from the serial library ok now let's come down to the loop how does this thing actually work so here is void loop and here is a for loop the conditions and fight side the for loop it initializes this pin it sets it equal to 0 then it says this pin less than pin count and if your call pin count was 10 and then it iterates this pin so it just adds 1 every time through the loop so what happens when we get inside this code well it does a digital right and it turns an LED on which LEDs that turn on it turns on the LED in the LED pins array that this pin is indexing so if this pin is 0 which would be the case the first time through the loop then LED pins of 0 which would be pintu would turn on and then we delay for a specified time timer and then we do a digital right so again we're indexing this LED pins array with this pin the first time through the loop it's going to be zero and we would turn the LED off so this timer variable right here is what controls the on/off time and what we're gonna do throughout this tutorial is find ways to adjust this dynamically adjust that timer variable throughout the sketch so that it shows acceleration and or deceleration and down here at the bottom this next for loop so this for loop takes the LED from the right side to the left side at least that way we have it set up and then down here this just doesn't in Reverse notice it's pin count minus one so this just takes it from the left to the right does the same thing so these are pretty much mirror loops they just go in opposite directions okay so let's go ahead and talk about the first type of acceleration so the first type of acceleration we're going to talk about is linear acceleration and so what that means is that the speed is getting faster and faster and faster at a fixed rate so if you were to think of like a line on a graph like an X or a Y x-axis graph it would just be a line and in this case it's going to be going from the top left down to the bottom right so it's gonna have a slope to it you know if maybe you remember rise over run that type of thing to start off demonstrating this what we're gonna do is set timer to 1000 so that is one second 1000 milliseconds and then what we're going to do is simply decrement the timer variable every time through the loop so how do we do that so all I've done is I've taken timer and then I've set it equal to itself minus 100 so the first time through the loop well you know we just set timers 1000 so the delay between the LEDs is gonna be one second and then we're gonna subtract 100 from 1000 because timer is a thousand so 1000 minus 100 is equal to 900 so the next time through the loop how long do we delay well we're going to delay 900 milliseconds and then so that would be 900 equal 900 minus 800 she's gonna leave this with rather 100 that would leave us with 800 and then the next time through the loop we're going to delay 800 and then will delay 700 six-hundred all the way down to 100 milliseconds so you can see that delay is going to get less and less and less as we continue across the LEDs from right to left now what I need to do before adjusting this other four loop down here because I'm going to do is take take this code right here I'm gonna add it to this other four loop so it should do the same thing to the other four loop but what I need to do is reset that timer to a thousand so that we can have that same acceleration here so I'll just do that and then I need to do it again at the end of the sketch all right let's go ahead and upload that okay so now if you look at the board you can see as the LED goes from right to left it gets faster faster faster until it gets to that last LED it's sort you know it's not extremely noticeable but you can definitely tell that it accelerates as it gets towards as the close and that's linear acceleration so you can see that's a really easy way to add one form of acceleration to your sketch let's look at another way so the next type of acceleration we're going to talk to is exponential acceleration so before with linear acceleration every time the for loop iterated the delay time got smaller and it got smaller by a set amount well now what we want to do is every time the for loop iterates we still want the timer variable to change but we want that change amount to start this the the amount of change to be small at the beginning but then to increasingly get bigger bigger bigger bigger bigger and then we're going to reverse it it'll start out really big and then it will get smaller smaller smaller smaller smaller faster okay so how do we do that well we're going to use a really handy function it's called the power function so here we are in the void loop and this is the top this is the first for loop and what I'm going to do is I'm going to put I'm going to adjust the timer variable up here I'm gonna say timer is equal to P Oh W that stands for stands for power and power takes two parameters the first parameter is the base so I'm going to put the number 2 in there and that's just rather arbitrary for our purposes it just ends up being a good amount of time and then I'm going to put in a variable and I'm actually going to put in this pin so this pin is the variable that's used to count the iterations for the for loop so we know that this pin is going to increment every time we go through the loop all right so what is what does this mean well all this does it says to to the this pin power so if this pin is 0 it would be 2 to the 0 what's 2 to the 0 well it happens to be 1 any number to the zeroth power is 1 so then the next time so that would be the first time through the loop because this pin is 0 and if if timer is 1 then this delay is gonna be really quick isn't it and then the next time through the loop this pin would be iterated all right so now this pin would be 1 so now what's 2 to the first power well that would be to delay is only gonna be 2 milliseconds man that's quick so it's gonna be on and then back off in 2 milliseconds then we're going to go back through the loop again so you can see this is off to a really quick start I mean that LED is on and off on and off the first 2 now the third time through the loop this pin will be 2 so 2 to the 2nd power that would be 4 so now timer's still only 4 milliseconds man does this thing ever speed up well it does in fact speed up it it exponentially speeds up because when this pin becomes 9 it it takes much more time to get through so we're gonna rush through all these LEDs and then slowly we're going to start adding delay time but you know it'll be fun why don't we take a look I'm just going to add some code so that we can print to the serial monitor so I'm just going to cut and paste some code in here okay so all this code does is this just gonna print this pin because you know I'm curious every time we go through the loop I want to see what this pin is and then it's gonna give us the value of timer and it will display it nicely so let's go ahead and take this code right here and put it inside this other four loop okay again the for loop down there is same same deal just in reverse order okay and then we can we don't need to reset the timer this time so let's go ahead and get rid of that and we can get rid of that comment it out all right so let's upload this so now you can see it's behavior now when the LED is going from the right to the left you can see that it quickly goes and then it starts to slow down and when it goes from left to right it slowly starts and then it speeds up again alright well why why is it doing this well that's pretty straightforward let's think about this pin so here this pin is zero and this pin gets incremented so this pin is always getting bigger bigger bigger bigger which means timer would also get bigger bigger bigger which would increase the delay time so the delay is really short and then it gets bigger now on the other for loop though we start this pin at pin count minus one so pin count was ten the number of LEDs were using minus one so this would be nine so in and then we decrement this pin so here timer is equal to two to the ninth so timer starts out big and then it gets smaller smaller smaller smaller so that's why we see the behavior in the in the LEDs this way while they're doing that so let's go ahead and take a look at the serial monitor okay so here we are and we can see when the LED is going from right to left that's this back line we can see when this pin is zero then timer is equal to one when this pin is one timer is equal to two you know when it's two it's three so forth so on you can see this gradual climb and then all of a sudden it gets to 63 now it's jumping to 127 to 55 and then 511 so it maxes out at 511 but you can see it it rapidly exponentially climbs and that's you know what this power function allows us to do and then the vise is true with the other side of that loop you can see that it starts out large and then it slowly declines and then BAM it's it's a rapid decline here okay well that's using the exponential function all right so the last thing we're gonna demonstrate is using the square root function and this gives kind of a neat behavior it's definitely a form of acceleration and you know I keep saying form of acceleration it's just acceleration just using it in different ways I suppose or different rates of change so the first thing we're going to do for this is we're gonna make timer up at the top where can make it 10,000 so that would be 10 seconds and then down in this loop at the very top I'm gonna say timer is equal to the square root and that's sq RT is the function of timer so what we're gonna do is we're going to take the square root of timer so the square root of 10,000 and we're going to delay for about that amount of time we're gonna go through the loop and when we get back we're going to take the square root of that previous square root so timer would have been the square root of 10,000 now we're going to take the square root of whatever that was and then we're going to continue through so you can see that timer is going to become very small very quick okay so let's go ahead and copy that paste it in down here and then I need to let's see we need to reset timer so I'm going to make that 10,000 and then down here we also need to reset timer and make that 10 set 10 2011 see the way the led is going back and forth really quick is actually pretty darn cool if you ask me I like that a lot so there you have it hopefully that was three relatively easy ways to kind of add a little more flavor to the movement to the delay to the fade whatever it is you might be using with your Arduino again so it's just a linear you know you're just decrementing whatever variable it is on a consistent basis you know 100 every iteration or one half every 100 every time through the loop or whatever there's the exponential acceleration that's where you're just using the exponential function and it's getting exponentially larger over time or you are using the square root function and you know doing something very similar to the exponential all right well hey that's it I hope this was helpful have a great day bye you
Info
Channel: Programming Electronics Academy
Views: 34,562
Rating: 4.9390864 out of 5
Keywords: Arduino, Acceleration (Dimension), linear acceleration, Mathematics (Field Of Study), math and programming, Electronics, LED
Id: hFfGFcGvFH4
Channel Id: undefined
Length: 17min 37sec (1057 seconds)
Published: Mon Jul 21 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.