WEBVTT

00:00.210 --> 00:06.360
This is the solution of the previous activity where you need to toggle the two and three when you just

00:06.360 --> 00:12.480
press the button and when you need to also make the LG one blink every one second, OK?

00:12.510 --> 00:15.040
And the two actions running at the same time.

00:15.660 --> 00:17.240
So let's go step by step here.

00:17.520 --> 00:20.570
Let's first make this energy blink, OK?

00:20.850 --> 00:23.240
And then we are going to handle the budget.

00:23.910 --> 00:28.650
So we initialize energy one in.

00:30.180 --> 00:36.330
So this has been 12 and then to make the blink, well, because we are not going to use delay.

00:36.690 --> 00:39.180
I'm going to create and sign long.

00:40.610 --> 00:44.480
Last time they won.

00:48.520 --> 00:53.050
Kate initialised to Minnesota Zero and unsigned long.

00:54.930 --> 01:05.720
Blink, delay anyone, and this will be one thousand milliseconds, but we have our two variables here

01:06.240 --> 01:13.080
and we also need something to use by the one they initialize it to load.

01:13.830 --> 01:18.300
OK, so this will allow us to know when was the last time we have blinks?

01:18.360 --> 01:25.280
The this will simply give us the time we need to wait between every time we want to blink.

01:25.590 --> 01:28.140
And this we can keep the state of the earth.

01:28.620 --> 01:34.620
So then we can toggle it in the if so now in the setup I'm going to use.

01:36.660 --> 01:45.240
In mode, we added one thing and set it an output.

01:48.040 --> 01:48.560
All right.

01:48.590 --> 01:54.250
And in the loops, I'm going to remove all that I'm going to do and signed long.

01:55.360 --> 01:56.470
Time now.

01:58.660 --> 01:59.860
But this is quite common.

01:59.890 --> 02:04.810
Usually at the beginning of the loop, you just get the current time and then for every action you do,

02:04.810 --> 02:08.960
you can use directly this current time, because everything here.

02:09.020 --> 02:11.020
After that, we go very, very fast.

02:11.170 --> 02:14.310
So you don't need to update all the time with minutes.

02:14.320 --> 02:17.090
You can just use time now for every action you do.

02:17.410 --> 02:18.610
This would be the same time.

02:19.030 --> 02:22.790
And let's make it to link anyone.

02:23.620 --> 02:24.910
Let's do this action.

02:25.150 --> 02:30.790
And here after that, we're going to toggle added two and three.

02:32.700 --> 02:41.820
So what do we do to make the evening we check if the time know, minus the last time anyone blinked,

02:42.790 --> 02:45.330
if the duration is greater than the blindingly?

02:47.160 --> 02:50.550
Then we enter if what we can do is.

02:53.270 --> 03:01.220
To the last time anyone blink, we add the duration, so we added the delay, so the next time we enter

03:01.220 --> 03:05.240
the IF will be after a minimum this duration.

03:05.270 --> 03:07.190
So after one single.

03:08.320 --> 03:16.810
And then we check if any one date is equal to eye with two equals.

03:18.090 --> 03:24.210
Then any one state becomes law with one equal and.

03:26.190 --> 03:29.820
And one day it becomes high.

03:29.850 --> 03:35.920
So that is exactly the same as what we did in the section on time functionalities, OK?

03:36.330 --> 03:37.280
Nothing new here.

03:37.650 --> 03:40.650
And after we have changed the state, we do digital.

03:41.160 --> 03:41.670
Right.

03:42.270 --> 03:47.630
And walk in with one state viable.

03:48.300 --> 03:49.250
OK, that's it.

03:49.260 --> 03:51.060
Now I'm going to go directly.

03:51.060 --> 03:52.760
I'm just going to verify the code.

03:53.340 --> 03:57.330
Lets me meet activity Helidon.

03:59.910 --> 04:04.060
OK, Don, compiling so there is no campaign era here.

04:04.650 --> 04:06.350
Let's continue now.

04:06.360 --> 04:12.690
I'm going to do the Segan action, which is toggled two and three when the button has been pressed.

04:13.020 --> 04:15.900
So I'm first going to hear.

04:18.110 --> 04:26.090
Define Button Penha two, so everything we do step by step, I'm going first to monitor the press button

04:26.090 --> 04:28.550
and then to handle it is the.

04:29.580 --> 04:35.460
Those three variables here, wait for the editor's note, I'm going to do unsigned long.

04:36.480 --> 04:38.040
Last time.

04:39.350 --> 04:40.940
But then changed.

04:44.200 --> 04:53.350
And unsigned, long live rounds delayed, let's put 50 milliseconds and we also need to store so.

04:54.360 --> 04:59.220
Wouldn't they'd start a brain state, let's put it low for the first time?

04:59.850 --> 05:01.770
So, as you can see here, quite similar.

05:01.820 --> 05:05.710
OK, we stole the last time we executed the action here.

05:05.910 --> 05:08.370
Here we still the last time the burden has changed.

