This is the solution of the previous activity where we agreed to improve the current program we have
so that we can make it, the three entities blink and stop when we press the button.
So we have quite a few things to do, but let's start step by step.
So first, we already have our Dippin right here.
OK, I'm going to work on the set up function first.
Right.
So following the setup, we have four p.m. and three digital.
Right.
So the ping button doesn't change in here.
Instead of using Benmore three times what I'm going to use a full.
That at zero and then I and we want to continue wide eyed is typically lower than the size of discovery.
I'm going to I less pass.
So it will start at zero and then one and then two, and when it arrives at three, three, which is
out of the great.
Then this condition is false and then we exit the fall, so as a reminder, here we have index zero,
index one, an index to we start to count at zero.
And then when you have so when you have the size of the array or the index, you have to stop at the
size minus one because we have historically lower than.
We will stop at the site, minus one, what we do in Moad.
And then so we need to find the PIN number and PIN number is actually inside the area.
We are going to write the name of ring in.
All right.
We've brackets in the brackets.
We're going to give the index and then I'll put.
So this will first, though, this will be I would be zero, so it will be one thing just like we did
here and then energy two and three and that's it.
But we can remove.
So three lines here and now the call is actually much clearer.
OK, we just use Benwood once and that's it.
The weather, we have three inches.
Two hundred.
And it is we have the same coat.
And let's put this into a function, it void set in mode, so we create a function, we void because
we don't return anything, we don't require any parameter.
OK, we are simply going to use directly the array inside the function.
OK, and what we can do is simply take this.
Up and set it there.
So this is our function, which will set the spin mode and then of course this function alone will not
do anything.
This is just a block of code that you declare and that can be used.
But if you don't use it, well, it's not going to do anything.
So in the setup function, we call the function set in mode.
All right.
And now let's do the same thing with this block of.
So I'm just going to create a function directly.
Then of all ages.
And we're going to do a full also.
Start at zero and then I is typically lower than the average size.
That is the same.
A plus plus.
All right, the ones you know how to go through an all right, well, the follow doesn't change.
It's always the same.
And then digital rights.
And in theory, I just like it here.
And then simply load.
And this function will turn off all the entities, just like the name suggests.
So what we can do now is simply call the function of both entities.
And let's not forget this semicolon, OK, when you call a function.
You don't need them.
OK, don't put a semicolon after you declare a function with the definition of the function in the block,
but when you call them, have to use the semicolon and let's just remove that.
And as you can see, our set of code is no much clearer.
And this will work whether you have three of 10 or more entities.
Great.
So now let's handle the loop function in the loop.
We are still going to use the digital arena check for a pushbutton, OK, but we simply want to remove
all of the digital rights as well.
OK.
I'm going to create a function here.
Which name is what do they say to these?
OK, and so void as well.
OK, because this is a function to execute an action, not to return something.
And then I'm going to copy that you.
The controlled explosion cut and then put it here, as you can see, the indentation is completely wrong.
So what I can do is.
I select everything in it it.
You can you can see increase or decrease the indents.
For example, we shift and double and decrease it.
Going to do this, that we have to in addition, here or here, that's all right.
I know here I'm simply going to call the function.
So let's see if everything is correct in the total dysfunction, so we take the blink states, OK,
this viable is it?
But in a more global scope, we can use it.
If it's one, then we simply do our business here I how we change the state.
So the next time we enter the function, the state is changed.
And instead of going here we go else and we do this, but we change the blink rate for it.
It is.
And then we change the variable to go back to one the next time we call it a function.
And of course after that we wait for three hundred milliseconds.
Actually, this maybe instead of putting here, I'm going to put it there.
The total LSD is just in charge of doubling the and it is then in the loop.
We had the delay so we can change the delay outside of this function.
In the loop, we only have it if the button is not pressed, we took all the ladies and we wait three
hundred milliseconds.
All right.
And now you can see that we have some valuables here for initialization.
We have some constants.
We have one, two and three functions.
And in the setup, we only have three lines of code and in the loop we only have four lines of code,
so that's actually much better.
The one thing here is that actually by doing which we did in this activity, we increased the total
number of lines in the program.
OK, the program may seem a little bit more complex, but the truth is that it's actually much less
complicated because the setup is very minimal.
The loop is very minimal.
And then the functions will allow you to easily change the behavior of your program.
So now we have three entities.
If they want to add a fourth entity, I just need to add a defined here.
But it's a defined entity for PIN nine and then change here to four.
Add the eight to the array and.
Well, this doesn't change.
This doesn't change.
The setup and the loop don't change either, we just change also here, the 80s, but it's much more
easier to handle and everything we did here, creating functions, using full loops, going through
arrays, etc. We are going to use them, of course, in the following of these calls and you are going
to use them in many projects that you create.
All right.
And of course, let's test the program.
So I'm going to verify it.
Number seven.
OK, and let's applaud code to see if we are not wrong with what we need and if we have the exact same
results and yes, it seems that it's working the exact same way as before.
All right.
So that's the end of this activity and that's also the end of the first part of the.
So congratulations.
You have not seen all the basic stuff you need all the basic hardware functionalities to be able to
create more complex program.