WEBVTT

00:00.180 --> 00:05.730
Now, what I do understand, digital pings as input beams a little bit better and that you have set

00:05.730 --> 00:11.550
up your secrets with the pushbutton, let's actually read the state from the push button.

00:11.910 --> 00:17.340
So the first thing I'm going to do is, of course, because we are using a pin, I'm going to use a

00:17.340 --> 00:21.090
defined here, just as we did for the entity.

00:21.300 --> 00:24.120
But here we are going to name it button.

00:25.990 --> 00:34.360
OK, and the PIN number is digital PIN number two, so I write to next, I need to set the load for

00:34.360 --> 00:41.800
the pin, OK, either input output here because we want to read something from the button I'm going

00:41.800 --> 00:43.840
to use in mode.

00:43.850 --> 00:52.450
We've gotten in an input so you can see input to also turn blue.

00:52.480 --> 00:55.780
OK, because this is a key world recognized by the.

00:57.190 --> 00:59.860
And then what I'm going to do is in the loop.

00:59.860 --> 01:07.090
I am going to print the state of the button every 100 millisecond not to print.

01:07.270 --> 01:10.750
I'm going first dial that begin.

01:12.470 --> 01:15.020
Ganassi initialised, say, a communication.

01:16.210 --> 01:23.680
And here I am going to the CIA that Brent Åhléns, so we print with a new line every time.

01:24.100 --> 01:25.500
And what do we print?

01:25.510 --> 01:31.780
We print the detail reign so you can see digital read around.

01:32.980 --> 01:37.060
And then Digital Read will take just one parameter, which is the.

01:38.630 --> 01:46.310
No other people so button up, which is defined here as to, OK, so we call the digital read function,

01:47.450 --> 01:54.950
the result of this function will be given to the print and in function, which will print the state

01:54.950 --> 01:55.510
of the button.

01:56.000 --> 02:05.110
And then because we don't want to go too fast for our eyes, would just add a delay of one hundred milliseconds.

02:05.110 --> 02:11.810
So we are going to print the button, stayed at ten hits ten times every single.

02:12.080 --> 02:19.640
Make sure that you have correctly plugged your bone and I'm going to first comp..

02:19.640 --> 02:22.190
So push button.

02:24.280 --> 02:27.550
All right, done compiling, I'm going to upload the code.

02:29.580 --> 02:31.670
And open the same money door.

02:33.500 --> 02:41.990
And you can see we have zero, so we don't have low or high octane printing on the same monitor because

02:41.990 --> 02:46.250
low will be evaluated to zero when you try to print it.

02:46.280 --> 02:50.390
OK, and now I'm going to press the pause button.

02:51.520 --> 02:58.510
I keep the push button pressed and you can see we have one, OK, I'll release the first button and

02:58.510 --> 02:59.400
we have zero.

02:59.800 --> 03:05.370
I press the button, we have one which corresponds to I and I realize we have zero.

03:05.380 --> 03:06.520
So it's working.

03:07.240 --> 03:07.930
OK, great.

03:08.270 --> 03:14.830
So now you can actually know very easily with just this line of code plus one line of setup.

03:15.130 --> 03:18.490
You can know what is the state of the push button?

03:18.820 --> 03:21.190
Is the push button pressed or not pressed?

03:21.940 --> 03:24.980
And let's improve this just a little bit.

03:25.270 --> 03:32.650
So instead of just printing the state, we can print some text, which may make more sense for humans

03:32.650 --> 03:39.610
who are reading and say, monitor, I'm going to remove that and I'm going to do if, OK.

03:40.640 --> 03:42.890
Digital read.

03:44.400 --> 03:51.620
Better theme, so I'm going to compare the results of these functions, so which will be either high

03:51.630 --> 03:55.290
or low to the equal equal.

03:55.320 --> 03:58.770
Make sure you put to equal sign a gate.

03:59.040 --> 04:06.120
If what we read from the spin of the bottom is high, then we are going to enter this block of code.

04:06.300 --> 04:09.060
And I'm going to say that Rintel in.

04:11.180 --> 04:11.930
He's pressed.

04:14.930 --> 04:23.180
OK, and then we can do so, we could do as if digital brain of the button being equal to low, OK,

04:23.180 --> 04:29.000
but because this is the only other solution, OK, because this is a binary state, I just need to write

04:29.150 --> 04:32.400
its essay on that.

04:33.020 --> 04:33.560
Ellen.

04:34.760 --> 04:37.640
Button is not pressed.

04:39.070 --> 04:46.600
Like this, we keep the delay here so we don't print too fast, so now we are going to print either

04:46.720 --> 04:55.420
this text or this text depending on if we press the button on it, let's that's the code.

04:55.420 --> 05:01.850
And let's open the a monitor and you will see Button is not pressed.

05:02.290 --> 05:04.090
Now I'm going to press the button again.

05:04.660 --> 05:08.860
Button pressed a button button is not pressed.

05:09.250 --> 05:09.550
Right.

05:09.600 --> 05:17.020
So this will actually make more sense if you want to bring something for a user to see what's going

05:17.020 --> 05:18.040
on in your application.

05:18.580 --> 05:24.610
So to recap, if you want to read data from a digital pin first, you define it to be here, then we've

05:24.610 --> 05:25.260
been mode.

05:25.270 --> 05:31.960
You said the mode to input and then you can just use digital read to reach the state from the pin.

05:32.170 --> 05:39.750
And one very useful thing to do is to compare this with high or low so you can use a, if true to your

05:39.770 --> 05:47.820
condition with a structure to decide what actions you are going to take depending on the state you read

05:47.830 --> 05:48.910
from the digital Peter.

05:50.090 --> 05:55.670
And just one thing I want to show you and think, OK, if you are using the simulation, so I have just

05:55.670 --> 05:59.810
put the same code here with the same circuits I'm running to code.

06:00.530 --> 06:07.220
So on the same monitor you can see Button is not pressed, OK, and then heads and you can actually

06:07.220 --> 06:10.120
interact with the button.

06:10.130 --> 06:14.420
So if I click with the mouse here, you can see we have button is pressed.

06:14.750 --> 06:17.230
If I release we have Button is not pressed.

06:17.330 --> 06:24.290
OK, you can actually click to interact just as if it were real hardware.
