This is the solution of the previous activity where you need to toggle the two and three when you just
press the button and when you need to also make the LG one blink every one second, OK?
And the two actions running at the same time.
So let's go step by step here.
Let's first make this energy blink, OK?
And then we are going to handle the budget.
So we initialize energy one in.
So this has been 12 and then to make the blink, well, because we are not going to use delay.
I'm going to create and sign long.
Last time they won.
Kate initialised to Minnesota Zero and unsigned long.
Blink, delay anyone, and this will be one thousand milliseconds, but we have our two variables here
and we also need something to use by the one they initialize it to load.
OK, so this will allow us to know when was the last time we have blinks?
The this will simply give us the time we need to wait between every time we want to blink.
And this we can keep the state of the earth.
So then we can toggle it in the if so now in the setup I'm going to use.
In mode, we added one thing and set it an output.
All right.
And in the loops, I'm going to remove all that I'm going to do and signed long.
Time now.
But this is quite common.
Usually at the beginning of the loop, you just get the current time and then for every action you do,
you can use directly this current time, because everything here.
After that, we go very, very fast.
So you don't need to update all the time with minutes.
You can just use time now for every action you do.
This would be the same time.
And let's make it to link anyone.
Let's do this action.
And here after that, we're going to toggle added two and three.
So what do we do to make the evening we check if the time know, minus the last time anyone blinked,
if the duration is greater than the blindingly?
Then we enter if what we can do is.
To the last time anyone blink, we add the duration, so we added the delay, so the next time we enter
the IF will be after a minimum this duration.
So after one single.
And then we check if any one date is equal to eye with two equals.
Then any one state becomes law with one equal and.
And one day it becomes high.
So that is exactly the same as what we did in the section on time functionalities, OK?
Nothing new here.
And after we have changed the state, we do digital.
Right.
And walk in with one state viable.
OK, that's it.
Now I'm going to go directly.
I'm just going to verify the code.
Lets me meet activity Helidon.
OK, Don, compiling so there is no campaign era here.
Let's continue now.
I'm going to do the Segan action, which is toggled two and three when the button has been pressed.
So I'm first going to hear.
Define Button Penha two, so everything we do step by step, I'm going first to monitor the press button
and then to handle it is the.
Those three variables here, wait for the editor's note, I'm going to do unsigned long.
Last time.
But then changed.
And unsigned, long live rounds delayed, let's put 50 milliseconds and we also need to store so.
Wouldn't they'd start a brain state, let's put it low for the first time?
So, as you can see here, quite similar.
OK, we stole the last time we executed the action here.
Here we still the last time the burden has changed.
OK, now this is the delay between every time we do the action and this is the delay to wait after the
button state has changed before we can read the state again.
OK, so this is very similar, but not the exact same functionality as you can see here.
And I'm going to do.
In mode, button in input's.
And what improvement what I can do here is directly update button state is equal to digital read button.
So just after I have initialized the pin, I can update the state because here we just say that the
button state will be low.
But actually maybe it can be high when we start the program.
If someone is already pressing the pause button, then maybe time.
So.
By doing this, just after we initialize the mood on the button, then we can correctly initialize the
state.
So current state of the page, but that's a quick improvement.
And now let's write the code.
So if time now we use the same thing now here, because this will go very fast.
So the time now can be considered as the same.
We don't need to use Milice again.
So every time no minus the last time, the button that has changed is greater than the difference delay.
What we can do.
Is to read so bright when you learn that, quote, digital.
Reed, we've beaten in, so we read the state of the button, and then once we have the red state on
the button, we check if the new button state is different from the current or the previous button state.
If yes, then the first thing we do is to update the last time the on changed.
To time, not because the bottom changed just now.
OK, just when we read it and we can also the current state is equal to new burdens.
OK, so the next time we enter this, if here we have the correct value for the state and then after
that, we can do the action which will be toggled two and three.
And one quick note here.
You can see I have made this activity on purpose so you can see the time functionality to make an election
every X amount of time like here and the functionality to be balanced.
But this is similar, but this is not the same functionality here.
So you will use the time every time you check with the last time you have made executive action.
OK, is this duration greater than the delay you have said?
Yes.
Then what you do is you directly add the delay to the last time and then you execute the action.
So the next time you execute the action would be exactly the delay after so one thousand milliseconds
after you have executed that one.
Now for the purpose, this is different.
We don't change the duration between the time now and the last time we have the right the button.
OK, we change the duration between time now and the last time the button has actually changed.
So if the button state didn't change, we are still going to continue to enter this very quickly.
Every time we enter the loop, every time we enter the volume and we are going to read with digits the
state of the button very fast.
Only when the button state changes, we are going to update that last time viable.
OK, but we are not going to add additional delay like we did here.
We are going to simply updated to the time.
No, because the goal is different here.
We just want to make sure that after the state has changed, we wait at least the delay before we read
the value again, only when the button state has changed.
That's a different purpose and that's a different code.
But with this, you should better understand the two separate applications we can do here and now.
Let's well, let's make those ELENITA in three or four.
So first, I need to initialize them, define eddied to anyone and define and then that.
So I'm going to go quite quick here because that's something you already know and I'm going to initialize
in the mode of the.
I would put.
Energy three be.
Output.
OK.
And to be able to toggle between the two and it is I'm going to add a new variable here.
Which name is Stogel?
State that's initialized to one, so let's say that when the total energy state is one, we are going
to pour on the two and two of the three and then we are going to switch with a simple state machine.
When the state is to we're going to do the opposite.
All right.
So now we can do that here, because now we know that the button state has just changed.
What we are going to do is add another if a button state.
Is with two equal I.
So we know the State of the Union has changed, but we don't know if the State of the Union has changed
to protest or not.
So we simply need to check the value, the exact value.
If it's high, it means we just priced.
But if it's low, we just released the burden.
So now we want to do an action only when we press the button.
I'm going to check if Tuggle any state is one.
Then we put together a date to two and we also put on some digital.
Energy to begin to low.
The literal state is to.
The report of the energy to print and digital rights and the green been hype.
OK, now if the.
Tioga State is two, which is the only option, so I'm going to use only your it's not as if I do to
at the.
Is equal to one, just one equal, then digital rights.
Anything to.
The eye and digital pride added to the look.
But I simply toggle between the two states, OK, Louhi, and then I look OK, if you want to improve
the code, you could also write this into a function and write this into a function that you call from
the voidable.
Actually, that's what I'm going to do.
That's a void link and one.
And I'm going to put this here.
I remove the indentation with Chief and Anton.
And he I called Link and one.
So the courts may be clearer for you and then I'm going to do void.
Stogel.
It is.
Let's take to the other end, it is one and two other Elize and.
I'm just going to take that.
Here, but Dan.
And remove the indentation that we have and call the function right here.
Other entities.
So now in the loop, I just have the time, functionality, OK, and the action that we do is actually
in separate functions.
Let's see if that combines.
Yes, and let's upload the code.
Code is in the arena and you can see the first lady is blinking every once again.
OK, no problem with that.
Now I'm going to press the pause button and you can see we have the three which is broken.
I pressed the button again.
You have energy, too, which is power, OK?
Eighty three.
Eighty two, three, eight.
Only when I press the button.
So I press the state changes.
I release and nothing happens because it's only when we press the button.
OK, and as you can see, once you understand how to do this and how to do the that, well, it's not
that difficult because that will be the same code structure every time.
So now you need to do 20 different actions.
Will you simply repeat this block of code 20 different times with additional variables here?
Of course.
So every time you need to do an action, you do this.
Every time you need to be a push button.
Are any senseor that require that they balance?
Well, you simply use the same good structure.
OK, so the hardest is behind you and now you can truly that to write such powerful applications.
All right.
That's the end of this activity.
And this section also on how to balance the push button.