I am having trouble with learning how to work with a PIR sensor, I found some code for it but I cant seem to find a solution for my issue. what it does is blink the LED really fast going between "motion detected" and "motion Stopped". But I do not know how to get it to blink the LED if it senses motion and not if it does not. I cant figure out what to add or adjust so it will work properly.
int ledPin = 13; // choose the pin for the LED
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(ledPin, OUTPUT); // declare LED 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(ledPin, HIGH); // turn LED ON
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;
}
}
}