This is the solution of the previous activity where you need to make an energy drink and actually choose
the delay from cellular communication.
So from what you receive, in the end, you also need to validate that user.
So first things first.
I'm going to create a defined here and then let's use the pin to have the right entity in the set,
the function.
I'm going to initialize the cell communication.
The one hundred fifteen thousand two hundred bothered issues that worry you and I'm going to also do
say that said.
Time out.
With ten milliseconds.
So that we are not stuck too long in the past in function.
And then, of course, I need to do spin mode and.
Being with about.
So I have initialized Saola communication and I have initialised demoed for Spin of the eight.
Now let's create this very simple building code, digital right in with high.
DeLay, so let's delay empty digital right energy in low and keep the delay empty.
We need to give a value for the delay.
I'm not going to hardcourt these for you.
We're going to use a variable that I'm going to create here.
Let's put it in the jar because we want to value between one hundred and one thousand.
So that's good.
We are not going to overflow the integer and it's a blink delay.
And we are going to initialize the value to, let's say, five hundred.
So not Lindela is what I'm going to use in the delay here.
All right, so we have a basic living example and know what I'm going to do here is check if we have.
So if say that the label.
Greater than zero.
So if we have received some data, this is a translation of that, we are going to do it so the data
will be.
They'll let us eat.
And then what are we can do is we can put the data inside the building, delete the what we've received
from the past and we put it in the blink delete, which we've changed to delete for the rest of the
program.
But before that, we are going to validate the data.
So if data is greater or equal than one hundred and.
With the two ampersands.
Data is lower of equal than 1000 then we can do what we want.
I'm going to add parentheses here to make it clearer.
So he had the first condition, is the data greater or equal than one hundred?
The second condition is data lower or equal than one thousand.
And we put and between the two.
So the two conditions must be true for it to be true.
So if we send 50, this will be false.
This would be true, but the result will be false.
If we send two thousand, this would be true.
This would be false and the result is false.
But if we send, let's say, eight hundred in both conditions are true and the result is true.
So if we have validated the user inputs, we can say the delay.
Is now equal to data.
So let's say we send four hundred, OK, we have the type of available we received the data pass it
data becomes 400, we validate the data and then blink, delay becomes four hundred, and then we do
delete 400.
And here again, we have a small problem is that we only be able to change the delay at this point of
the program.
OK, we can't change the delay between those two states, OK, because the sale that pass, it will
only happen at this line of the code.
So once you are here, you do need it right away.
So this will not program and then you do digital right now.
You wait again and then you can change the delay.
You can't change the delay between those two.
So what I'm going to do, I'm just going to change this a little bit.
OK, so let's say it.
And in the end, let's start with low.
OK, and here instead of this block of code and we to do if a date is low.
And what we do is we change it to.
I.
OK, and and so it means the state is high because there are only two states.
Eighty eight is low.
What would happen here this time?
The state is low, so we enter the blueprinted if this condition is true.
And then we put the states too high and we exceed the complete stranger, we go back to the beginning
of the loop.
Now, energy state is high, so this is false.
We go in the end and then we set back the energy state to low and then we come back again and this is
true, et, etc..
So what we are going to do with that is simply go digital right in with any.
And then delay.
We.
They did it and I can remove all of that.
OK, so this way, the result here is exactly the same, but we are going to be able to receive data
from sale between each change of state for the.
And let's applaud the program to the arena, so let's limit activity eight.
They and the program is imploding, so we also verify that the competition is working.
Yes.
And you can see so the ADC, the first one is blinking every five hundred milliseconds.
So quite a normal behavior.
Now, I'm going to open the sale monitor and let's try to send a different venue.
Let's try to send two hundred and let's see what happens.
So I press enter and you can see now the energy blinks much faster.
OK, now let's go back to five hundred.
OK, links are the five hundred milliseconds.
Now, let's put one down, which is the maximum, and you can see the energy blinks at one thousand.
Let's go back to the minimum, which is one hundred.
At one hundred.
OK, so you can modify the value any time you want.
OK.
Now, if they decided to build, let's say, 3000 or three seasons center and you can see nothing changes
it.
Because here in the program, we have validated the data so we can only give values between one hundred
and one thousand six hundred, we won't pay if I tried to give to that network work and we still have
the 600 millisecond delay.
So that is working quite well.
OK, but actually there is a small issue here with the timing, that may be a real problem depending
on what you want to do with your application.
So let's say you have put the lead delay to one thousand and then you sent another delay to one hundred.
So you want to change from one thousand two hundred.
The thing is that so when you use delay with one thousand, this will block the program for one complete
signal.
All right.
Let's say you send one hundred when the program has just entered the delay with one signal.
What will happen is that your data that you sent will be processed.
But after one second, OK, because first the delay will happen and then you will be able to read the
data that the Ardino has received, which means that when, for example, you have the delay, which
is one second and you want to change to one hundred millisecond, you may have to wait up to one single
one thousand millisecond so that the change can be effective.
And that may be quite a problem.
Don't know, let's say, but the maximum I'm going to give you an example for the maximum at four thousand.
So that would be for Siggins.
And when you say a monitor, I send for town.
OK, so now the power of, as you can see, power, I'm going to get one hundred and wait until the
end is put off.
OK, send one hundred.
And what happens is that the it is pulled off did for about four seconds and then the value changes.
OK, so.
That was the problem that I was talking about and, well, good news, because that's exactly what we
are going to solve in the next section.