# Simple PIR triggering random LEDs

Hi Folks,

I know this is very simple but I need help!

Scenario: for a fun lighting display for kids I have about 5-8 LEDs that I am placing under the snow that will be triggered by a PIR and basically be snow fairies (YUP, its for girls). Stupid simple. STUCK!

I found a solid code for the randomization of the LEDs, but need to integrate the PIR sensor to trigger the LEDs for some duration (millis) and then stop input PIR “trips” for some other duration.

PIR is this one: https://robotdyn.com/catalog/biometric/pir_motion_sensor_v_1_0/

Code I have so far is this:

``````#include <stdio.h>
#define numberOfLEDs 18
long nextFlash[18];
int ledPin[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; // LED pins to use.
int ledState[18];
int inputPin = 0 // PIR sensor

void setup(){
pinMode(inputPin, INPUT);

for(int i = 0; i<numberOfLEDs; i++){
pinMode(ledPin[i],OUTPUT);
ledState[i] = LOW;
digitalWrite(ledPin[i], LOW); // all LEDs off
nextFlash[i] = millis() +random(10,700);
}
}

void loop(){
val = digitalRead(inputPin);  // read input value
if (val == HIGH) {            // check if the input is HIGH
for(int i = 0; i<numberOfLEDs; i++){
if(millis() > nextFlash[i]){
if(ledState[i] == LOW) ledState[i] = HIGH;
else ledState[i] = LOW;
digitalWrite(ledPin[i],ledState[i]);
nextFlash[i] = millis()+random(10,1250) ; // next toggle random time
//delay(millis()+random(10,13));
}
}
}
``````

TIA!

A PIR usually need a calibration period - stick that in the setup

A PIR usiualy embeds a duration potentiometer which will keep the trigger active for some time even if there is no activity detected

Simplest way to not re-trigger all the time immediately is to trap the PIR changing back to its resting state moment after a trigger and include a random delay() there. Delay is not great usually but since it looks like your code won’t do anything else than this, it’s not a problem

(careful on LED under the snow, they will possibly emit enough heat when lighting up which could melt the snow. Current and water don’t usually bode well)