This is the solution of the previous activity where you need to interact with the entities using the
infrared, the remote controller and also print what you are going to do on the LCD screen.
So we have quite a few things to do here.
First, I'm going to initialize the infrared remote library to include.
I remotely h I'm going to define the I r.
Receive being digital pin number five for me, and I'm going to set up the sensor so I can make the
mapping for the different buttons that I need.
So I'm going to do the code for the visual three of the library.
But if you use the version to please go back to one of the previous lessons so you can just get the
code and also for the code that you can download and going to provide the solution for version two and
also version three.
So in the setup, I'm going to initialize sale because I will need sale to be able to match.
OK, so later on I will be able to remove sailboard I need.
All right, and then I.
Or see her that begin with the pin.
So that's it for the initialization now in the loop.
I'm going to remove that, but keep the comments here now in the loop.
I'm going to do if.
I received a note.
The code, so if we have received some data to do I receiver that resume very important, and then I'm
going to say, let's Brent Allen with a receiver that decoded my data.
That's coming back for Division three.
All right.
Let's make sure that the loop is closed here.
Let's upload the program to the Arduino and let's see if it does activity 18.
All right, don't applauding, I'm going to happen to say a monitor and I'm going to press what do.
I'm going to need to use the button zero.
So I press on zero multiple times to make sure I always tweet.
Some can do it just after the same define button zero twenty two, and then I will need also the button
one to button two, independent three and one, two and three.
So I'm going to press on button one and get one.
I'm going to press one button to get 24.
So you can see sometimes I get zero because maybe I have pressed multiple times also next year, and
that is that you correctly point to the sensor when you use the control 24.
OK, I'm going to put twenty four here, twenty four, and now I press on with three, I get ninety
four.
All right.
And I also need actually the.
The AIPA button phone stop will just stop and this will be something to press on the button.
This is for me, Seventy-one.
All right.
So that is the first step of the activity, mock debates now that I have done that, I can close the
sale monitor, I can remove this, I actually can remove the sale of print anything that is going to
come into it for now.
And then what I can do is do my switch to go through all of the different buttons, OK, to check the
command, all the data that I have received, if this can be mapped one of the button and then I'm going
to do the action once I have correctly the button with the command.
So let's do switch.
And actually to be able to do switch, I need to create so into command, which will be equal to this.
I are receiver that decoded data document and now I can remove that line.
So quick comment, because Currentzis got braces and then you cave in to the case for the first one.
And then it's the action that the zero and break, so I just redistrict you just.
And then I have Kaze.
We've but one.
Let's do dakis.
We've written to also Keys, we've written three.
Case we've got.
And don't forget the default.
So here but one, I do.
Action, but one and rich, same thing forward to.
Action between the two and break the same thing forward and three, action, but three break and also
for that one.
So action button stop.
Right.
And the default is, let's say, handle default here.
All right, so this way I can be pretty sure that the switch is correctly done and make sure that you
double check on your problem.
Let's just verify the code here, because I have added quite a lot of text.
And you can see here I have also the same thing as before.
I forgot the semicolon here.
All right, don't complain, I'm not going to upload right now, but verify from time to time that the
compilation is correct.
So, for example, that I have not forgotten a semicolon are that have used Brace's in the wrong way.
So now that we have a switch already, so we know which button we have pressed now we are ready to do
the action.
So what action we are going to do, we are going to toggle some entities.
So now is the time is the next step is to handle the I'm going to use some define here.
So any one thing let's do 12 define added to the 11 and define energy three been 10.
So the other are the define here doesn't really matter.
And then what I'm going to do here in the global scope, I'm going to create an array of VITAC because
this is about the number of the energy and energy array.
So this array will contain the different areas and let's put the size three.
And actually instead of just writing three, I'm going to create and you define constant energy, the
right size three so I can put a resize here.
So we have already done that before and then I'm going to put a different.
It is, and if you want any need to add any three.
And because this line starts to be quite long, what I can do maybe is just go back to the new line
after the equals sign unless I stop two or three times, OK?
That is the same line.
So I would be able to go through this area to go on the one, two and three with the index zero one
and two.
I know what I'm going to do is also create another array named eight state array with also the right
size.
And on this.
All right, what I'm going to start is so the state of energy, because we are going to need to toggle
the energy.
So if I have stored in an area, I also need so that are teams of the 80s, I also need to know what
are the states, what is the state for each state?
So let's put low, low and low, which means that we will be able to go through both arrays and the
index zero of this area will correspond to the pin number of the one and the index zero of that's the
right way correspond of the state for the element.
OK, so at the beginning, energy one with the state will be low and then energy two is located, three
is low.
So then we would be able to modify this.
OK, if we want to modify the state for eighty two, we are going to modify this element of the story.
All right.
So now let's set up we've been mode.
So in order to do that, I'm going to do void to create a new function in its energy mode.
And in that I'm going to use a full loop.
It iis zero.
So we start at zero and then go until energy precise.
So strictly lower.
So we go from zero one to OK and then three we exit.
I blusterous.
And what we're going to do is, at any rate, we've I said we are going to take this and actually we
are going to do most of the energy right.
With the AI index to about.
So first, we've zero here and then one key and then two here in three weeks.
So this is very convenient because if we decide to add, let's say, five other and it is if we have
eight, eight years, we don't need to change that.
We just need to add a fine for one more here to put in the array but stage enduring and also define
a new button, OK, to handle the new entities with the code, to actually handle the code, to initialize
the mode and into toggle the state will be the same.
So knowing the setup I can do in it entity not so entities are defined here and set up in the setup
we've built.
Great.
So now what do we can do?
Let's start with the command button zero.
So here we want what do we want to do is to power of all the entities.
So what I'm going to create a function here, void of all entities.
So when you use a switch in the loop like this, it's often a good practice to not do the complete action
here, but instead to create a function and call that function inside the switch.
So for it, I saw the same I lower down and the three sides that I less plus.
What we are going to do is so what do we need to do is to set.
Each state for each entity here to gloat, because we are going then to use those modes to toggle the
air, so we need to update this as well.
So all the states agree with the index is below.
And then we can do digital, right?
Eddie Ray with time, OK, so this time we use this area for the pin and then look where we do is of
the state in the right, and then we make sure that we apply the state to the pin.
So no dysfunction, I can use it here.
Power of all entities and then to print what we do on the city, we are going to do that just after.
All right.
So we have handled the button zero.
Now we are going to handle the button one button to two and in three.
And here I am going to create one new function e void Tuggle entity from index.
So this function will take INT index.
So that's the index of the array of the LG array here that we can give an index that is zero one of
two because here we have set the to three.
So what I'm going to do first in that function, I'm going to actually validate the data.
So if we try to give toggle and different index, we, for example, index three, four, five or more,
then we are not going to try to access an element that is outside of the array.
So first, I'm going to if index is greater or equal, then the entity, the right size.
I'm going simply to do return.
So in the void, I have told you that we don't return any data.
All right.
But it doesn't mean that you can't return at any time.
So here, if I just return with nothing, just means that I'm going to exit from the function.
So if I sent an index here that is greater or equal than a right size, well, I'm not going to go through
the array and find an element that is not here.
I'm just going to return and know that we have validated the data.
What I'm going to do is pull the two together, the entity we have seen before, that we have to get
a state to state the state as a global variable and then check if the state is low.
We set it too high and if the state is high, we set it too low.
So we are going to do the same thing here, but just using the array.
So we're going to do if L.G. states.
The right wing index.
If this is I.
Then what we're going to do is the state array of this index, we're going to set it to no and then
else.
So there is only one possibility is energy.
State theory of the index said it's too high.
So that's the exact same thing that we have done with the energy state previously.
But this time using an array and then what we can do after we have changed the state in disarray right
here.
What we can do is to do digital right with the PIN number.
The PIN number for the index will be inside the energy array.
So, Edilene.
Right, with index.
And we apply to this being the state that corresponds to the same index on the state every.
All right, so this function with this function, you can toggle any anything from the array, whether
you have one entity or ten entities, that's the same with dysfunction, dysfunction and that one.
If you ever want to increase the number of ads, you don't need to change the functions.
And now let's go.
So let's call Tuggle entity from index directly here.
So, Tugg, any different index?
We are going to use the index zero here and then the index one here and then the index to.
All right, let's just compile the code once again to see if we don't have any error done complaining.
So if it's compiling, it means that you don't have compiled compilation error and we run on the avenue.
It doesn't mean that the functionality would be exactly what you expected, OK, because you can still
make errors that still compile, OK, if you make a logical error, that's still going to compile,
but then your application will not behave the way you want to behave.