WEBVTT

00:00.080 --> 00:05.210
This is the solution of the previous activity where you need to send a counter from the Arduino to the

00:05.210 --> 00:12.080
Raspberry Pi and then every 10s So independently from that first action, you're going to reset that

00:12.080 --> 00:14.450
counter from the Raspberry Pi to the Arduino.

00:14.450 --> 00:16.580
And let's get started in other ways.

00:16.580 --> 00:18.800
I'm going to start with the Arduino code.

00:19.040 --> 00:24.980
So let's create a INT counter is equal to zero here as a global variable.

00:24.980 --> 00:26.810
And then I'm going to initialize.

00:26.810 --> 00:28.730
So I'm going to set up the sale.

00:28.730 --> 00:40.240
So sale dot begin with the baud rate and while sale like this.

00:40.250 --> 00:41.990
Okay, sale is ready.

00:42.390 --> 00:46.460
I'm going to create two new variables here.

00:46.460 --> 00:54.080
Unsigned long last time let's say counter sent is equal to.

00:56.230 --> 01:02.380
Millis are zero and then unsigned long counter.

01:02.380 --> 01:10.780
So actually send counter delay is equal to 500 milliseconds in the void loop.

01:12.490 --> 01:20.860
Unsigned long time now is equal to millis, which is the current time.

01:21.610 --> 01:27.940
If the time now minus the last time we have sent the counter.

01:27.940 --> 01:32.200
So this is a duration is greater or equal than the delay.

01:33.220 --> 01:41.140
What we do is so we enter the if and we say that the last time we have sent the counter is equal to

01:41.140 --> 01:42.160
time now.

01:42.160 --> 01:47.920
So I go pretty fast with that because, well, that's a common structure we have already used before.

01:47.920 --> 01:52.570
And then what I do is I simply do serial dot println.

01:53.530 --> 01:54.430
With the county.

01:55.000 --> 02:01.420
So it's going to send zero the first time and then do counter plus plus.

02:01.780 --> 02:09.340
So basically, this is going to send zero and then one, two, three, four, five, every 500 milliseconds.

02:09.370 --> 02:09.580
Okay.

02:09.580 --> 02:12.640
And the counter is going to increase indefinitely.

02:13.090 --> 02:19.510
Now, let's add the second action, which is when we receive the reset counter command, we reset the

02:19.510 --> 02:25.960
counter and here we need to choose exactly the string which will correspond to that command.

02:26.080 --> 02:30.760
Actually, let's just use that string reset, underscore, counter.

02:30.910 --> 02:40.300
Okay, so what I usually do when I send data over like that with comments is I put underscore, okay,

02:40.330 --> 02:42.430
if I need to separate two words.

02:43.100 --> 02:48.380
So here we have the first action, which is going very fast and is non blocking.

02:48.380 --> 03:01.880
And then we do if dot available strictly greater than zero and then we do string command is equal to

03:02.840 --> 03:16.730
self dot read string until backslash n with single quotes and semicolon and then if command is equal

03:16.730 --> 03:19.280
to reset counter.

03:20.670 --> 03:24.120
So this comment should be exactly equal to reject counter.

03:24.250 --> 03:24.390
Okay.

03:24.420 --> 03:30.180
So if you send, for example, counter reject or any other string, it's not going to work.

03:31.470 --> 03:34.710
So open, close, curly brackets.

03:34.710 --> 03:38.910
And then what we do is simply do counter is equal to zero.

03:40.730 --> 03:41.750
And that's it.

03:41.990 --> 03:43.660
And then we have the else.

03:43.820 --> 03:49.190
But in the else we do nothing here.

03:49.430 --> 03:55.190
So any command that you send that is not reset counter is just going to go nowhere.

03:55.280 --> 03:55.610
Okay.

03:55.610 --> 03:57.080
We are not going to process that.

03:57.110 --> 03:59.560
We're just going to process the reset counter.

03:59.570 --> 04:01.520
So that's it for the Arduino code.

04:01.550 --> 04:04.070
What we can do is we can already try this.

04:04.070 --> 04:09.890
So let's run this on the Arduino activity three.

04:13.290 --> 04:13.880
Compiling.

04:13.890 --> 04:14.850
Uploading.

04:15.600 --> 04:15.900
Okay.

04:15.930 --> 04:17.160
Down, uploading.

04:17.460 --> 04:19.440
I'm going to open the serial monitor.

04:19.680 --> 04:21.120
We receive zero.

04:22.590 --> 04:22.860
Okay.

