This is the solution of the previous activity where you need to send a counter from the Arduino to the
Raspberry Pi and then every 10s So independently from that first action, you're going to reset that
counter from the Raspberry Pi to the Arduino.
And let's get started in other ways.
I'm going to start with the Arduino code.
So let's create a INT counter is equal to zero here as a global variable.
And then I'm going to initialize.
So I'm going to set up the sale.
So sale dot begin with the baud rate and while sale like this.
Okay, sale is ready.
I'm going to create two new variables here.
Unsigned long last time let's say counter sent is equal to.
Millis are zero and then unsigned long counter.
So actually send counter delay is equal to 500 milliseconds in the void loop.
Unsigned long time now is equal to millis, which is the current time.
If the time now minus the last time we have sent the counter.
So this is a duration is greater or equal than the delay.
What we do is so we enter the if and we say that the last time we have sent the counter is equal to
time now.
So I go pretty fast with that because, well, that's a common structure we have already used before.
And then what I do is I simply do serial dot println.
With the county.
So it's going to send zero the first time and then do counter plus plus.
So basically, this is going to send zero and then one, two, three, four, five, every 500 milliseconds.
Okay.
And the counter is going to increase indefinitely.
Now, let's add the second action, which is when we receive the reset counter command, we reset the
counter and here we need to choose exactly the string which will correspond to that command.
Actually, let's just use that string reset, underscore, counter.
Okay, so what I usually do when I send data over like that with comments is I put underscore, okay,
if I need to separate two words.
So here we have the first action, which is going very fast and is non blocking.
And then we do if dot available strictly greater than zero and then we do string command is equal to
self dot read string until backslash n with single quotes and semicolon and then if command is equal
to reset counter.
So this comment should be exactly equal to reject counter.
Okay.
So if you send, for example, counter reject or any other string, it's not going to work.
So open, close, curly brackets.
And then what we do is simply do counter is equal to zero.
And that's it.
And then we have the else.
But in the else we do nothing here.
So any command that you send that is not reset counter is just going to go nowhere.
Okay.
We are not going to process that.
We're just going to process the reset counter.
So that's it for the Arduino code.
What we can do is we can already try this.
So let's run this on the Arduino activity three.
Compiling.
Uploading.
Okay.
Down, uploading.
I'm going to open the serial monitor.
We receive zero.
Okay.
You can see here we have restarted the program, actually, and we have zero, one, two, three, etcetera.
Great.
Now, if I send anything, nothing happens.
If I send a reset counter, boom.
The counter is being reset to zero.
So it's working.
Now let's go to the Python program and I'm going to go back to the previous activity and just.
Copy this for the structure.
Okay.
And let's remove everything in the while.
True.
Okay.
So once again, I use the same structure here, so I'm not going to write it every time and in the while
loop, what do we do?
So the first thing we need to do is to print the counter that we receive.
Okay, so if self dot in waiting is strictly greater than zero, once again, don't forget there is
no parenthesis here.
So if we have received something we do so counter is equal to c dot read line dot decode with UTF eight
dot strip and let's cast that to integer also because counter is an integer and we simply do print.
Um, let's say received counter plus and we print the counter and we cast it back to a string.
Of course, because now that's an integer.
All right, let's already try this code.
So the code here is running on the Arduino.
Let's try this code.
Run the script.
Let's name it.
Activity three.
Okay.
It's going to open this sale.
Okay.
Received counter, two, three, four, etcetera.
So as you can see, I'm going to kill it when I start again.
So we have sale, okay?
And we start to receive the counter at two here.
Why is that?
Because.
Well, you have the timeslip to give the time for the Arduino to actually start and set up the sale
correctly, and then the Arduino is going to directly start to send data.
Okay.
So it's going to start to send zero and then one, two, etcetera.
But basically the time that you slip, the Arduino already has sent zero and one, and at that time,
after the time slip, you do serial dot reset input buffer.
So the zero and one here are not processed because basically they are sent before you reach that line.
Okay, So actually, let's run this and comment the reset input buffer.
I'm going to run that again.
And you can see now we receive zero and one.
But if you check very carefully, we receive that directly at once.
Okay.
The zero and one is going to go in the buffer and this is going to be triggered for zero one and two
directly without any delay because zero and one are actually already inside the buffer.
So what's going to happen is that if you don't reset the input buffer, any data, any comment, anything
that's going to be received basically during this time, dot sleep is going to be processed at once.
Everything basically at the same time.
So that's why basically also add a reset input buffer is that you don't process many commands at the
same time at the beginning and you start from this point, you start with a clear input buffer and then
you only process any comment that you receive after that.
Okay, so we're going to continue like this.
Okay.
You can see received counter to three et-cetera and now well, the second part is that so we receive
the counter correctly.
Now what we are going to do is we are going to reset that counter every 10s and to do that we are not
going to use Time.sleep because if we use Time.sleep, well, what's going to happen is that this program
is going to be stuck for 10s okay, we are not going to go with this line.
And so when we go to this line after 10s, this is going to process everything in the buffer.
So we're going to see received counter, for example, two until 11 at once, and not just when we receive
it.
Okay.
We're going to see that late and all at once because they are going to be stuck in the input buffer.
So what we do is I'm going to do here, I'm going to add a variable last time reset counter is equal
to time dot time.
If you do that, you get the current time with Python and then reset.
Counter delay is equal to 10s.
So here we use seconds.
Okay.
And then here you have while loop.
So while true, if you have this, you enter this block of code.
This is going to be very fast.
And what I'm going to do is I'm going to add another action.
So here it will be action to.
And he action one.
So we get the current time.
So time now is equal to time dot time.
So as you can see, the structure is very similar to what we do in C plus plus with Arduino.
All right.
If time now minus the last time we reset the counter, if this is greater or equal than the delay,
we enter this if we do last time, reset counter is equal to time now.
Okay.
So exact same structure.
And what is the action?
The action is to send.
So cell dot right reset counter.
So the exact same string here backslash n dot encode with UTF eight.
All right.
And that should be it.
Let's actually do print send reset counter command so we know exactly when it has been sent from the
Raspberry Pi.
And let's try this program.
Okay.
Let's see if it works.
Run script.
Okay.
Received counter.
So basically we're going to wait 10s.
We should be around.
Okay.
You can see.
Send reset, counter command, and then we receive zero.
Okay, let's continue.
And you can see around 20.
So just before 20, where is it going to comment?
So we can actually see that it's correctly working okay, because here we send the counter every 500
milliseconds.
So 10s means that we have sent 20 times the counter, which is what we have done here from 0 to 19.
And then we send the reset command from the Raspberry Pi to the Arduino.
On the Arduino, we reset the counter to zero and we still continue to send the counter and receive
on the Raspberry Pi.
All right.
And that's the end of this activity and also the end of this section.
Let's now continue by adding more functionalities to the Arduino and use them in collaboration with
the Raspberry Pi.