Hi,
I am planning to build Arduino 2 motion sensor based automatic staircase lighting system for my new home.
The project is like this:
Iafter seeing some similar project i bought
(1.)1Arduino Uno R3.
(2.) 2 Hc-Sr501 PIR motion sensor
(3.)WS2812B Neopixel led strip and other required components.
I found one project which was exactly as per my needs:
But unfortunately the sketch didn't work as expected(the lights just keep on flickering and nothing else happens and i have very limited knowledge of coding
Can anyone help me with the sketch part?.I have attached the necessary file(Scematic and .ino file) which i am using to build my project.
#include <Adafruit_NeoPixel.h>
#define PIN 6 // Digital output to LED
// Set up Variables
int NUM_STRIPS = 16; // number of strips/steps
int PIXELS_PER_STRIP = 6; // number of pixels in each strip
int MAX_BRIGHTNESS = 250; // LED brightness (0 - 255)
int TOP_SENSOR_PIN = 10; // PIR at the top of the stairs
int BOTTOM_SENSOR_PIN = 12; // PIR at the bottom of the stairs
unsigned long timeOut=600000; // timestamp to remember when the PIR was triggered.
int downUp = 0; // variable to rememer the direction of travel up or down the stairs
int alarmValueTop = LOW; // Variable to hold the PIR status
int alarmValueBottom = LOW; // Variable to hold the PIR status
int ledPin = 13; // LED on the arduino board flashes when PIR activated
// total number of pixels
int NUM_LEDS = NUM_STRIPS * PIXELS_PER_STRIP;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(ledPin, OUTPUT); // initilise the onboard pin 13 LED as an indicator
}
void loop() {
// Constantly poll the PIR at the top of the stairs
alarmValueTop = digitalRead(TOP_SENSOR_PIN);
// Constantly poll the PIR at the bottom of the stairs
alarmValueBottom = digitalRead(BOTTOM_SENSOR_PIN);
if (alarmValueTop == HIGH) {
timeOut=millis(); // Timestamp when the PIR is triggered. The LED cycle wil then start.
downUp = 1;
//clearStrip();
topdown(); // lights up the strip from top down
delay(1000);
}
if (alarmValueBottom == HIGH) {
timeOut=millis(); // Timestamp when the PIR is triggered. The LED cycle wil then start.
downUp = 2;
//clearStrip();
bottomup(); // lights up the strip from bottom up
delay(1000);
}
delay(100);
if (timeOut+20000 < millis() && timeOut+30000 < millis() && alarmValueTop != HIGH && alarmValueBottom != HIGH) { //switch off LED's in the direction of travel.
if (downUp == 1) {
darkenDOWN(); // Off
}
if (downUp == 2) {
darkenUP(); // Off
}
downUp = 0;
}
}
void topdown() {
digitalWrite(ledPin,HIGH);
delay(200);
digitalWrite(ledPin,LOW);
brightenDOWN();
}
void bottomup() {
digitalWrite(ledPin,HIGH);
delay(200);
digitalWrite(ledPin,LOW);
brightenUP();
}
void brightenDOWN(){
uint16_t a;
for (a = 0; a < NUM_STRIPS; a++) {
int start = PIXELS_PER_STRIP * a;
brighten(start, 1);
}
}
void brightenUP(){
uint16_t a;
for (a = NUM_STRIPS; a > 0; a--) {
int start = PIXELS_PER_STRIP * (a-1);
brighten(start, 1);
}
}
// 0 to 255
void brighten(uint16_t ledNum, uint16_t wait) {
uint16_t c, i;
for (c = 0; c < MAX_BRIGHTNESS; c++) {
for (i = ledNum; i < (ledNum + PIXELS_PER_STRIP); i++) {
strip.setPixelColor(i, c, c, c);
}
c = c+3;
strip.show();
delay(wait);
}
}
void darkenDOWN(){
uint16_t b;
for (b = 0; b < NUM_STRIPS; b++) {
int start = PIXELS_PER_STRIP * b;
darken(start, 1);
}
}
void darkenUP(){
uint16_t b;
for (b = NUM_STRIPS; b > 0; b--) {
int start = PIXELS_PER_STRIP * (b-1);
darken(start, 1);
}
}
// 255 to 0
void darken(uint16_t ledNum, uint16_t wait) {
uint16_t c, i;
for (c = MAX_BRIGHTNESS; c > 0; c--) {
for (i = ledNum; i < (ledNum + PIXELS_PER_STRIP); i++) {
strip.setPixelColor(i, c, c, c);
}
c--;
strip.show();
//delay(wait);
}
// trun off the strip completely
for (i = ledNum; i < (ledNum + PIXELS_PER_STRIP); i++) {
strip.setPixelColor(i, (0,0,0));
}
strip.show();
}
my LED Lights just keep on flickering and some random lights appears.
LED Stair Lights Arduino Code (5).ino (3.91 KB)
LED-Stair-Lighting.pdf (507 KB)