In the previous lecture, we implemented the task schedule so that we don't need to use any delay functions
at all in our schedule, and that, as I said earlier, improves the capabilities of our inspectorate
to sketch its efficiency.
It also allows us to implement new functionalities that would be fairly hard and complicated to do without
a task scheduler.
What we'll do in this lecture is to implement a safety feature that involves getting the pump automatically
turned off after five seconds of operation.
And as you'll see in a moment, this is something very easy to do with the task scheduler.
And at the same time, we will edit.
I'm going to do a bit of refactoring and improve the organization of the sketch.
And in particular, what I'm going to do is to make the voltage reporting functionality independent
of the soil humidity reporting.
Essentially, I'm going to put report for that use to be executed in its own task, in its own schedule.
So to do that, I have implemented the changes in version five of the sketch.
Let's have a look at it in the header of the sketch, here is the soil humidity core back and its task
and I have added another two tasks and callbacks, the pump of Cool Back, which will ensure that the
pump is turned off five seconds after it starts.
And then also have to report to both the Chiefs quarterback, which will be independently reporting
voltages every two and a half seconds, while humidity will be reported every once and again independently.
And we still have the single runner.
A single runner can control multiple tasks.
So these are our quarterbacks then inside.
The set up function.
We initialize the runner, then we add the three tasks and we enable two of them, so I have enabled
here the soil humidity task in the report voltages task, which I want these tasks to be enabled always.
And therefore, I don't have to worry about turning them on and off.
But the task scheduler gives me the ability to programmatically enable or disable a task.
And when I enable a task, it's time I essentially start counting and when I disable it, it does not
for the pump.
It does not make sense to enable that at the beginning or in the setup of our sketch because I only
wanted to count down if the pump is turned on.
So that's when I will be enabling it.
So we'll have a quick look at that next.
Let's see, what else do we have here in terms of changes?
There's the loop in inside the loop.
We've got the execute function of the runner called every time we go through it.
We no longer report the humidity inside the loop in this no more, no longer delay.
Here's the report.
Humidity core back, which you saw in the previous lecture, that's where we implemented it.
And now you see that at the bottom of this function commented out the report voltages call to the public
function because this is now happening by the scheduler.
So let's put it out next.
Now, here's the thing that I mentioned earlier about the pump control in control.
The function, the code is exactly the same, except for line one hundred and forty six in line one
hundred and forty six.
I call the enable delayed function of the turn pump off task and a part of the value of 5000 the 5000
milliseconds, which means that this function will count down to 5000 and then it's going to call each
callback function or the tasks callback function, the turn off pump task callback function as we saw
at the top.
Is this pump of.
So that is implemented right here and as you can see, what happens in that is to turn off the pump
by sending a low signal to the pump pin and then we call the disable function of the task to turn it
off and then it's going to be turned on again.
Some point later when the pump is turned on again, and that's where the 5000 millisecond counter restarts.
Another callback he can see right here is the report, which is called Back.
This is called right now every two and a half seconds, I believe.
Let's see.
You know, every two and a half seconds, we call the report voltages to.
Which then will just carry on taking measurements and then publishing them the code in this callback
function is identical to the function called report voltages in previous versions of this sketch, and
that's about it.
So we've got a new function which will automatically turn off the pump when it's on.
After five seconds of spinning, of the motor spinning and then of refactored are the part of the code
so that the report voltages function happens autonomously from the soil humidity function in the last
thing to do to upload the sketch and make sure that it is still working and that we haven't broken anything.
OK, so the new sketches uploaded and turn on the power supply to do a bit of testing.
So you've got the threshold at five hundred and twenty and you've got the soil humidity from the potential
at 465 comi.
Turn that up a bit 540.
And that works.
And the pump stopped automatically perfect, then go down again and trigger the pump one more time.
Perfect, it's great.
So the author of.
Function is working.
And everything else in my dashboard is working as well.
OK?
Except for the manual pump control, you can see the manual pump control does instruct the pump to turn
on, but then the MCU itself, the switch that is running on the is pathetic to detect that the threshold
for the humidity is below what I've said and it will turn off the pump automatically.
All right.
So I think that is acceptable.
Of course, you can get your ISP heritage to allow you to override the pump state with the switch,
here is something that you can do if you want, but I'm not going to bother with it at this point.