int ledPin = 13; // choose the pin for the LED
int inputPin = 4; // choose the input pin (for PIR sensor)
boolean pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void read_PIR();
void setup()
{
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
digitalWrite(ledPin,LOW);
Serial.begin(9600);
}
void loop()
{
read_PIR();
}
void read_PIR()
{
val = digitalRead(inputPin); // read input value of PIR sensor
if (val == HIGH)
{
digitalWrite(ledPin, HIGH);
if (pirState == LOW)
{
Serial.println("Motion detected!");
pirState = HIGH;
}
}
else
{
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH)
{
Serial.println("Motion ended!");
pirState = LOW;
}
}
}