04:22.860 --> 04:28.860
You can see here we have restarted the program, actually, and we have zero, one, two, three, etcetera.

04:29.220 --> 04:29.880
Great.

04:29.910 --> 04:32.940
Now, if I send anything, nothing happens.

04:32.940 --> 04:36.900
If I send a reset counter, boom.

04:36.930 --> 04:40.890
The counter is being reset to zero.

04:41.040 --> 04:42.630
So it's working.

04:42.720 --> 04:50.400
Now let's go to the Python program and I'm going to go back to the previous activity and just.

04:51.500 --> 04:53.210
Copy this for the structure.

04:53.240 --> 04:53.810
Okay.

04:55.220 --> 04:57.970
And let's remove everything in the while.

04:58.010 --> 04:58.580
True.

05:00.720 --> 05:00.990
Okay.

05:00.990 --> 05:07.320
So once again, I use the same structure here, so I'm not going to write it every time and in the while

05:07.320 --> 05:08.490
loop, what do we do?

05:08.490 --> 05:13.190
So the first thing we need to do is to print the counter that we receive.

05:13.200 --> 05:20.310
Okay, so if self dot in waiting is strictly greater than zero, once again, don't forget there is

05:20.310 --> 05:21.620
no parenthesis here.

05:21.630 --> 05:35.670
So if we have received something we do so counter is equal to c dot read line dot decode with UTF eight

05:36.240 --> 05:47.670
dot strip and let's cast that to integer also because counter is an integer and we simply do print.

05:48.930 --> 05:58.050
Um, let's say received counter plus and we print the counter and we cast it back to a string.

05:58.080 --> 05:59.970
Of course, because now that's an integer.

06:00.000 --> 06:01.950
All right, let's already try this code.

06:02.490 --> 06:04.590
So the code here is running on the Arduino.

06:04.590 --> 06:05.840
Let's try this code.

06:06.090 --> 06:07.170
Run the script.

06:07.440 --> 06:08.400
Let's name it.

06:08.400 --> 06:11.400
Activity three.

06:13.750 --> 06:13.910
Okay.

06:13.950 --> 06:15.640
It's going to open this sale.

06:15.670 --> 06:15.990
Okay.

06:16.000 --> 06:18.670
Received counter, two, three, four, etcetera.

06:18.670 --> 06:22.900
So as you can see, I'm going to kill it when I start again.

06:25.000 --> 06:27.340
So we have sale, okay?

06:27.340 --> 06:30.580
And we start to receive the counter at two here.

06:30.760 --> 06:31.480
Why is that?

06:31.480 --> 06:31.840
Because.

06:31.840 --> 06:39.040
Well, you have the timeslip to give the time for the Arduino to actually start and set up the sale

06:39.040 --> 06:43.510
correctly, and then the Arduino is going to directly start to send data.

06:43.540 --> 06:44.050
Okay.

06:44.050 --> 06:47.890
So it's going to start to send zero and then one, two, etcetera.

06:48.010 --> 06:54.820
But basically the time that you slip, the Arduino already has sent zero and one, and at that time,

06:54.820 --> 06:58.930
after the time slip, you do serial dot reset input buffer.

06:58.930 --> 07:08.020
So the zero and one here are not processed because basically they are sent before you reach that line.

07:08.020 --> 07:13.160
Okay, So actually, let's run this and comment the reset input buffer.

07:13.160 --> 07:14.930
I'm going to run that again.

07:18.330 --> 07:22.020
And you can see now we receive zero and one.

07:22.020 --> 07:26.860
But if you check very carefully, we receive that directly at once.

07:26.890 --> 07:27.310
Okay.

07:27.330 --> 07:33.540
The zero and one is going to go in the buffer and this is going to be triggered for zero one and two

07:33.570 --> 07:39.360
directly without any delay because zero and one are actually already inside the buffer.

07:39.360 --> 07:45.510
So what's going to happen is that if you don't reset the input buffer, any data, any comment, anything

07:45.510 --> 07:52.140
that's going to be received basically during this time, dot sleep is going to be processed at once.

07:52.140 --> 07:54.420
Everything basically at the same time.

07:54.600 --> 08:01.350
So that's why basically also add a reset input buffer is that you don't process many commands at the

08:01.350 --> 08:09.300
same time at the beginning and you start from this point, you start with a clear input buffer and then

08:09.300 --> 08:12.930
you only process any comment that you receive after that.

08:13.140 --> 08:15.570
Okay, so we're going to continue like this.

08:20.320 --> 08:20.530
Okay.

08:20.530 --> 08:27.460
You can see received counter to three et-cetera and now well, the second part is that so we receive

