How to Use Millis to Master Arduino Multi-tasking

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we are going to expand upon Arduino multitasking from our last lesson where we looked at hardware and software interrupts and today we are going to be expanding beyond that to look at a very powerful function which is called the Millis function now multitasking or I should say seemingly multitasking with your Arduino because the Arduino does read one line at a time going down your your sketch so it's Works seemingly you know simultaneously but for the average viewer if you're trying to interact with a project it's good enough it's actually pretty convincing right so with the Millis you're probably at a point if you're watching this where you've already built a couple projects you've gotten LEDs to Blink you have gotten servos to sweep and you control it with the potentiometer the problem is have you ever tried to put the blank sketch and the servo sweeps sketch together it doesn't work so good does it right you know the light will blink and then it'll stop and then the servo will sweep and then it'll stop because it can only do one of these things at a time you know because of the dreaded delay function so we need the Arduino to you know multitask as best as it can and so Millis will definitely open the door to far more complex projects what is Millis to begin with right so your internal Arduino internally has a hardware clock and this thing counts in milliseconds the second you power up your Arduino or you upload your sketch it starts counting whether you use it or not up until now you've probably never used it you know or if you used it here and there like the blink without delay sketch so that's always there so it's like hmm anyways we might as well tap into it right and use it for our projects with Millis how long can it count to is it like forever and you can just like keep your board powered you know all day long not necessarily the with the Millis function it returns an unsigned long number so that means you can keep going continuously for almost 50 days now for most of our projects that's pretty good that's going to like fulfill the needs for most of our projects now if you are a scientist and perhaps you are collecting data over a three month period or six month period well you're going to have to do a couple workarounds and there are workarounds for the Millis function so the best way to kind of understand Millis is just to start using it right so enough talk let's start using the Millis function so here we see that I have a freshy MC fresh fresh sketch here and let's see what this Millis is all about like can we see it I mean let's see what it is so in our setup let's see if we can pull it up on our serial monitor so we are going to cereal dot begin and we'll use Ye Old 9600 for our bod weight bod rate good enough right okay and for our Loop let's see here and you can see that there's a whole bunch of things hooked up to my Arduino here we'll get to that we'll get to that uh we're not going to use any of it right this second let's just see what this serial uh monitor shows us so we are going to um serial dot print line yeah put it like that and what are we going to print we are going to print the Millis function and that's our Millis function right there right so that sounds pretty good to me so let me upload this oh and look at that look closely my Arduino is not plugged in I will be right back people now we can officially upload this thing all right and I don't want to save saving is for you know people that are not confident famous last words right people always save and frequently all right so okay so it looks like it uploaded let's pop open this serial monitor business here whoa and you can already see the numbers going up and up and up and this is counting in milliseconds so looking at this you can see that this number gets pretty big pretty fast all right so if it can count up to 49 days you can only imagine how big this number can get and we're going to talk about unsigned long and exactly you know what it is that that means but you can see I mean this thing is just churning and churning and churning we're like in the 30 000 right now all right it's getting a little ridiculous let's make that go away a little ridiculous all right so we got a lot going on here with this circuit so let's take a look at you know maybe a simplified view of it so with this circuit you can see I have four things going on maybe five if you count the button so we have a red LED we have a blue LED a green led a button and a Servo so the objective today is to try and get all these components doing something different in a seemingly simultaneous manner so let's see if we can do it so let's start off with the easiest let's only look at that red LED and get it to Blink without using delay at all and then we'll tackle the rest we'll just kind of build up from there so we're gonna try and get that red LED to Blink with no delays challenge accepted people let's do it you see me getting rid of all the serial stuff which you know is a big No-No because like knowing me we're probably gonna have to troubleshoot some of this code let's kind of start up here and because we're dealing with color different colored LEDs I'm just going to set up the pins based on the color of the LED so red pin you know so we know that's our red LED pin that I believe is connected to pin 11. we shall find out soon enough all right so we have our red pin anytime you're going to use Millis you already know that you're gonna have to set up a minimum of three variables minimum you're gonna have to set up a variable that reads the current time like what's what's the clock time right now is it 30 000 milliseconds is it 50 000 milliseconds so that's going to have to keep track of the current time and so of course that variable can just read Millis and be like okay Millis where are we at where are we at right so we we're gonna have to clock the current time then we are going to have to find the time stamp of when did this led change States in time you know like say you're trying to Blink your LED every 500 milliseconds so when was the last time it blinked was it at the 500 millisecond mark because when it gets to the Thousand um millisecond mark it knows it needs to Blink again so not only do you need to know the current time you need to know what time the thing happened you know your event it could be a sensor read a blinking LED whatever so those are two and then third you're going to have to specify a period like every X milliseconds that's when you know to change the state so say we want to blink our LED every 500 milliseconds well that's you know we're measuring blocks of 500 milliseconds and we're gonna have to know the current time and a past time that this happened to know if that 500 milliseconds has elapsed all right so but we also need to be able to control the state of the LED it needs to know is the LED off because if it's off well turn on you know and then vice versa so let's get some of that easy stuff out of the way and let's get this led state right and why don't we start with it off look okay so the LED is going to start off off all right so now we need to come up with our current time variable so let's do a unsigned long right and what is this unsigned long now some of you may already be familiar with all this we are definitely I'm just going to create some space down here so I can write notes so far I imagine a lot of you have seen the end the constant which means a constant integer and integer that does not change you'll see oftentimes that pins sometimes are written this way because a pin is constant it's not going to change you know or you can just leave it as int you have probably run across the byte uh perhaps you have one across the float maybe some of you have run across along right so why do you normally see Millis used with unsigned long you know so if you look at the bite a byte can basically house it's a container that can house anything from I say zero to 255. right so 0 to 255 is way too small to use with Millis like you're gonna fire up your Arduino it's gonna be done you know so okay so bite's not a really good Contender I'll get rid of that and we'll get rid of this constant right here because illness is always changing right so float it kind of has to be a decimal and like Millis doesn't Millis counts in whole numbers so that's not a really great candidate all right so that leaves us our int and ant is still too small it just you know it goes from negative to positive but something like this it only counts into the like the 30-ish thousands of milliseconds you're gonna like not even get to a minute in your program you can't even run your program for a minute so probably not a great contender long long can house anywhere from like 2 billion ish to positive to billion-ish yeah billionaires there's a little over 2 billion now this is a contender so that's quite a few days you know it's it's two billion milliseconds that's usable but if you look at this negative situation right here that's just wasted space that's two billion just units that you can't even use because we're always counting up from zero Mills is never going to be negative right so is there a way we can kind of make this work in our favor and that's where the unsigned identifier comes into play because unsigned can only be positive so in essence it takes this negative 2 billion and it puts it in the positive category giving you something that is like four billion ish four billion ish in a little over 4 billion units you know so we're milliseconds in the situation so the unsigned long is really the best variable container or data type container to be messing around with when you're doing that working with Millis so that's why you see unsigned long pretty much exclusively used with Millis all right so back to setting up our variables we need something that's going to measure our current time we'll call it the current Millis what is the current Millis value right and so this Millis well in the beginning of the sketch let's set it to zero because nothing's happened yet you know the clock starts at zero so let's set that to zero that's always going to measure the current time and like I said now we need a variable that's going to measure what was the time stamp that the event happened like change of state of the LED so let's also call this unsigned long because depending on how long you run this program the last time something could have happened could have been like you know 50 000 you know milliseconds was that time stamp so pretty safe to to do that now because we're blinking an LED and we're going to build upon this circuit I'm going to be a little bit descriptive with these so the blink previous Millis like what was the previous time stamp that this thing blinked now the previous time stamp I'm going to start it off as zero because nothing's happened yet you know so so everything's starting off at zero we're zeroing everything out all right so the third variable that I talked about was the period how what's the rate of blink that we want so for this we can be all textbook and call it you know uh unsigned long and we can say blink period That's a good one blink period And what I say uh what did I say half a second that sounds good all right and you can see that I changed this up a little bit I put you know constant because the blink period will never change we want this to Blink every half a second now in more complex sketches maybe you want to change the rate of blank you know so you would take away that const now for a number like 500 in unsigned long a little Overkill I admit I admit but what I have found doing math depending on how complex your math is going to get with these timers I like to kind of match up the different variable types so that way the math is happening with kind of the same variable types and we'll see how that works a little bit later on it seems to be a little more reliable get a lot less wonkiness right all right so I think that's all we need for this and let's do our setup here our pin mood so the pin mode is is going to be our red pin is going to be an output because we're going to be outputting that blink yeah all right so the loop so in the loop the first thing that we're going to do always is what's the current time like what time is it right now so we are going to grab our current Millis and how are we going to get that current time well we're going to look at the clock what is Mill is telling us so whatever time it is right now is going to be passed to this variable so we know what time it is all right now let's start writing our blink sketch now when you're getting into Millis I am going to assume that you are trying to time more than one component because if it's just one component you don't need to use Millis right so if you're trying to time more than one component I actually like to set up each component as its own function it makes the loop a lot smaller it's also easier just to find that component and just change the code in there in that function and it automatically updates in the loop so you will find sketches and it's 50 50. some of the extra functions are at the top above the void setup or sometimes it's at the bottom after the loop you know I like to kind of go in order so we kind of have our Loop set up and in a loop you always want to check that timer so let's write our own function here and we'll call it blink LED I know real you know real creative there you know but heck it's it's descriptive so what do we want to happen in this void blink so we'll start with an if so if the current time and this is where we're going to do a little bit of math nothing too complicated and if the current time and we subtract the blink previous Millis from that if that is greater than or equal to the blink period what happens basically what we're saying here if the current time say it's zero milliseconds because we just started this thing and minus the blink previous which is also zero milliseconds because you know nothing's happened well that is not greater than 500 milliseconds so we want the LED to stay off but say we get to a point where we check the current time and uh oh and we minus zero say the current time is 500 and we take away the previous Millis which here was Zero well that is equal to or greater than the blink period so we want that thing to Blink so let's start writing kind of a more if statements you know all right so if indeed our elapsed time is greater than 500 milliseconds what do we want to happen first thing off the bat you typically want to time stamp that like we are at the 500 millisecond mark this is when the LED changes take note of that and put that in the blink previous Millis variable all right I need to come up with short names for this this is crazy all right so the blink previous Millis is going to equal the current Millis all right so basically it's 500 milliseconds right now so that 500 time stamp it in there so this remembers the last time we blinked this thing was at 500 so when it gets to a thousand it'll be like oh yeah time stamp the 1000 and it'll go time stamping every 500 milliseconds now if this is less than like we haven't really reached the 500 Mark well there's no sense in it like keeping a new time stamp and a new time stamp we want to keep only the time stamp of when this thing blinks so that's why it's in this if statement right here all right so we've time stamped when this action is now about to happen so if the LED States happens to be low like this thing is off well we want to turn it on so we're going to change the LED state to high oh yeah we are all right and I'm putting in too many uh a parentheses all right and or else we want to keep it off so let's keep this led state off all right and then here if you notice we haven't like digital written anything yet so we are going to digital right the red pin by the LED state I'm just like looking around my microphone here it's in my way all right so I think that's that we also have to make sure we have the correct number of bracketries so if I see here my else is matched up my if here is matched up this oh but this one doesn't have a buddy so we have to give it a buddy all right let's give it a buddy all right and to help me kind of find all this stuff I'll just kind of do that all right so we've written what we want the blink and how we want it to work so how do we call this in our Loop now some of you have already done this you're very familiar with this those of you that have not you know created your own functions added them this is how you would do it you know so we are just going to put our blink LED right in the loop so all in all the way this sketch works is we've set up all of our variables right here and let's skip down to the void setup we've set our pin as an output and in the loop the first thing you do always is what time is it what time is it and store it in here and then it's going to go to the blink function and it's going to go like oh okay you know it's it's up here so before we start adding more components I like to check component by component so we can already rule out that this works so let's let's do our little upload no saving oh it's angry about something so let's see here uh declared void all right oh and look I made a mistake you gotta put the little the little uh parentheses see I was putting parentheses over here you know but not where they actually should have gone no saving oh it's angry here again all right so let's see what is going on so current time was not declared in this scope so it could be that I spelled something wrong which is usually oh current time current Millis sometimes I will write what I talk versus what I set above so the current Millis and let's make sure that I didn't do that again so no that's that's all right let's try all right people yeah you want to give it like beady eyes or you want to give it the side eye then it gets scared so look at that our red LED seems to be blinking like at half a second but let's see if this really works why don't we change the blink period to a full second now because this is a variable here it'll automatically update so let's see what like a second looks like no saving it's so slow oh yeah let's just let's make it more action-packed let's bring it back up to 500 milliseconds awesome all right guys we've done it we've blinked an LED with no delays we're gonna build on this and there's no way we can get all this stuff working if we're going to use delays you know at the same time so with the blue let's move on to this blue business what do we want to do with that so the other thing we can do with LEDs and especially if you're making props you want different lighting effects and this applies to LED strips too once you master this Millis you're going to be able to have different animations going so say a prop gun or a small robot you can have kind of a steady blinking I'm on you can have cool fading you know that's kind of like a different lighting effect so why don't we make that blue one fade on and just fade on turn off Fade up turn off right while that red one is blinking oh so we have our red pin now let's do our blue pen and I'm just going to keep our color theme going and I believe that one is connected to pin number 10 and like I said we are going to find out sooner or later all right so for the blue we want to fade it up so what variables are we going to need now for every component you add you can't just be reusing this stuff except the current Millis current time is current time it's applicable to every single component you add but if you're going to have each component doing something at a different rate it needs its own timestamp and it's going to need its own period so this is kind of like where things start to get a little bit long and it's going to need its own State and so what we've done here so far is create what's called a state machine so it knows on off on off according to the time so we're going to create another state machine for our fading and so what is some variables we're going to need well obviously we're going to need brightness you know we're going to have to have a way to control the brightness of the LED so we'll just call it brightness and we're going to start it off at zero we're going to start it off and I'm using byte because remember byte goes from 0 to 255 which is conveniently what the LED pwm pins use for the fading oh which reminds me if you're going to feed an LED make sure it's connected to a pwm pin which pin number 10 is so all right that's very rare then I get it like that you know all right so we're going to start with a brightness of zero but how fast do we want to brighten this meaning by what increment do we want to increment it by five every time the time period is up or by 10 so it's more like a choppy Fade up or do we want a smooth Fade up I think I want a smooth fade so let's do a fade increment of one you know so it just Fades up by one every time so all right so basically you can see that I have my state machine things grouped here I have my pins grouped here and then I start to kind of group these things together and everyone groups in a different a different way there's no right or wrong grouping you know so let's make a fade previous Millis it's pretty long and let's set that to zero two because we're just getting started and we also need a period like how fast we want this thing to fade up and we'll call it the faith period and I kind of like maybe 10. yeah 10. so every 10 milliseconds it's going to fade up by one all the way up to 255 and then it'll cycle back to zero because we are using a byte for this so we have uh let's you know Skip that we don't really need to deal with that anymore let's go in the void setup here where we need to set up our pin mode so let's set up the pin mode for our blue pin and that's also going to be an output and all right so in the loop we're going to have to create another one of these things you know another function so here is the blink one so I'm going to kind of create some space here and create another one so void fade LED right we did blink LED fade LED don't forget to open and close parentheses and let's get started now you're going to see that a lot of this code is pretty much the same because it's how it works it's what time is it now when was the last time something happened subtract them is it time for something to happen and if it is if state statement it away to your heart's content right so that's basically how these things work they get a little bit uh repetitive all right so it's the same thing if the current Millis which is gotten right first thing in the loop function so if the current Millis is less than now the fade previous Millis because we're not blinking the blinking you know is a different function the fade previous Millis and if it is greater or equal to the fade period which is 10 milliseconds well what what do we want to happen okay so in this case we want our fade previous Millis remember when you want to time stamp that right away our fade previous Millis to equal variables you want it to equal the current Millis so you want to make sure that you have that time stamp that something is about to happen so now basically if that happens well it's time to analog right you know let's start fading up because we have exceeded our 10 millisecond situation here so we're going to analog write that blue pin by the brightness right and remember the brightness to start out with is zero so we basically start dark and then from there we're gonna start to increase the brightness so the brightness is going to be by the uh fade increment I remember like the the variables that I use so basically what happens here is if the current time minus the fade previous Millis like the previous time something happened if that exceeded our 10 millisecond period Well we want to go ahead and record okay this is the time that something's going to happen we're going to increment our fading so then we want to analog write that blue pin by the brightness after that happens for the next go around we want to increase that brightness by one so this is where we go brightness we're going to go brightness plus the fade increment now oh don't forget your semicolon if you've never seen that plus equal operator I'm going to create some space down here all it means it's shorthand for basically brightness equals brightness Plus fade increment so that's those two lines are exactly the same the plus equals is just like a shorthand you know so you'll see me use that a lot throughout so we can get rid of that all right looks good I think I think we we got it all so let's check bracketry that's always where it gets us so here's this bracket and here's this bracket okay so let's oh we can't forget to add it to our Loop because then nothing will happen so I think it was called fade LED like that so let's see what happens when we upload this thing it's uploading look at that guys hey it's fading up now if you recall from past lessons where we're learning to fade LEDs Ellie the the cool fade part happens right in the beginning once that voltage you know or that your duty cycle starts to increase it's very difficult for the naked eye to tell and see it getting brighter and brighter and brighter so a brightness of like 170 kind of looks like a brightness of 255 and that's why visually a fading LED kind of always looks like it it hangs bright but it's not it's just the differences are so subtle that our eyes can't see it but you can see that our red is blinking at a rate of half a second independently of our blue LED which is now fading up and then once it reaches 255 it turns off and so it's you know this kind of thing now I spy me a green LED so what can we do with this green LED all right so I have an idea there's a button right next to it right so we have a blinking red a fading blue why don't we make the green like a uh when I push the button it goes on you know type of situation so let's do that yeah yeah so let's see we're gonna have a blink a fade and a top all right so let's go back up here and see and that's why I like everything in order because you remember to add all this stuff so now we have to add a green pin we'll call it green pin and this is going to be number nine I'm thinking it goes down like that and then oh we have a button too so the button pin and that is tell I'm gonna go look alright so the button is number seven all right okay our button pan is pin number seven and our green pin so now we need to create you know again some kind of state machine for our green LED so in order to do that let's do a toggle State you know like we have led State let's do a toggle state so we're going to start that at low toggle State low all right so we have our current Millis everybody uses that but we need some kind of situation for our button so we'll call it button previous Millis yeah why don't we make it longer just because all right and that's also going to be set to zero because when this starts nothing's going to happen all right and we need a button period consigner button period and we want to read the button let's just go like every 100 milliseconds I don't know we can try different things but that sounds good to me now it's going to also have its own function but let's just skip that for now I always like to make sure I always forget to set up pin mode and then I'm sitting there scratching my head why things aren't working so for our green pin it's going to be an output and for our button you see that there is no resistor at all and that's because let's see button pin we are using the internal pull-up resistor input pull up so we'll do that so remember when the button goes low it means that it has been pushed it's like opposite day all right so we have our Loop here which We'll add it once we write it so here we stopped off at fade LED so let's kind of like create some space here and call this um toggle LED this is our toggle LED and let's it's gonna be very familiar to you you know so if the current time and right Millis and not time is a you subtract the button previous Millis from it and if that has exceeded our 100 millisecond check which is the button period I think that's what I call it above quick look button period well what happens well we want to record that time so we're going to say button previous Millis you know snag that time from the current Millis all right so now if we're checking the button if that digital read is of the button pin of the button pin if it is equal to low which remember now it means the button has been pressed what do we want to happen you see I'm don't semicolons everywhere everywhere so we want to toggle state we want to make that equal High right just like we did for our LED so we want our led to be written High then or I should say else we are going to toggle state it all right and then here we actually have to write out to the LED so we're going to digital write the green pin by the toggle State and this mic is totally in my way all right so I'm just going to do a quick bracket check and that one goes with that and that one goes with that okay so we call this toggle LED so I'm going to add that all right guys we're gonna have like a couple things going here so it's pretty cool all right let's let's upload that no saving all right now of course it looks like nothing's happening because I have to go over there and press it let's press this thing okay yes yes you can see how you can apply this to a sensor reading you know perhaps the sensor has to be read every thousand milliseconds and at that time perhaps you want to print to an LCD screen that says hey it's this temperature or this pressure or whatever you know scientific thing that you're doing or perhaps it's something like an interactive robot where it's reading a touch sensor where if somebody touches it on the shoulder it behaves in a certain way or on its head you know like a lot of the the interactive fur Baby Style toys that we see so ah spying lonely Servo there this time this time put him in action so let's add to this a Servo and then we're gonna have like a whole bunch of things going on but you can see you can kind of get the pattern of what's going on here you know so we're gonna try and get the servo working and you can pretty much copy this and add a ton of stuff as much as you want well until you run out of memory you know all right so let's add the servo and the first thing we have to do when adding a Servo is at the library so you can either type it out or we can just include the library here if you kind of like forget what the syntax is all right so we have our library and now it's time to set up pins and all that kind of fun stuff so now we got buttons really the only pin we're worried about here is the servo pin and I forget what the servo pin is connected to I think it's six yes because if the button is seven and the servo six I think I put everything in a row all right so let's create what kind of state machines do we need for our Servo so we definitely need some kind of increment to move that arm so let's call it Servo increment you know I guess since we have fade increment we can have Servo increment and we're going to start this thing at zero and let's do I guess uh an initial Servo position we're going to start that at zero two so actually we wanted to increment by one every round if it increments by zero which is going to sit there so that gives us a more Smooth Move although with these little micro servers they're kind of kind of jerky but if you have like a nice nicer quality Servo uh you know a one degree per round is gonna be nice you know or you can do 10 or 20 whatever it is you want until 180. so I think that's that and we have our well before we get too far let me create my Servo object so let's do Servo I'll just call it Servo because we only have the one Servo so we'll just you know keep it at that and now we can create these things for it so unsigned long Servo previous Millis and we're gonna set that to zero just like everything else and now we need a Servo period so and [Music] how smooth we could do 15 milliseconds that's kind of like a nice smooth fast enough you know type of sweep I think that that's pretty good all right and now I'm going to jump down to the to the setup let's just kind of get that resolved so I don't forget and we have our well we don't necessarily have like pin modes but we do have to attach the servo that's it so Servo dot attach and it's attached to the servo pin all right so I think that's all we have to do for setup we're going to come back to this Loop because now we have to write one of these for our servo so we'll call it all right we'll squish it together all right and I'm gonna just move this up so we're not like watching the screen like this and we get like achy neck all right so as you may have guessed we are going to do the current time to the last time something happened comparison so if the current Millis minus the servo previous Millis if that is greater than or equal to our Servo period I'm trying to remember all these variables uh what do we want to happen well first thing let's time stamp that sucker right so the servo previous Millis is now going to equal whatever current time it is so we know that this is the time that something is about to happen and so now what we want to do is that Servo position so we are going to use our little plus you know plus equal we are going to then add to that the servo increment so you can see here that we started at a position of zero and we're going to add zero plus one because we're incrementing by one so then this value will be one the next time around it'll be two three four you know as that Servo arm does its thing so there's a problem though you know because when we were doing the whole fading with the LED here we were using byte that goes from zero to 255. magically perfect for LEDs but there's no real variable type that goes from zero to 180. so we're gonna have to cap the bottom end and the top end because if we keep incrementing eventually we're going to go 180 181 182 and the servo is going to be like I'm done here I'm done so we need to say that when it gets to 180 well now increment back the other way negative increment if you will until you get to zero then positive increment negative increment so let's put a let's if statement the heck out of it alright so here we're going to say if the servo position is greater than 180. right so if it tries to bypass 180 uh or or the servo position tries to go below zero not allowed and I believe I need to stick to parentheses like that there we go over parenthesizing it all right so what do we want to happen so basically we're going to take that Servo increment and we're just going to reverse it so that's like a quick way of saying okay if the servo position tries to exceed 180 well take that 180 and start minusing the servo increment so it's going to go back by negative one until it tries to go below zero so it's going to take that negative one Servo increment and negative of the negative which makes it a positive and it'll start to positively move so the last thing we have to do which we haven't done yet is to actually write the servo position like we told a lot of things you know issued many orders added but you know now we have to help it carry it out so we're going to write the servo by the servo position I think that should be it let's make sure our bracketry is correct all right so that's this a statement that's that if statement and that's the loop okay or the function the loop is down here so now we have to add our I believe it was called Servo sweep right so I think that should be it let's try this thing and I'm gonna tell you that that is my that is my demonstration Servo so it's a little oh let's see here oh we have some bracketry problems right here it's a semicolon see I was sprinkling it everywhere except in all the right places all right everybody give it side there we go that's when it accepts it gets all scared the surface working mostly you know this is my demo Servo it's been pretty abused so it can look a little jerky but it's doing its thing it's like I want to show you all how Millis works so I'm gonna go over there oh but there's something off do you guys see that look at that blue LED it's like blinking it's not really like fading so what happened something happened that when we added this Servo code it's making the blue wonky now is it something in our code it totally could be but I did this because I wanted to remind you of something that I've run into before anytime you use the servo Library sometimes it can affect certain pwm pins and the first one that I find affected is pin number 10 and look we are using pin number 10. now this is different per per board so the first thing to do is try and move that PIN to something else so I'm going to pick another pwm pin because of course we want to fade that blue LED let's shuttle it to pin number five pin number five is a pwm pin and you really shouldn't be doing this you always want to like turn off your microcontroller anytime you are changing up connections it's always good practice and you'll see me not do it all the time you know I try and remind at least you guys at least somebody should be doing this stuff right right all right so I've hopped it to pin number five I'm just kind of like pushing everything down to make sure and I'm going to plug it back in now of course it's going to make sense that that blue is not working because we have to change the pin in the code so let's do that all right pin five and let's see if this resolves the issue if not then we're gonna have to start looking more deeply into the code yeah yeah there it goes so it was that pin so just remember anytime you add the servo library or any library that kind of software wise controls pwm you may want to like try different pins or read the documentation of how this Library works because it will usually tell you right off the bat like hey you know there's some conflicts here so let's oh we're not done our testing we have to make sure that that green lights up all right guys so we have like four things going on no three things four things going on exactly it's so many things I can't even count them so yes we have our red blinking LED at a rate of half a second we have our blue LED that's fading up at a rate of 10 microseconds and then we have our green LED that turns on and off with our toggle because it's constantly reading that every 100 milliseconds because think of like if you had like a giant you know program and your read button is only at the top well while it's carrying out all this stuff you know it can't do those two things at the same time now this is it like textbook doing it at the same time no but it's close really close you know enough to make really awesome functional projects and then we have our Servo which is like sweeping independently and this is really cool for robotic arms we have made Halloween spiders where the legs have to move independently from a sound clip being played an emotion sensor that senses uh people coming by which then activates a Servo to shoot Silly String at it so we got all these things working and you can't do it without Millis this is your intro to the Millis but by now if you were watching this video I'm gonna suspect it's because you kind of reach the limb submit of what you can do and you want to make your projects that much so yes I would love to hear about your projects post below or better yet I invite you to join our community we have live lessons you can watch this lesson live where you can talk to me in real time also say you catch it later or you have a question afterwards you can always Post in the community I'm always there we can dissect your projects together so link for that is below also I always think of more examples or something I should have covered and I'm like man I can't believe I didn't talk about that so check the tutorial on the website that includes this video it includes all of this code that we did and even more code as I think of it because I'm always like updating these things so guys I had a blast I hope to see you in the community or in the comments below away people I want to see some melissified projects from you see ya
Info
Channel: Rachel De Barros
Views: 49,515
Rating: undefined out of 5
Keywords:
Id: _WSDLt24XQ0
Channel Id: undefined
Length: 50min 16sec (3016 seconds)
Published: Tue Apr 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.