Now, what I do understand, digital pings as input beams a little bit better and that you have set
up your secrets with the pushbutton, let's actually read the state from the push button.
So the first thing I'm going to do is, of course, because we are using a pin, I'm going to use a
defined here, just as we did for the entity.
But here we are going to name it button.
OK, and the PIN number is digital PIN number two, so I write to next, I need to set the load for
the pin, OK, either input output here because we want to read something from the button I'm going
to use in mode.
We've gotten in an input so you can see input to also turn blue.
OK, because this is a key world recognized by the.
And then what I'm going to do is in the loop.
I am going to print the state of the button every 100 millisecond not to print.
I'm going first dial that begin.
Ganassi initialised, say, a communication.
And here I am going to the CIA that Brent Åhléns, so we print with a new line every time.
And what do we print?
We print the detail reign so you can see digital read around.
And then Digital Read will take just one parameter, which is the.
No other people so button up, which is defined here as to, OK, so we call the digital read function,
the result of this function will be given to the print and in function, which will print the state
of the button.
And then because we don't want to go too fast for our eyes, would just add a delay of one hundred milliseconds.
So we are going to print the button, stayed at ten hits ten times every single.
Make sure that you have correctly plugged your bone and I'm going to first comp..
So push button.
All right, done compiling, I'm going to upload the code.
And open the same money door.
And you can see we have zero, so we don't have low or high octane printing on the same monitor because
low will be evaluated to zero when you try to print it.
OK, and now I'm going to press the pause button.
I keep the push button pressed and you can see we have one, OK, I'll release the first button and
we have zero.
I press the button, we have one which corresponds to I and I realize we have zero.
So it's working.
OK, great.
So now you can actually know very easily with just this line of code plus one line of setup.
You can know what is the state of the push button?
Is the push button pressed or not pressed?
And let's improve this just a little bit.
So instead of just printing the state, we can print some text, which may make more sense for humans
who are reading and say, monitor, I'm going to remove that and I'm going to do if, OK.
Digital read.
Better theme, so I'm going to compare the results of these functions, so which will be either high
or low to the equal equal.
Make sure you put to equal sign a gate.
If what we read from the spin of the bottom is high, then we are going to enter this block of code.
And I'm going to say that Rintel in.
He's pressed.
OK, and then we can do so, we could do as if digital brain of the button being equal to low, OK,
but because this is the only other solution, OK, because this is a binary state, I just need to write
its essay on that.
Ellen.
Button is not pressed.
Like this, we keep the delay here so we don't print too fast, so now we are going to print either
this text or this text depending on if we press the button on it, let's that's the code.
And let's open the a monitor and you will see Button is not pressed.
Now I'm going to press the button again.
Button pressed a button button is not pressed.
Right.
So this will actually make more sense if you want to bring something for a user to see what's going
on in your application.
So to recap, if you want to read data from a digital pin first, you define it to be here, then we've
been mode.
You said the mode to input and then you can just use digital read to reach the state from the pin.
And one very useful thing to do is to compare this with high or low so you can use a, if true to your
condition with a structure to decide what actions you are going to take depending on the state you read
from the digital Peter.
And just one thing I want to show you and think, OK, if you are using the simulation, so I have just
put the same code here with the same circuits I'm running to code.
So on the same monitor you can see Button is not pressed, OK, and then heads and you can actually
interact with the button.
So if I click with the mouse here, you can see we have button is pressed.
If I release we have Button is not pressed.
OK, you can actually click to interact just as if it were real hardware.