This is step number three of the final project of the intercom system.
And now that we have initialized all the hardware components on the Arduino, we're going to write the
code so we can send the output messages to the Raspberry Pi when we have something that happens with
the hardware.
So actually here we have just the button code to write.
So first of all, I'm going to save this as intercom step three.
Okay?
Okay.
Save.
And let's actually open again the protocol that we have written here.
So from it's right there.
So we can see here from Arduino to Raspberry Pi, we just have the button pressed command to send with
backslash n.
So that's what we are going to do.
And so.
Well, this we have already done that before, but I'm still going to do it again.
Right there is to read from the push button and detect when we have pressed on the push button.
So I'm going to create a 2 or 3 new variables here.
Let's do that right there as a global variables.
So unsigned long last time button changed with equal to millis or zero, it doesn't matter.
And unsigned long debounce delay.
Let's put 50 milliseconds.
You can adjust that a bit more, a bit less depending on how it works with your circuit.
So this is going to be for the Debounce mechanism.
And then I'm going to also add byte previous button state and the previous button state.
I initialize it here, previous button state is equal to digital read from the button pin.
And of course I do that after I have put the pin mode with button pin input now in the void loop.
So in the void loop, we can read from the push button, but before we read from the push button, we're
going to do the Debounce mechanism to be sure we can read from the push button.
So unsigned long time now is equal to millis.
So we get the time every well, every time we enter the void loop, which is going to be pretty fast.
If so, if the current time time now minus the last time the button state has changed here is greater
or equal then the debounce delay.
So if we have spent enough time since the button has changed for the last time, then we can read the
button state.
So bite button state is equal to digital read button pin.
And then we can compare the button state.
So the current one we read from the previous one.
So from previous button state.
If those are different, then we have either pressed or released the button.
And in that case, first we can update this.
So the last time the button has changed is actually now.
So we put time.
Now we also say that the previous button state is the current one for the next time we enter the void
loop.
And then we can do an action so we can check if, for example, button State is equal to high, which
means that we have pressed on the button because we have a pull down resistor.
And if it's low, we have just released the button.
But here we want to know when we press on the button and once we have this, we can send to sales.
So send the message to sale and to send the message to sale.
I'm going to initialize sale here.
Serial dot.
Begin with this baud rate.
We're going to use the same in the Raspberry Pi.
Let's do while not serial okay for compatibility with some Arduino boards and here I'm going to add
something else that I didn't do before and we probably won't need it in that project, but that can
be a good best practice to add it to the while serial dot available.
Is greater than zero.
You do serial dot read.
So read is going to read only one byte.
So here we just read it and we don't store it.
We don't do anything with it.
But basically what we do here is we just clear the input buffer.
If you remember with Serial in Python, you have the reset input buffer function.
We don't have one in Arduino, but we can easily implement it by just checking that well, while we
still have bytes on the input buffer, we read them so we clear them from the buffer.
So we are sure that when we start here we don't have anything in the input buffer.
And so I said for this project is not going to be useful because why is that?
Because when we start the Python program and we initialize the communication with the Arduino, well,
the Arduino is going to be ready first.
So by the time that we enter this block of code and that we enter the void loop, the Python program
is still not going to be sending data.
Okay?
So this is should not be called, but that can be a best practice for other projects of your own.
Okay.
If you want to make different boards communicate between each other and now that the cell is initialized,
let's actually do here serial dot print.
Ln okay, because we need the backslash n at the end button pressed exactly like that.
This should be it.
Let's check.
So we have those three variables here.
We correctly initialize the state for the button.
We read the previous state.
We have the serial initialization and then we read from the push button with the debounce mechanism.
And when the button is pressed, we send button pressed.
Let's connect the Arduino.
Okay, let's now so it's good.
Let's now upload the code and well, actually I have forgotten so just made a typo here.
Uploading down, uploading.
Let's open the serial monitor and I'm going to press on the push button.
So starting pressing the push button.
Button pressed button pressed button pressed.
So every time I press, I receive a button pressed message on the serial monitor.
Everything is correct.
And that's the end of this.
Step number three, we can now focus on the input comments that we receive from the Raspberry Pi.