05:08.590 --> 05:15.780
OK, now this is the delay between every time we do the action and this is the delay to wait after the

05:15.780 --> 05:19.380
button state has changed before we can read the state again.

05:19.920 --> 05:24.440
OK, so this is very similar, but not the exact same functionality as you can see here.

05:26.070 --> 05:27.210
And I'm going to do.

05:28.870 --> 05:32.980
In mode, button in input's.

05:34.260 --> 05:43.980
And what improvement what I can do here is directly update button state is equal to digital read button.

05:44.850 --> 05:51.750
So just after I have initialized the pin, I can update the state because here we just say that the

05:51.750 --> 05:52.800
button state will be low.

05:52.800 --> 05:56.510
But actually maybe it can be high when we start the program.

05:57.000 --> 06:01.150
If someone is already pressing the pause button, then maybe time.

06:02.130 --> 06:02.550
So.

06:03.900 --> 06:10.950
By doing this, just after we initialize the mood on the button, then we can correctly initialize the

06:10.950 --> 06:11.310
state.

06:11.330 --> 06:14.760
So current state of the page, but that's a quick improvement.

06:15.540 --> 06:18.990
And now let's write the code.

06:19.320 --> 06:26.370
So if time now we use the same thing now here, because this will go very fast.

06:26.380 --> 06:28.630
So the time now can be considered as the same.

06:29.550 --> 06:31.610
We don't need to use Milice again.

06:32.100 --> 06:41.550
So every time no minus the last time, the button that has changed is greater than the difference delay.

06:43.540 --> 06:44.470
What we can do.

06:46.130 --> 06:51.080
Is to read so bright when you learn that, quote, digital.

06:52.400 --> 07:01.910
Reed, we've beaten in, so we read the state of the button, and then once we have the red state on

07:01.910 --> 07:10.490
the button, we check if the new button state is different from the current or the previous button state.

07:12.260 --> 07:18.410
If yes, then the first thing we do is to update the last time the on changed.

07:19.430 --> 07:23.310
To time, not because the bottom changed just now.

07:23.450 --> 07:30.710
OK, just when we read it and we can also the current state is equal to new burdens.

07:31.220 --> 07:37.730
OK, so the next time we enter this, if here we have the correct value for the state and then after

07:37.730 --> 07:42.550
that, we can do the action which will be toggled two and three.

07:42.980 --> 07:44.300
And one quick note here.

07:44.300 --> 07:51.620
You can see I have made this activity on purpose so you can see the time functionality to make an election

07:51.620 --> 07:56.970
every X amount of time like here and the functionality to be balanced.

07:57.420 --> 08:02.960
But this is similar, but this is not the same functionality here.

08:02.970 --> 08:09.830
So you will use the time every time you check with the last time you have made executive action.

08:10.040 --> 08:13.760
OK, is this duration greater than the delay you have said?

08:13.970 --> 08:14.570
Yes.

08:14.570 --> 08:21.890
Then what you do is you directly add the delay to the last time and then you execute the action.

08:22.940 --> 08:28.970
So the next time you execute the action would be exactly the delay after so one thousand milliseconds

08:28.970 --> 08:30.950
after you have executed that one.

08:31.790 --> 08:34.000
Now for the purpose, this is different.

08:34.520 --> 08:39.490
We don't change the duration between the time now and the last time we have the right the button.

08:39.550 --> 08:45.620
OK, we change the duration between time now and the last time the button has actually changed.

08:46.280 --> 08:53.090
So if the button state didn't change, we are still going to continue to enter this very quickly.

08:53.630 --> 08:59.340
Every time we enter the loop, every time we enter the volume and we are going to read with digits the

08:59.420 --> 09:01.520
state of the button very fast.

09:01.790 --> 09:07.980
Only when the button state changes, we are going to update that last time viable.

09:08.030 --> 09:13.220
OK, but we are not going to add additional delay like we did here.

09:13.430 --> 09:15.870
We are going to simply updated to the time.

09:15.900 --> 09:17.970
No, because the goal is different here.

09:17.990 --> 09:24.500
We just want to make sure that after the state has changed, we wait at least the delay before we read

09:24.770 --> 09:28.910
the value again, only when the button state has changed.

09:29.510 --> 09:32.630
That's a different purpose and that's a different code.

09:33.020 --> 09:38.450
But with this, you should better understand the two separate applications we can do here and now.

09:38.450 --> 09:43.070
Let's well, let's make those ELENITA in three or four.

09:43.670 --> 09:54.800
So first, I need to initialize them, define eddied to anyone and define and then that.

09:56.090 --> 10:01.970
So I'm going to go quite quick here because that's something you already know and I'm going to initialize

10:02.450 --> 10:04.910
in the mode of the.

10:07.860 --> 10:08.600
I would put.

10:12.110 --> 10:14.870
Energy three be.

10:16.150 --> 10:16.760
Output.

10:17.020 --> 10:17.310
OK.

10:18.100 --> 10:23.850
And to be able to toggle between the two and it is I'm going to add a new variable here.

