// https://forum.arduino.cc/t/stair-lights-with-pir-at-either-end/1280361
#include <FastLED.h>
#define NUM_LEDS 190
#define DATA_PIN 2
#define COLOR_ORDER GRB
#define CHIPSET WS2812B
#define BRIGHTNESS 255
CRGB leds[NUM_LEDS];
#define MOTION_SENSOR_1_PIN 12 // Arduino pin connected to the OUTPUT pin of motion sensor
#define MOTION_SENSOR_2_PIN 11
int motion_state_1 = LOW; // current state of motion sensor's pin
int motion_state_2 = LOW;
int prev_motion_state_1 = LOW; // previous state of motion sensor's pin
int prev_motion_state_2 = LOW; // previous state of motion sensor's pin
void setup() {
Serial.begin(115200);
FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
pinMode(MOTION_SENSOR_1_PIN, INPUT); // set arduino pin to input mode
pinMode(MOTION_SENSOR_2_PIN, INPUT);
}
void loop() {
prev_motion_state_1 = motion_state_1; // store old state
prev_motion_state_2 = motion_state_2; // store old state
motion_state_1 = digitalRead(MOTION_SENSOR_1_PIN); // read new state
motion_state_2 = digitalRead(MOTION_SENSOR_2_PIN);
if (prev_motion_state_1 == LOW && motion_state_1 == HIGH) { // pin state change: LOW -> HIGH
Serial.println("Motion (1) detected!");
// turn on the led strip
for (int i = 0; i < NUM_LEDS / 2 + 1; i++) {
leds[NUM_LEDS / 2 + i] = CRGB::White;
leds[(NUM_LEDS / 2) - 1 - i] = CRGB::White;
FastLED.show();
delay(10);
}
} else if (prev_motion_state_1 == HIGH && motion_state_1 == LOW) { // pin state change: HIGH -> LOW
Serial.println("Motion (1) stopped!");
for (int j = 0; j < 255; j++) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(255 - j, 255 - j, 255 - j);
}
FastLED.show();
}
}
if (prev_motion_state_2 == LOW && motion_state_2 == HIGH) { // pin state change: LOW -> HIGH
Serial.println("Motion (2) detected!");
// turn on the led strip
for (int i = 0; i < NUM_LEDS / 2 + 1; i++) {
leds[NUM_LEDS / 2 + i] = CRGB::White;
leds[(NUM_LEDS / 2) - 1 - i] = CRGB::White;
FastLED.show();
delay(10);
}
} else if (prev_motion_state_2 == HIGH && motion_state_2 == LOW) { // pin state change: HIGH -> LOW
Serial.println("Motion (2) stopped!");
for (int j = 0; j < 255; j++) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(255 - j, 255 - j, 255 - j);
}
FastLED.show();
}
}
}