#BB6 Arduino StatešŸ˜²Machine - Part 2: Implementation šŸ„³šŸ„‚

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome to my series of short videos in which we discuss how the arduino interacts with various electronic components yes in less than 30 minutes we'll talk about how we can move your arduino to a state machine hints tips tricks and traps and welcome to the second part of moving your arduino to a proper state machine now you hopefully have watched the first part there we are look up in the corner of the screen you'll see the first part link yeah if you haven't watched it you really should do is you won't make a lot of sense of this part i'm just saying yeah so watch that one first it's only about 20 minutes and then come back to this one i want to give a big shout out to pcb way pcb prototype the easy way now we're all familiar with their special pcbs five dollars for 10 pieces but you can also have flex pcbs advanced pcbs and of course you can order custom parts let's have a look at the cnc and 3d printing options they have so it's 3d printing first you upload your cad files as you would do normally for a pcb but then you select your materials and submit a quote request cnc machining is pretty similar in the way you submit your files upload your cad files select one of these many many different types of material you can use and of course there are also 27 different options for the surface finishing just look at a few of those there from anodized brush bead blast spray painting and there's more under that list as well and finally there's sheet metal laser cutting and bending so let's have a look at that once again you upload your cad files select the products you want to make it from and specify a few of the parameters here about whether you want threads for example and you can always submit your request for a quote and it's about seven to nine business days to get it done okay that's pcb way excellent service good quality why don't you check them out now oh you're still here okay now last time we left the state machine as it was then in this multi-tasking situation here where we had a red and a green led running independent functions and not interfering with each other and that is important for the next stage where we actually use proper known and recorded states yes now some of you mentioned that there's a switch on here with a yellow led and that's what we're going to be using so i'm going to disconnect the green and red lines for the minute because they're sort of a distraction we'll just push those to one side and we'll concentrate on what the yellow led does so we've disconnected those two flashing leds so it doesn't distract us and we're going to concentrate on the yellow led plus the switch and we're going to model a real world state machine on this arduino now it's going to be slightly simplified but it gets the point across so what real world example can we use before we think about that i think we need a tiny little bit of theory of about what a state machine is and why we need to know about it and the little drawings that you can do it's useful when we come to actually program this believe me so what what is a state machine then it sounds great can i drop it into the conversation that the next party i go to for example now if you're going to drop these terms into the conversation at your next soiree or party you need to get them right so we say state machine but what we actually mean is finite state machine in other words there are a limited number of states that the thing you're trying to model in your computer can be at right and that applies to anything and really you me the world most things are in finite states although there may be a huge number of them that are still limited to how many states they can be in and they've got to be for the computer to cope yeah so first of all it's a finite state machine fsm as it shows you on there for short and the diagrams we're going to draw are state diagrams great okay now what does wikipedia say about it wikipedia says a state diagram is used in computer science yeah to describe the behavior of systems oh dear um it is a bit dry isn't it all right that's fine as a declaration of what it is let's move on and just see what else it can tell us state diagrams require that the system described is composed of a finite number of states well we just said that haven't we let's take the absolute simplest example of everyday things that could be in a finite state okay are you prepared for this a light bulb or a light here in my workshop yeah it can either be off or on and the starting point for that light as shown by that squiggle with an arrow is always off that's where we start from that's what that bit indicates in our diagram okay great so my light here has got two states and it's not dimmable so it can't sort of dim in between it's either off or on two states you can't get simpler than that how do you get from one state to the other that is something that happens some input some action required to move it from off to on and that action is we flick a switch yeah that's that's literally it so i flick my switch in the morning the light goes from off to on tada and then when i leave in the evening i flick the switch again would you believe yeah i know it's really difficult stuff this isn't it we flick the switch and it goes back to the off we're back to the starting state aren't we oh yes you can drop this in as well this little diagram is known as a directed graph yeah just a little bit of terminology for you does it help you write better code in your order or you know or esp that of course it doesn't of course it doesn't but if you were studying computer science you really would have to know this stuff but from our point of view as sort of hobbyists and low-level coders this this sort of stuff is enough okay let's think of a better real-world example shall we here we are now a traffic light everybody knows what a traffic light is and as you can see there there are only three states the traffic lights can be in red amber or green okay okay all those people in the uk will be going oh no it's not you can have red and amber together yeah no we're different okay in fact there are quite a lot of countries that do things differently would you believe with traffic lights but we're gonna stick with well uh when i was in germany this is the way i did it when i was in the united states this is the way they did it so the starting point for the traffic light is red that's what that means remember that circle with an arrow we start at red and you're waiting at the traffic light and after a few seconds let's say 15 it goes to green state great all the cars start moving and after 30 seconds it goes to an amber state and then pretty shortly after that five seconds it goes back to red and that's that's it that is another state machine that we we know from everyday life there's nothing complicated but that is the diagram now the example we're going to be using in our arduino then is what happens if you're standing in the lobby of her and hotel and you want to call the elevator yeah lift yeah elevator same thing let's have a look how the state machine for that might look and more particularly the state diagram first of all the starting point is it's idle nothing is happening no parts are moving because nobody's there yet let's assume it's 2 am in the morning nothing's moving everything is idle waiting yeah okay the next date is that a lift has been called then the lift arrives at the floor where so you can get on to it great the doors open fantastic you get in door's shut and off it goes yeah but this is simplified remember so how do you move from one state to the other in this particular example well to get from idle to called we have to physically push a button yeah great after that though there's some sort of delay isn't there between after it being called and when it arrives you don't know how long that's going to be it could be instant because the lift unknown to you is actually waiting behind that closed door so as you push the button it's like there to i don't know a minute later you're still waiting what happens then of course everybody pushes that button 20 times quickly in some bizarre hope that it's going to change the state machine state diagram that we've got here makes no difference once it's cooled it's called it knows what it's doing okay so the doors um are still shut well even though it's arrived so the next state must be the little tiny delay as it settles and make sure it's all safe doors open great and i think that's about it we're done then once the doors are open it goes back into the idle state simple yeah okay oversimplified maybe but we're just proving the point here in arduino speak how we're going to get that diagram into code into your arduino and working at least as well as what i'm going to show you on the board now let's let's see if we can do that it's quick easy and you will have a state machine after this right let's see how the diagram then matches up to what's been coded on this arduino first of all it's idle nothing's happening yeah no lights are lit nothing we're just waiting so the first thing i'm going to do is push that button which of course is this action here push the button it's now been called there's a random delay until it arrives and we wait we wait we wait oh there it is so it's arrived that little bleep tells us it's arrived and the doors opened yeah all very simplistic but it will show you the the four different states in our code right talking a code time to move over to the arduino ide okay i lied it's not the arduino ide this is in fact a different ide platform i o visual studio code basically but this will work just the same of course in the arduino ide okay now i've also got on there the above my head here the serial monitor output from this program let's have a look at that first because i think the code might make more sense if we do it that way and bearing in mind what we just saw for the state diagram what we can see here is that okay once the setup's been done everything is in the idle state yeah then i had to push the button so the elevator was called and it says oh look there's a five second delay it's a delay i've just built into the sketch to emulate what happens in real life yeah if only the elevator did come within five seconds yeah i know so at that point it moves from idle state to called state yes and after five seconds it moves to arrive state then doors open state and then idle state again i know there's just you think hang on surely there's got to be more to the no there's not more to it there is more in the code of course but that is basically what we're looking at okay let's have a look at the code now as i said you must have watched the previous video i did on multitasking which is the first step towards having a proper state machine and indeed there was some state machine-ness of that previous code even though it wasn't explicit there was implicit state machine though so but this time it's going to be very explicit okay but you should watch it there is up in that corner there okay right you're still here let's have a look at the code this time round now the code is just an enhancement on the code that we did last time especially in the first few steps here so what we've done is defined a couple of extra pins for the push button and the beeper just to give us a bit of audible feedback as you do get with the lift of course as the lift arrives you tend to get a bing or something so you know which one has arrived otherwise you stand there blindly looking at a lift don't you and the doors haven't opened because it's the one behind you yeah i know okay now we've got another yellow led yeah fine and there's a couple of little um functions here that are extra to last time one we've got the yellow led function that puts the led on or off and the display state is what you just saw in that debugging window the serial monitor output all it does is display that string to keep it out of the main bit of code so the setup has been modified to count to account for that yellow led but apart from that nothing else so we can just basically shut that one up okay now last time we had a blink red led and a bright green led and they worked independently by the virtue of the fact we had this static unsigned long integer here and this bit of code only ran every so often but mostly it was doing nothing and going straight back to the loop and that's how we got our multitasking function working rather nicely and implicit in this of course we didn't actually state it but the very fact that the millis was the driving factor and the state of the led as it was is also a bit of a state machiney thing to do but not explicitly so let's just close this down and move on to the real state machine right so toggle yellow led if we look in the loop we see we're going to do three things yeah do the red led do the green led and now we're going to do the yellow led but the yellow led is not just a simple flash on and off it's a little bit more sophisticated than that so let's have a look right once again though we do have this unsigned static long here because we are after all turning it on and then off and we're not having any delays in here this has all got to be continuously flowing code yes no halts no no bits of code that say stop everything don't do anything just wait no blocking code at all now the elevator delay we don't know what that is i've just emulated that and said go and get me a random number as to how long the elevator is going to be and anything between i think it's three and seven or something like that but here we come to the first thing in your state machine of interest in our state diagram we've already discovered what the states are so there's no thinking about what we're going to call this now we know what they're going to be called because we drew them out in that state diagram idle called arrived doors open the exact names are not important but it's what the state is describing the state as best you want to do it yes now if you don't like the word idle you just want to be called waiting or something fine it's not going to change the logic of the code is whatever works best for you so we've got this enum if you're not familiar with enums they are constructs that allow a particular variable to have a particular value as defined in here now as you can see if a value of another variable had a value of idle it would be zero if it was called it would be one arrived is two and doors open 3. you can change that but i'm not going to get into that because it's not relevant to what we're trying to do here i haven't just said enum integer name now i've said class why have i used a class well here i've said it's for type safety and code readability yes that's true but what do i mean by that well if you declare an integer of this type now it can only have one of these four states it the compiler will not let you set it to another state if by some fluke in your program you wanted to set that to five the compiler would go you can't do it what do you mean what do you mean five five is not relevant i've only got between zero and three here so choose one of those that's what we're using a class you'll see the construct in a minute here is our elevator state this is the thing that determines what's happening in our program and as you can see it's a static elevator state the thing we've defined here yeah so this is what the type is we're calling it ker state you can call it elevator state or lift state or whatever and the first state that it's set to is elevator state that's the name here colon colon idle now this is where the class thing comes into its own it's forcing you to use this kind of construct you must say the name of the class elevator state colon colon and then one of these four names here you can't say current state equals eight it just the compiler goes what's that then that's not what you've told me that particular class can contain so it's type safe and it's readable if you look at that line elevator state current state is equal to elevator state idle i mean it's self-descriptive isn't it very important when we get into code like this now all state machines that i've ever seen either have some kind variant of a switch statement which you might not have used before of course a switch statement in days of your we had go-to depending on oh god that brings back bad memories yes i know basically what it means is we have the current state and a particular value and therefore we're going to jump to a particular piece of code depending on what that value is let's have a look in more detail so what it's doing then is saying okay let's look at the current value of kerstate and if it's this state do something but if it's another state we'll do something else but look at the way this is written because we're using that class enum we're saying well if it's elevator state colon colon idle we don't have to say if it's one or eight or 22 or something else which are all magic numbers and mean nothing to anybody but elevator state idle most definitely means something to you me and the compiler and it's self descriptive it's commenting and describing itself as we write it so great we're saying okay well if that current state is idle elevator status idle we're going to do this bit of code and then right down the bottom you'll see where it says break you must always have a break at the very last sentence of a case statement otherwise what happens well one the compiler normally complains and say you're gonna drop through to the next case statement yeah you don't want that we're saying no we're going to do this bit of code only if the elevator state is idle we're in the idle state now sure everything's dead yep it's two o'clock in the morning nothing's moving no lift has been called anywhere it's just sitting there idle so the what we're looking at here is well has somebody press that push button to call the lift if it's not this if statement goes all the way down to the bottom break end of statement end of switch statement go back to the loop and try again so it's in and out very important remember to keep our whole process running fluidly eventually of course somebody does press that button when they press that button we go right let's let's capture the millies as it is now which you should of course remember from the last video we did on this so that we know how long it's been in the rest of the code since we captured that and then of course having pressed the button the elevator turns up after a random interval of time isn't it which is why i've put this bit here in braces because it's not really part of our state machine code but i had to get the delay somewhere so what i've said is just create a random delay between three and seven seconds inclusive and uh put that into a variable called elevator delay that one there yeah cool but that's not really part of the state machine it's just how we're going to emulate this delay and then we say well having pushed that button and got the delay the state has changed now we're not idle anymore no no the current state now is elevator state called which means we shouldn't be in this bit of code anymore so we're breaking back to the loop yeah it doesn't try other states now it just exits immediately but the state now is that we're in a called state okay so it's gone back to the loop the loop has called the red and green led to flash and now it's back in here the switch statement says right what's the current state and it's called so it skips the first case statement because that was not valid and it comes to this one here it says right the case elevator state called is true so this is the bit of code now i want to do so we're going to display calls date to our debugging monitor we can then going to turn the light on the one that's normally on part the push button to say yep you've called the lift great and then we need a certain delay don't we we go well if the delay now is greater than the elevator delay do something else but of course half the time it's not going to be is it so we skip this break go back to the loop so basically we're still in this called state the state hasn't changed but we're waiting we're waiting for this statement here to become true has the delay for the lift that time has it passed well eventually it says yes there's been four seconds delay and that now is true the elevated delay was four seconds and four seconds has passed so what do we do well we move it to the next state because that's the only thing that was preventing it moving the delay the delay has happened so now we say the elevator state is now arrived break out back to the loop so the loop once again goes through its little routines of flashing the red led flashing the green led back into this routine looking at the switch statement going right what's the current state and it is arrived so this is the bit of code we do so it says arrived we display that arrive state on the debugging monitor we then say beep to alert the user that the elevator has arrived we then set another timer here to say how long are we going to beep that for how long does the beep happen we can't do a delay we can't say start the beeper going and then just wait for half a second quarter of a second one second whatever it is we can't sit here and wait that's that's a no-no there's no delays here no blocking code so having started the beeper beeping at this point here we take a snapshot of the millies again into beep millies and say great the next day doors are opening now the the bells started to bing so it's just gone be the doors are gonna open so we set the state to doors open break which means we exit all this back to the loop red and green leds flash back into this bit of code for the yellow one what's the current state well doors are open what do we do now right so doors are open what's happened well we display the fact that doors are open on the debugging monitor we extinguish the led straight away because that's been on now for x seconds hasn't it and we go well is it time to turn the beeper off yet and we look at this statement here and go well has it been 100 milliseconds since that beeper started beeping and if it's not what we just continue through break go back to the loop yeah i know so eventually it comes into here and having come into this doors open state again and again and again and again after 100 milliseconds time of having come in here this statement will eventually be true and it goes huh right yes i've been that's been beeping for 100 milliseconds one tenth of a second great let's turn it off and now we're setting the elevator state back to idle all right i know that the doors are now effectively open that wasn't quite the state it was when we first started i should have said doors closed and off it goes before we go back to idle but i'm guessing the boredom fracture factor could easily creep in here so we're just going to mull over that a little bit and say fine at that point we're going back to the idle state because nobody's pressed anything yeah so back to idle which means of course the the next time it comes into this routine it's going to go right to the very top and go has anybody pushed the button to call me yet easy now the switch statement is a bit funny because right at the bottom it goes default so you've checked for various cases effectively checking for is the current state equal to zero one two or three and if it's not any of those it must be something else so default and it says here we're going to print something out to the debug monitors again the default switch case is reached it's an error well not only is an error it means you can pilot i didn't compile the code correctly so the chances of that happening are slim to none yeah let's face it you can't get this the current state of the elevator anything other than zero one two or three because that's what we've defined idle called waiting doors open that's all you've got so if any this ever comes up well your your arduino or whatever processor you're using has probably been hit by some cosmic ray and jumped into something else or there's been some weirdness going on because this can never ever ever be true but you have to put it in the compiler will insist that you put a default statement at the bottom and let's face it it's good practice to do it anyway um certainly if you were going to test this you'd try and test it to make this happen but you won't be able to fine okay is that it then what you expecting more were you hmm so if you think you've followed this and seen that diagram then you go yeah i've got the idea that there's four states for this particular elevator or whatever it is you're trying to control and we've moved from one state to another by some action or you know it could be a delay that is still an action or somebody actually pressing a button or something happening that's great i mean let's just press it again make sure this is all still working press the button it's cool the lights on eventually the light will go off and we should get a beep there we are beep light off back to idle state it happens so quickly in real life doesn't it in computer programs but that's exactly what it's doing this this thing up here now i said that the loop is calling the red and green flashy thing as well as all this okay let me put those wires back and uh we'll see how well it's it's worked yeah so there we are the red and green leds are still flashing at their own independent rates and does this all still work let's have a look press the button we'll wait for the lift to arrive eventually the lift does arrive and the light goes out the beep happens everything happens all simultaneously which we know is of course not the case but it's close enough for us humans to say yeah it's all happening at the same time so and although everything i've shown you on here is all part of one big state machine the bit that we're really concentrating on this one is the calling of the elevator and the yellow led that's the bit where we explicitly state this is a state machine cool yeah now if you can do it one if you can do this up here for a simple little elevator that doesn't actually exist you could do it for any number of things whether it's um you know a web radio or a smart heater controller or anything else once you've got the diagram in your head and it well ideally not in your head written down so you understand the states that you can go to that is how you get to a state machine and your code can only emulate what you've got written up there if you if you end up writing stuff that isn't part of that diagram you're going in a minute what's going on here why am i breaking the diagram and adding extra bits in is it because i didn't cater for a particular state up here or perhaps a particular action between one state and another that's the thing to avoid it's the diagram that really tells you whether you're doing it right and the code simply implements that don't start adding stuff into the code that doesn't exist in the diagram because otherwise the two will never match and you'll wonder what's going on as an introduction to moving to a state machine i think this is okay yeah i mean you can take the code all the code will be in my github and download that and see if you can follow it through i made it as simple as i can but no simpler i know yes i do follow the the words of albert einstein yes and i think we're done really if you've got any comments and queries do put them down there comments are good comments are good as are likes apparently youtube like likes and it likes comments so great i'd be happy if you did both or either of those and uh yeah we'll see where we go next in our coding adventures on the arduino see you in the next video i hope you're finding these videos useful and interesting there are plenty more videos to choose and a couple are shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching
Info
Channel: Ralph S Bacon
Views: 26,633
Rating: undefined out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: TH__MdQD_GA
Channel Id: undefined
Length: 29min 21sec (1761 seconds)
Published: Fri Apr 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.