-
void loop() {
-
if (pir.motion_detected()) {
-
Serial.println("Motion detected");
25
} else {
-
Serial.println("No motion detected");
-
}
-
delay(200);
-
}
30
With the constant
PIR_INPUT_PIN
, you can define the digital pin you’ve connected
your PIR sensor to. In line 4, we begin the definition of a class named
Passive-
InfraredSensor
that encapsulates all things related to PIR sensors.
We define a member variable named
_input_pin
that stores the number of the
digital pin we’ve connected our sensor to. Then we define a constructor that
expects the pin number as an argument and assigns it to our member variable.
The only method we need to define is
motion_detected
. It returns
true
if it has
currently detected a motion and
false
otherwise. So, it has to check only
whether the current state of the sensor’s digital pin is
HIGH
or
LOW
.
Compile the sketch and upload it to your Arduino. You should see an output
similar to the following screenshot when you start to wave your hand in front
of the sensor.
Now we’ve built the two main components of our burglar alarm, and the only
thing left to do is to bring them both together. We’ll do that in the next section.
Chapter 11. Creating a Burglar Alarm with Email Notification • 196
report erratum • discuss
www.it-ebooks.info