So let's begin by replacing the daily function with the SHIGEMATSU, what you're looking at is version
three of the sketch.
This delay is a blocking execution of anything on the security for one second and obviously is very
inefficient.
So I'm going to replace it with.
Version for.
Of our sketch, which then introduces the task scheduler, then show the necessary modifications to
use a as a scheduler in place of delay, if you want to learn more about the tasks because you have
a look at this GitHub location, it's this page here which contains lots of examples and of course,
an introduction to the various bits and pieces of the task scheduler and how to use it.
It's actually an excellent and very high quality library that I have used in a couple of other courses,
especially in E.S.P 32 for busy people and be to unleash the Fuster, the desks because you casula extensively
in these courses and it's very reliable, very efficient and very configurable as well.
So check the documentation out.
Now, as far as the necessary changes are concerned, that you see is very straightforward.
First of all, we need to include the tasks cédula here.
First of all, you will need to import the task scheduler and you can do it using the library manager.
Just search for a task scheduler.
It's this one here by utterly Pankow amusing version three point one point six to install the latest
available version and then include the original head of file at the head of your sketch.
Then what we do here is to declare a callback.
I mark any function that I'll be using as a callback from the scheduler with the letters C B at the
end of the name of the function.
So this function here is implemented further down in the sketch.
I'm going to jump right there to show you and then go back up in the header where we said.
They released a report, so your humidity go back and so your humidity core back, you may remember
it from previous versions.
This was formally known as reports.
So your humidity could have just left it as this without renaming it.
But I find that by putting this at the end of the function name, it's easy to recognize that this function
is being called back by the scheduler inside the function.
There's nothing changed.
It's exactly the same as in version three.
All I've done is to change the name because now it is not called by the loop.
As you can see, this is what it used to be like in version three, the loop would call this function
and then the various functions inside the security function would be triggered.
But now, of course, in our because this function is being called by the scheduler.
Let's go back up the top in line eight on creating a task, and this is its name, which is another
reason why I added Sibi in the name of report so that the original function name is now used as a task
name and every one second.
This task will be called by the scheduler and it's going to trigger the reports.
So the humidity callback function.
Then I create a runner in line number nine in this row now, especially what keeps track of what to
do.
And when I jump down to this set up function, so in the search function in line 66, we initialize
the runner and then we add a task to it.
So we add the task of the report.
So your humidity, which contains a callback that we want to trigger every one second, and then we'll
enable this task so that it's especially the clock starts ticking.
No changes here and then insert we call the execute function off the runner every time we go through
the loop so that it can update its internal clock to know when to trigger the callback function.
And that's about it.
Every one second the callback function is called and the code inside it is executed.
So I'm going to now upload this sketch to my especially two and make sure that our functionality remains
just more efficient this time.
OK, I'm going to turn on the power supply.
But to make sure that everything is still working and here's the dashboard, the right.
So let's let's see.
What we have is a threshold is five hundred and twenty.
And the soil humidity rule is now four full.
So let's move that up a bit.
And that works.
Right.
So as far as functionality is concerned, nothing has changed.
Everything still works as it was with version three of the editorships sketches.
Just now we're using the scheduler and things are running a lot better, a lot more efficient.
So let's continue with our changes now, taking advantage of the scheduler in the next lecture.
I'm going to do a bit of refactoring.
For example, you can see here that I'm calling the report that year as part of the report Community
Call Back.
But what I want to do is to separate the voltage reporting into its own task that can run independently
of the soil humidity task.
And also, what I want to do is to include a safety timer for the motor so that it automatically cuts
off five seconds after it begins to prevent flooding the terrorism, which I have done.
And that's why I decided to use the cutoff time to prevent my terrorism from running again first thing
in the next election to implement those changes.