WEBVTT

00:00.150 --> 00:06.720
This is the solution of the previous activity where you need to interact with the entities using the

00:06.960 --> 00:13.530
infrared, the remote controller and also print what you are going to do on the LCD screen.

00:13.800 --> 00:16.110
So we have quite a few things to do here.

00:16.530 --> 00:22.920
First, I'm going to initialize the infrared remote library to include.

00:24.060 --> 00:32.160
I remotely h I'm going to define the I r.

00:33.850 --> 00:40.780
Receive being digital pin number five for me, and I'm going to set up the sensor so I can make the

00:40.780 --> 00:44.070
mapping for the different buttons that I need.

00:44.530 --> 00:48.090
So I'm going to do the code for the visual three of the library.

00:48.580 --> 00:53.380
But if you use the version to please go back to one of the previous lessons so you can just get the

00:53.380 --> 00:58.900
code and also for the code that you can download and going to provide the solution for version two and

00:58.900 --> 00:59.920
also version three.

01:00.070 --> 01:05.560
So in the setup, I'm going to initialize sale because I will need sale to be able to match.

01:05.990 --> 01:11.910
OK, so later on I will be able to remove sailboard I need.

01:13.900 --> 01:15.820
All right, and then I.

01:17.790 --> 01:21.370
Or see her that begin with the pin.

01:23.140 --> 01:26.840
So that's it for the initialization now in the loop.

01:26.860 --> 01:31.330
I'm going to remove that, but keep the comments here now in the loop.

01:31.340 --> 01:32.380
I'm going to do if.

01:33.850 --> 01:36.550
I received a note.

01:37.520 --> 01:47.060
The code, so if we have received some data to do I receiver that resume very important, and then I'm

01:47.060 --> 01:57.710
going to say, let's Brent Allen with a receiver that decoded my data.

01:57.710 --> 02:02.300
That's coming back for Division three.

02:02.990 --> 02:03.560
All right.

02:03.560 --> 02:06.850
Let's make sure that the loop is closed here.

02:07.400 --> 02:14.150
Let's upload the program to the Arduino and let's see if it does activity 18.

02:18.350 --> 02:23.940
All right, don't applauding, I'm going to happen to say a monitor and I'm going to press what do.

02:24.150 --> 02:26.950
I'm going to need to use the button zero.

02:26.960 --> 02:30.830
So I press on zero multiple times to make sure I always tweet.

02:32.750 --> 02:46.010
Some can do it just after the same define button zero twenty two, and then I will need also the button

02:46.280 --> 02:53.300
one to button two, independent three and one, two and three.

02:54.910 --> 02:59.080
So I'm going to press on button one and get one.

03:01.010 --> 03:04.520
I'm going to press one button to get 24.

03:06.080 --> 03:12.140
So you can see sometimes I get zero because maybe I have pressed multiple times also next year, and

03:12.140 --> 03:18.200
that is that you correctly point to the sensor when you use the control 24.

03:19.720 --> 03:27.400
OK, I'm going to put twenty four here, twenty four, and now I press on with three, I get ninety

03:27.700 --> 03:28.120
four.

03:30.420 --> 03:31.050
All right.

03:33.080 --> 03:36.280
And I also need actually the.

03:37.480 --> 03:46.350
The AIPA button phone stop will just stop and this will be something to press on the button.

03:47.380 --> 03:49.900
This is for me, Seventy-one.

03:52.820 --> 03:53.360
All right.

03:54.530 --> 03:59.930
So that is the first step of the activity, mock debates now that I have done that, I can close the

03:59.930 --> 04:05.510
sale monitor, I can remove this, I actually can remove the sale of print anything that is going to

04:05.510 --> 04:06.560
come into it for now.

04:07.370 --> 04:14.450
And then what I can do is do my switch to go through all of the different buttons, OK, to check the

04:14.450 --> 04:21.620
command, all the data that I have received, if this can be mapped one of the button and then I'm going

04:21.620 --> 04:26.390
to do the action once I have correctly the button with the command.

