Let's now see how to read data from the push button and more specifically, how to know when we have
pressed on the button.
So the exact moment when we have pressed on the button.
This is something you should already know how to do as per the prerequisites of the course, but I'm
still going to do it quickly.
As a recap.
Also, we will debounce the button.
So first of all, what you're going to do is to create a define here button.
So define a constant int.
And so the pin is number seven.
And then if you want to initialize the push button, you do pin mode with button pin and then input.
So the push button is initialized.
Now if you want to read from the push button, you are going to do digital read button pin and this
is going to return you a byte.
Okay, that is high or that is low.
So for example, bytes state is equal to digital read button pin.
Now if we want to check if we have pressed on the push button, what we need to do is we need to save
the previous state.
So we need to read the state, save the previous state, and then read again and check if we have something
different.
Okay.
So I'm going to add here, byte previous button state, and I'm simply going to initialize that here
to the current state of the button just after we initialize it.
So that will be the first state of the push button.
And so now what we need to do in the for loop is to read the push button again with digital read and
then compare to the previous button state and see if we have something different.
But what I'm also going to do is I'm going to debounce the push button because if we just compare the
previous and the current state, what's going to happen is we're going to have the bounce mechanism.
So the physical bounce of the button and this will give us some wrong measurements.
Okay.
So we need to debounce the button to make sure that when we actually press, we get just one press on
the code and not multiple prices because the button is bouncing.
So to do that, I'm going to create two new variables here.
Unsigned long last time button pressed is equal to.
So let's initialize to millis or to zero.
It doesn't matter.
And then unsigned long let's say button debounce is equal to and let's put 50 milliseconds.
So what we're going to do here, basically we're going to first in the void loop, get the time.
So unsigned long time now is equal to millis.
And then we are going to do if time now minus last time the button has been pressed if this is greater
or equal than the button debounce duration here, then we can enter this.
If so, we will see that when the state of the button changes, we're going to update the last time
the button was pressed to the current time and so that the next time we enter the void loop, we're
going to check the condition.
So time now minus last time the button has been pressed.
And if this is lower than the delay.
So basically, if this is lower than 50 milliseconds, then we are not going to read the state again.
So basically we are going to ignore the button for 50 milliseconds every time the button has been pressed
so we don't read all the bounces of the button.
So now if enough time has passed, what we can do is do bite button state.
So the current one is equal to digital read with button pin.
Like this.
We read the button state and then we do if Button State is different from the previous button state.
In that case, we know that the state has changed.
And what we can do is to now update the last time the button pressed.
So actually let's name it different.
Last time button changed.
Okay.
Because it's not pressed here, it's just changed because if we are here, it could be pressed or released.
So I'm just going to use changed, which is for both.
Okay.
So last time the button has changed is actually time now.
Okay.
Because that's what just happened.
And also I'm going to update the previous button state, which is now the current button state.
And so the next time we enter the void loop, we're going to first check if enough time has passed since
the last time button changed.
And we only going to enter this if again after 50 milliseconds and then well, when we are here in the
code, we know that the button state has changed.
So to know if the button has actually been pressed or released, we do.
If button State is equal to high in our case because we have a pull down resistor, this means that
the button has been pressed and if it's low, it means the button has been released.
So here we could do so.
Let's actually do sale dot begin.
So here we use sale to debug and serial dot print.
Ln let's say button has been pressed.
So when we enter this, if we know that the button has been pressed, so just pressed and we don't have
any bounds because the next time we are going to read the button is 50 milliseconds later.
So let's upload the code.
And actually, we have an error.
Why do I have this error?
You can see can't open device because I have simply not plugged my Arduino to the Raspberry Pi or to
the computer.
If you are using your normal computer.
And now check tools.
Okay.
Upload.
And done uploading.
I'm going to open the serial monitor.
Make sure the border is correct and I'm going to press on.
The push button button has been pressed.
Okay.
Press again.
And you can see every time I price.
So price again button has been pressed one more time and well, the code is working.
So this is the structure that you are going to use often to know when you have pressed or released on
a push button and with the debounce mechanism.