10:25.330 --> 10:27.100
Which name is Stogel?

10:30.000 --> 10:36.030
State that's initialized to one, so let's say that when the total energy state is one, we are going

10:36.030 --> 10:43.190
to pour on the two and two of the three and then we are going to switch with a simple state machine.

10:43.740 --> 10:47.580
When the state is to we're going to do the opposite.

10:48.510 --> 10:48.980
All right.

10:49.020 --> 10:55.260
So now we can do that here, because now we know that the button state has just changed.

10:55.650 --> 10:59.670
What we are going to do is add another if a button state.

11:01.700 --> 11:04.130
Is with two equal I.

11:06.800 --> 11:12.260
So we know the State of the Union has changed, but we don't know if the State of the Union has changed

11:12.650 --> 11:13.910
to protest or not.

11:14.660 --> 11:18.020
So we simply need to check the value, the exact value.

11:18.030 --> 11:20.790
If it's high, it means we just priced.

11:20.850 --> 11:23.410
But if it's low, we just released the burden.

11:23.420 --> 11:27.120
So now we want to do an action only when we press the button.

11:27.650 --> 11:32.690
I'm going to check if Tuggle any state is one.

11:34.520 --> 11:41.720
Then we put together a date to two and we also put on some digital.

11:44.390 --> 11:47.870
Energy to begin to low.

11:48.950 --> 11:50.330
The literal state is to.

11:52.020 --> 12:00.140
The report of the energy to print and digital rights and the green been hype.

12:00.940 --> 12:02.840
OK, now if the.

12:04.610 --> 12:11.470
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

12:12.160 --> 12:13.430
at the.

12:16.400 --> 12:20.230
Is equal to one, just one equal, then digital rights.

12:21.560 --> 12:22.640
Anything to.

12:23.520 --> 12:31.380
The eye and digital pride added to the look.

12:33.010 --> 12:39.070
But I simply toggle between the two states, OK, Louhi, and then I look OK, if you want to improve

12:39.070 --> 12:45.850
the code, you could also write this into a function and write this into a function that you call from

12:45.850 --> 12:46.460
the voidable.

12:47.020 --> 12:48.550
Actually, that's what I'm going to do.

12:49.150 --> 12:53.200
That's a void link and one.

12:56.600 --> 12:58.370
And I'm going to put this here.

13:02.180 --> 13:05.690
I remove the indentation with Chief and Anton.

13:07.110 --> 13:11.130
And he I called Link and one.

13:13.250 --> 13:18.300
So the courts may be clearer for you and then I'm going to do void.

13:20.000 --> 13:20.570
Stogel.

13:22.540 --> 13:23.090
It is.

13:26.040 --> 13:32.160
Let's take to the other end, it is one and two other Elize and.

13:33.780 --> 13:35.430
I'm just going to take that.

13:37.820 --> 13:39.200
Here, but Dan.

13:40.250 --> 13:46.360
And remove the indentation that we have and call the function right here.

13:50.260 --> 13:51.550
Other entities.

13:59.760 --> 14:06.000
So now in the loop, I just have the time, functionality, OK, and the action that we do is actually

14:06.030 --> 14:07.920
in separate functions.

14:10.270 --> 14:12.520
Let's see if that combines.

14:14.170 --> 14:17.470
Yes, and let's upload the code.

14:19.950 --> 14:25.700
Code is in the arena and you can see the first lady is blinking every once again.

14:25.770 --> 14:27.250
OK, no problem with that.

14:27.630 --> 14:34.620
Now I'm going to press the pause button and you can see we have the three which is broken.

14:34.890 --> 14:36.330
I pressed the button again.

14:36.720 --> 14:39.780
You have energy, too, which is power, OK?

14:39.850 --> 14:40.560
Eighty three.

14:40.810 --> 14:42.420
Eighty two, three, eight.

14:42.750 --> 14:44.160
Only when I press the button.

14:44.160 --> 14:46.290
So I press the state changes.

14:46.290 --> 14:50.250
I release and nothing happens because it's only when we press the button.

14:52.570 --> 15:00.220
OK, and as you can see, once you understand how to do this and how to do the that, well, it's not

15:00.220 --> 15:04.540
that difficult because that will be the same code structure every time.

15:04.570 --> 15:07.660
So now you need to do 20 different actions.

15:07.900 --> 15:13.820
Will you simply repeat this block of code 20 different times with additional variables here?

15:13.840 --> 15:14.380
Of course.

15:15.220 --> 15:17.620
So every time you need to do an action, you do this.

15:17.830 --> 15:20.800
Every time you need to be a push button.

15:21.890 --> 15:24.990
Are any senseor that require that they balance?

15:25.310 --> 15:27.590
Well, you simply use the same good structure.

15:28.190 --> 15:35.090
OK, so the hardest is behind you and now you can truly that to write such powerful applications.

15:35.820 --> 15:36.320
All right.

15:36.320 --> 15:37.730
That's the end of this activity.

15:37.730 --> 15:41.810
And this section also on how to balance the push button.
