EasyManuals Logo

Arduino NG User Manual

Arduino NG
311 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #212 background imageLoading...
Page #212 background image
-
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 youve 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 weve 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 sensors 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 weve built the two main components of our burglar alarm, and the only
thing left to do is to bring them both together. Well do that in the next section.
Chapter 11. Creating a Burglar Alarm with Email Notification 196
report erratum discuss
www.it-ebooks.info

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Arduino NG and is the answer not in the manual?

Arduino NG Specifications

General IconGeneral
BrandArduino
ModelNG
CategoryMotherboard
LanguageEnglish

Related product manuals