04:26.540 --> 04:28.640
So let's do switch.

04:29.870 --> 04:37.940
And actually to be able to do switch, I need to create so into command, which will be equal to this.

04:38.630 --> 04:45.050
I are receiver that decoded data document and now I can remove that line.

04:47.320 --> 04:56.880
So quick comment, because Currentzis got braces and then you cave in to the case for the first one.

05:00.160 --> 05:08.490
And then it's the action that the zero and break, so I just redistrict you just.

05:09.820 --> 05:12.220
And then I have Kaze.

05:13.580 --> 05:15.140
We've but one.

05:17.480 --> 05:18.650
Let's do dakis.

05:20.220 --> 05:26.670
We've written to also Keys, we've written three.

05:29.240 --> 05:31.460
Case we've got.

05:34.010 --> 05:36.020
And don't forget the default.

05:37.260 --> 05:40.000
So here but one, I do.

05:41.270 --> 05:47.000
Action, but one and rich, same thing forward to.

05:48.780 --> 06:02.670
Action between the two and break the same thing forward and three, action, but three break and also

06:02.670 --> 06:03.270
for that one.

06:03.900 --> 06:07.170
So action button stop.

06:08.370 --> 06:08.800
Right.

06:10.230 --> 06:15.700
And the default is, let's say, handle default here.

06:16.740 --> 06:26.310
All right, so this way I can be pretty sure that the switch is correctly done and make sure that you

06:26.310 --> 06:27.590
double check on your problem.

06:27.960 --> 06:33.270
Let's just verify the code here, because I have added quite a lot of text.

06:34.140 --> 06:37.080
And you can see here I have also the same thing as before.

06:37.080 --> 06:38.640
I forgot the semicolon here.

06:43.610 --> 06:50.210
All right, don't complain, I'm not going to upload right now, but verify from time to time that the

06:50.210 --> 06:51.710
compilation is correct.

06:52.070 --> 06:58.910
So, for example, that I have not forgotten a semicolon are that have used Brace's in the wrong way.

06:59.420 --> 07:06.530
So now that we have a switch already, so we know which button we have pressed now we are ready to do

07:06.530 --> 07:07.180
the action.

07:07.520 --> 07:12.140
So what action we are going to do, we are going to toggle some entities.

07:12.740 --> 07:20.050
So now is the time is the next step is to handle the I'm going to use some define here.

07:20.840 --> 07:35.390
So any one thing let's do 12 define added to the 11 and define energy three been 10.

07:36.830 --> 07:39.260
So the other are the define here doesn't really matter.

07:39.740 --> 07:46.550
And then what I'm going to do here in the global scope, I'm going to create an array of VITAC because

07:46.550 --> 07:51.800
this is about the number of the energy and energy array.

07:51.920 --> 07:58.630
So this array will contain the different areas and let's put the size three.

07:59.030 --> 08:06.920
And actually instead of just writing three, I'm going to create and you define constant energy, the

08:06.920 --> 08:11.840
right size three so I can put a resize here.

08:11.960 --> 08:17.660
So we have already done that before and then I'm going to put a different.

08:19.620 --> 08:25.490
It is, and if you want any need to add any three.

08:27.930 --> 08:34.470
And because this line starts to be quite long, what I can do maybe is just go back to the new line

08:34.470 --> 08:39.460
after the equals sign unless I stop two or three times, OK?

08:40.390 --> 08:41.760
That is the same line.

08:42.420 --> 08:48.750
So I would be able to go through this area to go on the one, two and three with the index zero one

08:48.750 --> 08:49.250
and two.

08:49.660 --> 09:01.080
I know what I'm going to do is also create another array named eight state array with also the right

09:01.080 --> 09:01.560
size.

09:02.770 --> 09:04.090
And on this.

09:06.690 --> 09:13.200
All right, what I'm going to start is so the state of energy, because we are going to need to toggle

09:13.440 --> 09:14.090
the energy.

09:14.550 --> 09:22.770
So if I have stored in an area, I also need so that are teams of the 80s, I also need to know what

