/*
PIR sensor tester with 3 LEDs (sequential)
*/
int ledPinA = 13; // choose the pin for LED A
int ledPinB = 12; // choose the pin for LED B
int ledPinC = 11; // choose the pin for LED C
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPinA, OUTPUT); // declare LED A as output
pinMode(ledPinB, OUTPUT); // declare LED B as output
pinMode(ledPinC, OUTPUT); // declare LED C as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPinA, HIGH); // turn LED A ON
delay(500); // wait for a while
digitalWrite(ledPinA, LOW); // turn LED A OFF
delay(100); // brief pause before next LED
digitalWrite(ledPinB, HIGH); // turn LED B ON
delay(500); // wait for a while
digitalWrite(ledPinB, LOW); // turn LED B OFF
delay(100); // brief pause before next LED
digitalWrite(ledPinC, HIGH); // turn LED C ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
delay(1000); // time for LED C to remain ON
digitalWrite(ledPinC, LOW); // turn LED C OFF
} else {
if (pirState == HIGH) {
// we have just turned off
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}