#253 Accurate Task Scheduler for the Arduino (and STM32, ESP32...)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back now on the workbench today you can see these four digits which are in fact just controlled by a shift register we've covered that in previous videos but that isn't what this is doing here this is a clock and it's accurate within well a few microseconds every day we can tell when it's going to change just by counting down three two one go there we are look spot on how does that work then I'd like to give a big shout out to my sponsor PCB way PCB prototype the easy way let's have a look at their new Superior a plus Advanced PCB here you can find all the details of what their Advanced PCB service consists of and there's a handy comparison table to see the benefits of the advanced PCB service scroll down to the PCB showcase and you can see some examples of what they can do for you just go to their easy online PCB instant quote page and select the advanced PCB option simple if you have advanced requirements for your PCB try PCB way try them now yes indeed now there's my wristwatch that I in fact I synchronize this one do well you know I win the second hand it's just three seconds before the 12 o'clock that's when the animation for this shift register controlled four digit LED starts to work about now there we are there it goes you see um yeah so basically this was brought about because somebody commented in the previous video when we talked about shift registers like oh maybe that bit wise walking of the individual bits could be quite a good animation for a clock or would it get annoying after a while I thought well what you could do there how how do you make it shift down and keep the same value without it looking a bit of a mess so I thought well I'll just have a go so I did and um yeah he was right it was a bit of a mess and it did get a bit annoying but the animation that you just saw there would in fact I believe work for a bedside clock at least but more importantly while I was doing this I thought well we know we already know that trying to make some kind of accurate Loop in an Arduino for clock purposes is just a non-starter it just doesn't work hang on here we go three two one off you go clock there we are look at that see the accurate so I thought what can I do how are there any timer tasks bits of code out that I can quickly snuffle and stuff in here and this is when I came across this task scheduler and it's just that little bit better than most I guess you call it it's obviously a very sophisticated task scheduler but what what no no this isn't like rtos well I suppose it must be a little bit but it's yeah that video there that we did about running free rtos on the Arduino is similar granted but this is simpler and you can just run tasks I mean as many as the Arduino will support anyway and you can have different attributes to those tasks like for example this one you say I want this one to be ultra accurate that is I've said I want those digits to change every minute okay there it goes and if it gives out a sync it says well I'm a little bit behind now so I'll just catch up and make sure it really does get executed once per minute or once per second or whatever whatever interval you've put in all right okay just before we move on to the code of them I've actually changed the code yes I thought well hang on if I've got a task scheduler I suppose I really ought to have more than one task shouldn't I just show you it's working so I've added in this other task down here that flashes once every second well actually it's not true it flashes twice a second does it flashes on then it flashes off so it's on off half a second each time so that's two Hertz yeah two cycles but anyway that that is a separate task to the task that's controlling all the digits at the top just so that we can see that in the code so we're just going to whiz through the code pretty quick I'm going to ignore all the stuff that's talking about the HC 595 at the back there because we've sort of covered that and I was only doing this for a bit of fun just see that um animation as it changes on the minute yeah I think there's probably more you could do if you really wanted to make a a bedside clock with this sort of display I reckon a you can now do it because this is accurate enough and secondly by tweaking those individual digits with the load line you know as you transfer the value from the register to the actual digit so you can see it by doing that independently I think you could have quite a an interesting display for very very little cost so a nano plus those digits you see there would probably work okay the worst thing about this of course is that this huge gap in between because the board that they're on just was never designed to Cascade properly you know to basically butt them up like that also have a tiny little Gap where you can just have a colon in the middle but I guess you could make your own board if you're really that interested but let's have a look at the code now for the task scheduling okay this is the sketch and as I said I might have said before it might be on The Cutting Room floor I was using the Arduino 2.0.0 IDE it's just released it's now out of beta and into proper production however this is now not odd we know this is now my platform IO which looks very similar because the underlying code is the same as for the Arduino 2.0.0 IDE it's very similar because it's all based on Eclipse but we're using platform I as I say just because a I'm more familiar with it and there's a couple of things I want to do so let's whiz through this first of all okay yes I have to have to put this include Arduino at the top because it could be anything in there really Arduino is just one of the many platforms that supports uh charts it forget that we did that last time it's just a little tiny file I've written for the binary implementation of those digits yes I am using lib print F what yes yes that's right it's the it's that video there that we did a little while ago lib printf for Arduino absolutely brilliant I'm using it here because I get sick and tired of having to write multiple serial dot print statements when I just want to print it all on one line so you'll be seeing that in here the use of that okay so do it yeah Do It Go and download it watch that video and just see how brilliant it is right now we've we're defining some options here before we include the task scheduler Library yeah I'll tell you where I where I've got the library from it'll be in the video description down there okay but we need to do all these defines if you're going to do them at all before you include it because there's you know if statements as part of this so I'm saying look I want to tell you about some options I want and we'll come on to those um all this here digits clock pin blank all that's part of the digit implementation that we've already covered there's my new second counter right it's just on pin 13 which means you don't even need an LED because it's the built-in LED on the Arduino Uno board fine um that's when I started up 10 30 right these are the functions that we're going to be using as part of the task scheduler and you've got to forward declare them because if you look at this line here we'll just skip over that one minute we're saying a lot I've won a task and I'm calling calling it clock task and I want it to run exactly every 60 000 milliseconds in other words every 60 seconds right so every minute it'll it'll fire I want it to run forever and this is the task that I want you to run this is the Callback so at the end of that period I want you to fire off this particular function and he goes what what function what what I don't know anything about that function that's why we have to declare it up here first okay now whether the Arduino IDE no the Arduino IDE doesn't allow that actually normally the Arduino IDE is very forgiving about four declarations about it which is through your program finds them all declares them all up front behind the scenes for you and then compile so you don't get any of this I don't know what you're talking about messages but in this particular instance we do need the actual task function that you want to run declared like this that's just exactly the same as whatever function you've actually written without the body okay if you're hearing a slight tinkling Bell that's the cat collar yeah that one she's scratching right so this is the second task I've got one called seconds task and I wanted to run as I say twice a seconds every 500 milliseconds so it's firing twice a second but wants to turn off and wants to turn on so we get the turn on of the LED every second okay and it's exactly the same as the first one I wanted to run Rubber and that's the name of it and I've had to declare that up here fine set up okay all these pins are output pins from the HC 595 from last time plus the LED one yeah we know all that fine okay this is the interesting bit then so we're saying now look my clock task and the seconds task set scheduling option and you can only do the set scheduling option if right at the top we've included this bit here task scheduling options all right if you don't include that there it goes I don't know what you're talking about so bear that in mind so the scheduling option is Task schedule now what does that mean right task schedule means I want this to be accurate so if as part of scheduling that task uh and I'm I'm guessing because I haven't looked at the code and I don't want to either I'm doing this as a consumer of this Library if as part of scheduling this task you're giving those tasks a certain amount of time and you notice that one's starting to drift a little bit you know we haven't done quite enough tasks if we're saying we want to do this every let's say every half a second yeah like the Flasher but there's a drift because the task is a little bit long running maybe it will catch up so it will ensure that it does actually execute the right number of tasks in any given time frame which is why obviously that clock on the workbench is super accurate because it really is ensuring that every single minute on the dot it's firing and it's not losing seconds or whatever now the two other options on here which you can see in the examples one is I don't care just run it pretty much every second or minute or hour or whatever it is you're gonna do and maybe it's close enough it's close enough not worried fine okay and a lot of tasks like that they a lot of tasks do not have to be that accurate just fire it run it and do it get in you know five minutes or something and the other option is to say yes I want it accurate but in a different way I want you to fire the task and then five seconds later fire it again but it's always from when the last task started so it will keep the five seconds going but of course there could be drift overall yeah it's subtle but it really does matter so you really need to look at the examples in the library for this and just see what options there are and things in fact if we if we execute an F12 on there 12 F12 what did I press let's no it's not going to do it is it that's because my keyboard is Switched Off I'll tell you you can't make these things up can you hear I'm in the middle of a video my keyboard is supposed to be running on internal batteries now because I've disconnected it here it is it's a Corsair by the way yeah um it's switched off how can it do that let's switch it back on right it's back on now in Wireless mode right okay so let's um let's have a look at these set scheduling options here we are um okay that's defining and that's not really what I wanted I wanted here this bit so these are all the options you can State as part of that task whether it's time critical for example sleep on idle oh yeah that's the other thing I do if it's scheduling all these tasks but there's nothing to run now say it's scheduled you know three or four tasks and it goes well that one's due in in 50 milliseconds that one's due in a second and so on I've got nothing to run I'll go to sleep saves power on the Arduino and if you're not on battery I'm not sure it really matters a great deal quite frankly I mean you make your own mind open that if you're running a battery powered project absolutely that's when this comes into play doesn't it any even a millisecond of sleep will save you power on a battery believe me in the last 12 months I've made lots of battery powered stuff and those seconds really do add up you know what milliseconds add up and can save you an awful lot of power and your battery will last you know another month or two or six or whatever so yeah that's that's quite interesting and I'll probably have to have a look at that myself actually anyway there's all these tasks that you can see here and I haven't really looked at a lot of these yet but because I wanted to bring this to your attention so you can have a look at that yourself right back to the main code so we've set these scheduling options now we're saying look the schedule that we did in fact so skip over didn't we we declared it up here we just said look there's a schedule right there's no no detail on it it's just a schedule what we're now saying is uh initialize it add the two tasks that we've defined previously yeah and enable the tasks if you do not enable the tasks they will not run okay when I first did this I didn't enable that led flashing task nothing happened I thought what's gone wrong with that anyway Quick Fix done and uh well that's that's that and in the loop the only thing you have to do is say execute the schedule and anything else you want to do in the loop of course yeah so your Loop can consist of whatever code you want plus schedule execute the schedule but of course remember if if your Loop is long running and takes longer than say one millisecond to run which is an awful long time in computer programming terms things might get out of Step which is why those catch up options are so very useful my Loop though as you can see only does the scheduling so it's not surprisingly it keeps things in line right so there's a couple other things here that are just to do with the hc595 which we covered in the previous video this is my callback for the clock task though so when it comes in every minute it says right work out which are the tens which are the units for the hour which are the tens and which of the units for the minutes so we've split them up now into individual values for the digits a little bit debugging here now this is a good example of the prince f look where you can say look you can print all these things in one line so I can say tens and there's a decimal value here units decimal value decimal value decimal value and then tell it what the decimal values were that I want displayed great isn't it I mean it's really really really useful that would take I don't know how many serial dot print standards that would take too many that's for sure and then I'm just sending out the actual individual digits to display the time on here fine right then I'm just saying well okay if the minutes have overflowed Now update the hours and of the hours have gone beyond the end of the day update that as well fine that's not a problem and then my second's toggle so basically what it's saying is every half a second as this is fired by that task scheduler on that led output whatever it isn't now so as a digital read of that particular pin and goes not that's what the exclamation mark is there so not whatever it is now so if it's on now make it off and if it's off make it on pretty easy and straightforward isn't it and avoids an if statement and all that kind of stuff so you can see it's um it's all pretty straightforward this isn't it now I'm going to put all the details of that Library the task scheduler Library in the description below and there'll be stuff up in my GitHub as well do go and have a look at the GitHub because all the sketches will be up there that you can just download and run or whatever and you can take out all the stuff for the HC 595 if you don't want that and just do a display to your debugging window you know the serial monitor yeah that's pretty easy so let's just have a very quick look at the GitHub for that Library not mine that Library there right this is the GitHub where he's put his Library this is uh Anatoly archipenka that's his name there it is Anatolia entrepreneur he's in is in Brooklyn I think his name yeah Brooklyn New York yeah I know that wasn't a New York accent was it so uh that's that's where he's put this and the examples are many and I was gonna say complex that's not the right word they are um they are quite detailed so you need to look at the examples to decide what sort of function you want to carry out and there's there's too many here for me to go into um but they're definitely worth a look at the code yeah he does cover quite a bit though in some of the some of the examples you have to sort of read through it but it's worth it believe me if you're looking for a task scheduler on your Arduino which you should be because it avoids all that spaghetti code isn't it you know one thing calling another calling another and if this then that yeah I know I'm as guilty as everybody else saying you know if a certain number of milliseconds has passed run this and if not do something else or do nothing this way we can avoid all that yeah we can just say Tire scheduler run this every so often the end never have to think about it again you don't even look at your code nothing it's just you can put the code in fact into a different file then can't you and it's out of sight out of mind it just does it yeah I know I shall be exploring this uh quite a bit actually you know because it's it's a nice step up from standard coding using Millis to determine whether you're not going to run a function but less complex than the rtos stuff you know that stuff the rtos which does take a little bit of space in your especially in Arduino be all right on a mega 2560 because there's a lot more space but Arduino it's a little bit a little bit tight isn't it yeah a little bit tight but this hardly takes up any space at all so yeah it's definitely worth having a look at this now Anatoly does say as part of the GitHub this is just scroll down a bit a little bit of um documentation here a summary really about what it can and can't do and yeah and updates and stuff like that yeah and the logic how he uses it the change look great and some user feedback um yeah so it's it's a what would you call it a mature Library it's been around for a little while last updated 10 months ago we're now in October so the beginning of 2022 this was last updated at the time of making this video okay I think I've probably wetted your appetite at least I hope I have in using this task scheduler this is certainly accurate enough to be a clock with this task scheduler so um some options spring to mind there because I've got a project at the moment on the go for some Matrix LEDs you know the max 7219 compact yet 64 in a little block eight by eight well I've got a 0.9 meter length sitting over there that I'm hoping to use as would you believe anyway I'm going to leave this with you comments queries suggestions whatever experiences please please do put them down there I enjoy reading your comments and as you know I answer them all and if you thought this was mildly entertaining or you learned something or whatever yeah give it a hey hey that's the one a like yeah and um don't forget if you like these sort of videos subscribe and ring the bell it's a two-stage process I'm afraid yeah we're not going to get around that so do both great and I'll 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: 20,255
Rating: undefined out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: eoJUlH_rWOE
Channel Id: undefined
Length: 21min 37sec (1297 seconds)
Published: Fri Oct 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.