09:22.770 --> 09:26.380
are the states, what is the state for each state?

09:27.060 --> 09:35.820
So let's put low, low and low, which means that we will be able to go through both arrays and the

09:35.850 --> 09:43.740
index zero of this area will correspond to the pin number of the one and the index zero of that's the

09:43.750 --> 09:47.460
right way correspond of the state for the element.

09:48.440 --> 09:54.780
OK, so at the beginning, energy one with the state will be low and then energy two is located, three

09:54.780 --> 09:55.310
is low.

09:55.710 --> 09:57.900
So then we would be able to modify this.

09:58.140 --> 10:05.700
OK, if we want to modify the state for eighty two, we are going to modify this element of the story.

10:06.360 --> 10:06.750
All right.

10:06.780 --> 10:08.960
So now let's set up we've been mode.

10:09.510 --> 10:16.410
So in order to do that, I'm going to do void to create a new function in its energy mode.

10:17.610 --> 10:20.130
And in that I'm going to use a full loop.

10:20.470 --> 10:22.480
It iis zero.

10:22.500 --> 10:27.200
So we start at zero and then go until energy precise.

10:27.900 --> 10:29.260
So strictly lower.

10:30.780 --> 10:36.090
So we go from zero one to OK and then three we exit.

10:37.190 --> 10:38.320
I blusterous.

10:40.820 --> 10:49.120
And what we're going to do is, at any rate, we've I said we are going to take this and actually we

10:49.130 --> 10:54.190
are going to do most of the energy right.

10:54.260 --> 10:58.730
With the AI index to about.

11:00.880 --> 11:06.330
So first, we've zero here and then one key and then two here in three weeks.

11:07.240 --> 11:13.810
So this is very convenient because if we decide to add, let's say, five other and it is if we have

11:13.810 --> 11:15.790
eight, eight years, we don't need to change that.

11:16.330 --> 11:24.220
We just need to add a fine for one more here to put in the array but stage enduring and also define

11:24.220 --> 11:33.160
a new button, OK, to handle the new entities with the code, to actually handle the code, to initialize

11:33.160 --> 11:35.540
the mode and into toggle the state will be the same.

11:35.950 --> 11:45.470
So knowing the setup I can do in it entity not so entities are defined here and set up in the setup

11:46.120 --> 11:46.990
we've built.

11:47.530 --> 11:47.810
Great.

11:47.890 --> 11:49.080
So now what do we can do?

11:49.930 --> 11:54.020
Let's start with the command button zero.

11:54.430 --> 11:58.870
So here we want what do we want to do is to power of all the entities.

11:59.530 --> 12:07.220
So what I'm going to create a function here, void of all entities.

12:08.290 --> 12:15.940
So when you use a switch in the loop like this, it's often a good practice to not do the complete action

12:15.940 --> 12:20.820
here, but instead to create a function and call that function inside the switch.

12:22.050 --> 12:34.100
So for it, I saw the same I lower down and the three sides that I less plus.

12:36.040 --> 12:40.480
What we are going to do is so what do we need to do is to set.

12:42.170 --> 12:47.930
Each state for each entity here to gloat, because we are going then to use those modes to toggle the

12:47.930 --> 12:49.990
air, so we need to update this as well.

12:50.300 --> 12:56.660
So all the states agree with the index is below.

12:58.090 --> 13:00.640
And then we can do digital, right?

13:02.460 --> 13:13.310
Eddie Ray with time, OK, so this time we use this area for the pin and then look where we do is of

13:13.350 --> 13:18.180
the state in the right, and then we make sure that we apply the state to the pin.

13:19.910 --> 13:23.450
So no dysfunction, I can use it here.

13:24.080 --> 13:31.720
Power of all entities and then to print what we do on the city, we are going to do that just after.

13:32.360 --> 13:32.720
All right.

13:32.730 --> 13:35.360
So we have handled the button zero.

13:36.050 --> 13:40.450
Now we are going to handle the button one button to two and in three.

