Let's not use what we've learned in the previous lesson to make some entities blink at different rates.
OK, so I'm going to use two ladies and make them blink at two different frequencies.
First, let's handle the first energy, OK?
So define that is different.
So I'm just going to add a new one.
OK, in let's use spin room 12 in the set up in mode energy one in a Puzzo and went quite fast because
you already know that.
And then within the loop let's make this a living without the delay.
So first thing I'm going to create and sang long previous time energy one that's new one like.
This is initialised to me and then I need to add also time, long time interval, if you were.
Which is to say, we want to make blink every 400, let's say four hundred and seventy millisecond.
OK, so what we do in the loop first we do unsigned, long time, now is minutes.
So we get the time, we get the current time in the loop function.
And then we compare time now with the previous time we have made the break.
And if this is greater than the time interval.
Then we enter this book of code, the way we do is we are going to to your lady, OK?
And just after all, before, it doesn't really matter, you can do previous time.
Plus, equal time interval.
And so we have Destructo.
And then to talk about the idea what we're going to do, because here what we can just do digital,
right, high end digital right now, we need to keep the state of the art.
So I'm going to add a new value here into.
The one eight, which starts at, say, low.
And then I'm going to do this, but we have already done that in a previous activity, if the state.
Is low with two equal sign.
Then we do the one that is equal to high and then else.
One state is equal to low, so if it's law, we said it's too high, if it's high, we said it too low
and then we do digital.
Right.
Any one thing, we want it.
All right.
So what happened here in the loop?
We get the Turrentine.
We change the current time for tango, minus the previous time we linked the energy one.
If this is greater than the interval, the time interval, we go inside that log of code that we execute
the action and then we add a timetable to the previous time.
So we make sure that we don't enter if just after, but we make sure that we are going to wait, at
least for the time interval to enter the if again and to do the action we need to store the red states
outside of this loop function.
OK, because every time we enter the loop function and then if here we are going to toggle the added
state, we need to know what was the previous date.
So we initially that date to lock as an arbitrary choice and then we compare it to know if it's law,
we set it too high, if we set it to low, and then we simply do digital right at any point with the
state that we have just modified.
And let's upload the program.
So it's first verify the.
Listening to it is because I mean to another one, just Attaran.
OK, let's applaud the code.
And you can see the edit is for the first lady is linking, OK, at four hundred and seventy milliseconds
between each link.
So we have just solved the delay problem for the editing.
And now, of course, this just forward is a little bit overkill because if you just need a program
with just one energy, then why would you use this instead of the simple solution with the delay?
Well, because if not, you want to blink and other energy with a different rate, that would be much
easier.
And that's what I'm going to do right now.
So let's add the other one.
Define energy to.
In which has been number 11 here in Moad and.
To begin with output.
And now I'm simply going to create.
Valuables here for the LTTE to.
So I create the previous time we bring the energy to, OK, this is a different one than the energy
one, and the time interval here for one is five hundred seventy nine, the time interval for 80 to
let's say we want to blink at one point three.
Segan, the one thousand three hundred.
All right.
Then you can see this would have been very, very hard to make those to link for the first one with
this frequency, the second one with this frequency, just with the delay function.
So I'm going to also add it to.
Eight.
So for each new action, you are going to add the previous time, you're going to a time interval and
then for this specific case, we need to start also the state.
And in the loop, so first I get the current time I do my first action and then we the same current
time, I can do my signature action.
So I'm going to do time, No.
Minus previous time to think if this is greater than the time interval to link.
Then I'm going to enter this block of code, I'm going to do if anything to eat is low with two people
saying we said it.
We said it's.
Too low.
All right, then, so this is the exact same structure, digital, right, edited to deal with Eddie
to the kids.
So make sure you use the labels that we have created for the energy to.
Make sure you don't make a typo, because if you change the state of energy, one when you are handling
energy too well, this is where you can have a serious problem and then you scratch your head trying
to find what happened.
And this is a common area, OK, when you copy and paste stuff.
OK, so sometimes it's just better to copy and paste the structure and modify the variables.
But here you can see this can be a source of Beryl's.
The previous.
I'm ready to think less equal time interval and need to think.
I'm just going to make sure I have a meet and campaign, they're all done.
So here we simply have actually one.
And here we simply have.
Action to.
We initialize some variables here for the time we assign time into it all the action, one Brustein
time interval for the action to some dates that we need for the application, initialize the edit is,
of course, and then we get the time.
So.
The time as we get the time and then we seem to take the duration for the action one, and then we simply
try to duration for the action to etc., etc..
So, no, let's run.
That could explode.
Could.
And you can see here we have the right energy, which blinks every one hundred and seventy milliseconds
and the yellow energy which blinks every one point three signals, the blink rate is very different.
But here it's just working perfectly.
So here you have the structure that you can use for any action you want.
So here the action is to make the head blink.
OK, but this can be any other action, basically any action where you need to use a delay.
OK, let's say you want to read data from the sun.
So every five signal that you are not to use a delay of five sergant, you are going to use this structure.
And now you can see we have completely removed the delay from the loop function.
So, no, I have to say, don't use the delay.
Actually, I'm not going to say never use delay because they are just a few specific cases where using
delay is absolutely not a problem, let's say, in the setup function, let's say.
So if you first you initialize the mode for the beans and then you just want to digital.
Right.
And what you just want to put on the pin number one, OK, for three seconds.
OK, so when you start the program, you put on one for three things and then that's it.
And then you enter the loop.
OK, so the setup will only be executed once.
So that's not really a problem if you use a delay here.
Pejeta Right.
That's the energy work load.
So here I just want to hide for three seconds.
I also want to wait for some communication to start.
So let's say I'm communicating with a sensor and the sensor needs one point two seconds to start.
Then I can use a delay for one point two seconds.
OK, that would be more convenient.
And this is because we are in the setup function.
So this is like an initialization sequence.
You do whatever you want and then of course you make sure you don't plug anything in the loop function
and in the loop function sometimes.
Well, sometimes you will need to just wait a few microseconds for a response which comes from a sensor.
In that case, you can use the delay microseconds.
We were a small number, for example, 10 microseconds.
OK, this is a very, very small and sometimes actually that will really help you in your program to
keep the program simple when you need to read data from the sensor and you just need to wait a few microseconds
to get the result.
So to recap, don't use delay in your code, but just you can use delay in the setup of initialization
and in the loop, you can use delay microseconds with very small numbers in case you really need it.
All right.
And now this is time for you to practice more with the new time functionalities that you have just learned.