And now here is the solution to avoid using delays, so you have seen how to use the minis and macros
function to get the time, you have also discovered how to compute the duration of connection with those
functions.
And now we are going to use all of this knowledge in order to reproduce the behavior we had with delay,
but without using delay.
So for this, I'm going to make a simple test.
I want to print something every 500 musician in the loop function so they'll begin.
OK.
And in the function, I'm going to do that to bring in a load, and so we have to delay 500, that would
be with the delay function.
OK, so gate to basic program, we bring Telo every five hundred milliseconds and now let's remove the
delay and let's see how we can do without it.
So of course if we just applaud the program like this, we don't print hello every 500 milliseconds.
We print at full speed.
So the full speed of the original execution.
OK, so to be able to print hello every five hundred milliseconds, we're going to need to check the
duration between the current time here in the Loop and the last time.
So the previous time we have printed on the same monitor.
And if the duration so the difference between the current time and the last time we have done the action
is greater than 500 milliseconds, then we can execute the action.
So we would need to store the previous time.
I'm going to create a global viable here.
And signed long and name its previous time, not just previous time, OK, but previous time still rates.
So we know it's the previous time we have used the sale print function that we are going to use here
and this.
So I could just initialize it to zero to merely.
So I'm just going to use Milli's because when the program starts, this would be zero.
So this would just be executed.
So this value is declared and defined with this value.
So now in the loop function, what I'm going to do is I'm going to unsigned long.
Crunch time.
All I can right now.
Equal to miss.
So every time we enter the loop, we check what is the current time?
And then I'm going to do if the condition, if the time No.
Minus the previous time the sale printed, something here is greater than we want five hundred, I'm
writing 500, then I execute this book of code and I put the sale inside the block of code.
OK, so quick recap of what happened here, we create a viable to stop the previous time we have printed
on the sale.
OK, we initiate easy to zero at the beginning, of course, and then you loop it going in a loop.
We take the time now, which is the time we meet.
OK, so every time we go in the loop, we check what is occurring and then we make the difference between
the current time, time now and the previous time we have printed on the same.
OK, so if you're running from the last lesson we do this, you are just creating a duration.
OK, time now.
Minus time in the past is the duration.
This duration must be greater than five hundred.
So we execute the action.
OK, so every time we're going to look at said the beginning this would be one and then two, three,
four, five, etc. So this will not be greater than five hundred.
And then we don't execute this action.
So not when the minutes reaches five hundred for the first time, this would be greater than 500.
So maybe more.
Five hundred and one, because it's really greater than that.
When this is equal to five hundred and one or more, we enter the.
But then we need to do something because the previous time, so you'll print will always be zero here
because we have initialize it to zero.
So this condition after the minutes here reaches five hundred and one will always be true.
And so we will keep printing this very, very fast.
So what we need to do is that whenever we enter this block of code so we can do an action.
And we said the previous time.
The current.
So if the current time is five hundred, about 500, then we are going to enter this and then the previous
time will become five hundred, which means that not when we go in the loop again, but let's say now
the time is five hundred and ten.
The previous time would be five hundred, which means that this is not greater than five hundred.
OK, but we are only going to go back to the if when the time now is equal to 1000 or more.
But let's actually try this.
I'm going to upload old, let's say, without delay.
And let's use the same lingo here, and you would see hello, hello, printed every five hundred milliseconds.
Great.
So it works and we haven't used any day here, as you can see.
So now, of course, this is a little bit more code, but what is the advantage here?
The advantage is that you never block the instructions.
OK, this will run at full speed.
OK, this takes very little time to do.
This is just a condition.
And this is just you set value inside of value.
So this will take almost no time, which means that if you have a second action, you can do this.
The second action in the loop function here using a similar structure and thus you can do many things
at the same time.
OK, you don't block the program anymore with the delay function.
All right.
And let's come back to this.
I'm going just to improve a little bit.
So here I have you.
Five hundred, which is upgraded.
We can do better than this.
We can do.
Let's see.
Unsign long time interval.
So rate right, which is five hundred.
The we use an inside long as well, OK, because everything we compare here, unsane longsword, just
give the same data type and then we are just defining a time interval viable here.
We say Alperin so we know it's for this action.
And so I can just.
Do this here.
OK, this is better if you can avoid hard cutting values inside the education of your code and create
variables instead of constant and know to improve this, I'm going to also just show you something else.
And then let's bring.
Milice here.
So let's see let's see what happens.
So we bring hello, we also the time, so you see five hundred and one, one thousand and two, etc.,
and you can see we are shifting a little bit.
OK, the eight, 10, 11 Segan.
But you can see it.
I'm going to remove the scroll.
You can see here, this is not exactly 14 Siggins, OK?
We have twenty eight additional milliseconds and this is a shift that is going to increase every time.
Why is that?
And this is simply because, well, we executed some action.
The action takes some time to execute, basically.
And when you record time now, of course, there is always a little delay that will happen because you
execute the actions.
So what are we going to do instead of sitting the previous time?
So point to time, know what we can do is simply add the time interval I'm going to do plus equal.
OK, so plus equals simply means that you are going to add this to the variable on the left.
So I simply add the time interval to the previous time.
OK, so whenever we execute the action, I'm going to add plus five hundred, which means that all the
small delays that we have added when actually executed some code will not be taken into account.
But if you don't understand exactly that, let's just see the result and you would see that is better.
Now you can see we have 500, so we still have a small delay for the first one.
But then you can see we already have a good volume.
So we don't have a shift here.
You can see after 13 Segan after 14 seconds, OK, we have 14 and then zero zero one.
So you can see there is exactly 500 milliseconds before each instruction.
All right.
So to recap, first, you create an unsigned, long previous time for the action.
You initialize to zero images and you create a time interval.
OK, the timetable will be basically what you want to put in your delay here.
Five hundred milliseconds, then in the loop function, you get the time.
Time now with Milice.
And then for each action you want to do, you check the difference between the time now and the previous
time you did the action.
This gives you a duration.
You check if the direction is greater than the time interval, you have said, if, yes, you do direction
and then you add the time interval to the previous time, which means that you are only going to go
in if again after the time interval has passed another time.
All right.
So this concept is not the easiest concept to understand, but with the following hands on lessons and
with the following practice exercises, you would be able to get a better grasp and then, of course,
come back to this lesson to better understand.