13:41.270 --> 13:51.830
And here I am going to create one new function e void Tuggle entity from index.

13:52.610 --> 13:56.880
So this function will take INT index.

13:56.890 --> 14:07.910
So that's the index of the array of the LG array here that we can give an index that is zero one of

14:07.910 --> 14:11.970
two because here we have set the to three.

14:12.440 --> 14:17.360
So what I'm going to do first in that function, I'm going to actually validate the data.

14:17.840 --> 14:23.840
So if we try to give toggle and different index, we, for example, index three, four, five or more,

14:24.320 --> 14:28.940
then we are not going to try to access an element that is outside of the array.

14:29.180 --> 14:36.980
So first, I'm going to if index is greater or equal, then the entity, the right size.

14:39.830 --> 14:42.350
I'm going simply to do return.

14:42.800 --> 14:47.710
So in the void, I have told you that we don't return any data.

14:48.110 --> 14:48.530
All right.

14:48.710 --> 14:52.490
But it doesn't mean that you can't return at any time.

14:52.910 --> 14:58.290
So here, if I just return with nothing, just means that I'm going to exit from the function.

14:58.760 --> 15:06.500
So if I sent an index here that is greater or equal than a right size, well, I'm not going to go through

15:06.500 --> 15:08.990
the array and find an element that is not here.

15:09.140 --> 15:13.020
I'm just going to return and know that we have validated the data.

15:13.580 --> 15:19.280
What I'm going to do is pull the two together, the entity we have seen before, that we have to get

15:19.280 --> 15:25.370
a state to state the state as a global variable and then check if the state is low.

15:25.370 --> 15:29.170
We set it too high and if the state is high, we set it too low.

15:29.390 --> 15:32.280
So we are going to do the same thing here, but just using the array.

15:32.900 --> 15:37.700
So we're going to do if L.G. states.

15:38.760 --> 15:40.680
The right wing index.

15:42.370 --> 15:44.350
If this is I.

15:45.740 --> 15:56.990
Then what we're going to do is the state array of this index, we're going to set it to no and then

15:56.990 --> 15:57.530
else.

15:58.400 --> 16:01.910
So there is only one possibility is energy.

16:01.910 --> 16:07.170
State theory of the index said it's too high.

16:07.190 --> 16:11.120
So that's the exact same thing that we have done with the energy state previously.

16:11.130 --> 16:19.550
But this time using an array and then what we can do after we have changed the state in disarray right

16:19.550 --> 16:19.840
here.

16:20.270 --> 16:26.210
What we can do is to do digital right with the PIN number.

16:26.210 --> 16:29.680
The PIN number for the index will be inside the energy array.

16:29.780 --> 16:30.980
So, Edilene.

16:32.630 --> 16:35.300
Right, with index.

16:36.340 --> 16:44.890
And we apply to this being the state that corresponds to the same index on the state every.

16:48.200 --> 16:55.070
All right, so this function with this function, you can toggle any anything from the array, whether

16:55.070 --> 17:03.110
you have one entity or ten entities, that's the same with dysfunction, dysfunction and that one.

17:03.440 --> 17:07.920
If you ever want to increase the number of ads, you don't need to change the functions.

17:08.600 --> 17:10.370
And now let's go.

17:11.060 --> 17:15.890
So let's call Tuggle entity from index directly here.

17:17.540 --> 17:19.250
So, Tugg, any different index?

17:19.260 --> 17:29.360
We are going to use the index zero here and then the index one here and then the index to.

17:32.290 --> 17:38.840
All right, let's just compile the code once again to see if we don't have any error done complaining.

17:39.430 --> 17:44.870
So if it's compiling, it means that you don't have compiled compilation error and we run on the avenue.

17:45.190 --> 17:50.620
It doesn't mean that the functionality would be exactly what you expected, OK, because you can still

17:50.620 --> 17:55.630
make errors that still compile, OK, if you make a logical error, that's still going to compile,

17:55.640 --> 17:59.850
but then your application will not behave the way you want to behave.