08:27.460 --> 08:28.510
the counter correctly.

08:28.510 --> 08:34.840
Now what we are going to do is we are going to reset that counter every 10s and to do that we are not

08:34.840 --> 08:41.620
going to use Time.sleep because if we use Time.sleep, well, what's going to happen is that this program

08:41.620 --> 08:48.460
is going to be stuck for 10s okay, we are not going to go with this line.

08:48.460 --> 08:53.920
And so when we go to this line after 10s, this is going to process everything in the buffer.

08:53.920 --> 09:01.480
So we're going to see received counter, for example, two until 11 at once, and not just when we receive

09:01.480 --> 09:01.660
it.

09:01.660 --> 09:01.900
Okay.

09:01.900 --> 09:09.040
We're going to see that late and all at once because they are going to be stuck in the input buffer.

09:09.040 --> 09:20.570
So what we do is I'm going to do here, I'm going to add a variable last time reset counter is equal

09:20.570 --> 09:24.470
to time dot time.

09:25.440 --> 09:29.630
If you do that, you get the current time with Python and then reset.

09:30.050 --> 09:35.720
Counter delay is equal to 10s.

09:35.840 --> 09:37.370
So here we use seconds.

09:37.400 --> 09:37.910
Okay.

09:38.120 --> 09:40.760
And then here you have while loop.

09:40.760 --> 09:44.720
So while true, if you have this, you enter this block of code.

09:44.720 --> 09:46.550
This is going to be very fast.

09:46.550 --> 09:49.370
And what I'm going to do is I'm going to add another action.

09:49.520 --> 09:52.850
So here it will be action to.

09:54.260 --> 09:57.140
And he action one.

09:57.740 --> 09:59.480
So we get the current time.

09:59.480 --> 10:05.870
So time now is equal to time dot time.

10:05.870 --> 10:10.820
So as you can see, the structure is very similar to what we do in C plus plus with Arduino.

10:10.820 --> 10:11.420
All right.

10:11.780 --> 10:21.950
If time now minus the last time we reset the counter, if this is greater or equal than the delay,

10:23.000 --> 10:30.620
we enter this if we do last time, reset counter is equal to time now.

10:31.190 --> 10:31.430
Okay.

10:31.430 --> 10:32.930
So exact same structure.

10:32.930 --> 10:34.910
And what is the action?

10:34.910 --> 10:36.650
The action is to send.

10:36.650 --> 10:42.140
So cell dot right reset counter.

10:42.140 --> 10:52.650
So the exact same string here backslash n dot encode with UTF eight.

10:54.180 --> 10:54.870
All right.

10:54.870 --> 10:57.960
And that should be it.

10:58.290 --> 11:08.820
Let's actually do print send reset counter command so we know exactly when it has been sent from the

11:08.820 --> 11:09.510
Raspberry Pi.

11:09.810 --> 11:12.030
And let's try this program.

11:12.030 --> 11:12.300
Okay.

11:12.300 --> 11:13.680
Let's see if it works.

11:13.680 --> 11:14.700
Run script.

11:17.880 --> 11:18.320
Okay.

11:18.570 --> 11:19.710
Received counter.

11:20.070 --> 11:23.010
So basically we're going to wait 10s.

11:23.870 --> 11:25.040
We should be around.

11:25.260 --> 11:25.450
Okay.

11:25.460 --> 11:25.850
You can see.

11:25.890 --> 11:28.520
Send reset, counter command, and then we receive zero.

11:30.330 --> 11:31.710
Okay, let's continue.

11:33.400 --> 11:35.040
And you can see around 20.

11:35.050 --> 11:38.140
So just before 20, where is it going to comment?

11:38.140 --> 11:44.030
So we can actually see that it's correctly working okay, because here we send the counter every 500

11:44.030 --> 11:44.800
milliseconds.

11:44.800 --> 11:52.630
So 10s means that we have sent 20 times the counter, which is what we have done here from 0 to 19.

11:52.630 --> 11:57.340
And then we send the reset command from the Raspberry Pi to the Arduino.

11:57.370 --> 12:03.190
On the Arduino, we reset the counter to zero and we still continue to send the counter and receive

12:03.190 --> 12:04.330
on the Raspberry Pi.

12:04.780 --> 12:05.230
All right.

12:05.230 --> 12:09.180
And that's the end of this activity and also the end of this section.

12:09.190 --> 12:15.880
Let's now continue by adding more functionalities to the Arduino and use them in collaboration with

12:15.880 --> 12:16.750
the Raspberry Pi.
