Hi,
I really need some help for my project please. What I'm trying to do is using a PIR sensor get a LED strip to fade on with movement and then if there is no movement after 5 mins fade off.
Thank you so much I really appreciate any help!!
here is my code
#include <Adafruit_NeoPixel.h>
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16
#define LED_PIN 8
#define MAXLEVEL 100 // do not exceed 150!!!
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
// See the explanations from the Adafruit library
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LED_PIN, NEO_GRBW + NEO_KHZ800);
// Some pin defs for the motion click
#define MOTION_PIN 2 // interrupt pin for motion sensor
int MotionDetected;
//int val = 0;
long firstTimeNoMovement;
long timeSinceNoMovement;
boolean movement;
boolean sensorValue;
//int pirState = on;
void setup() {
pixels.begin(); // This initializes the NeoPixel library
//attachInterrupt(0, MotionIrq, RISING); // ATMEGA2560 - Pin D2 is INT0
//MotionDetected = 0; // initial setting
pinMode(MOTION_PIN, INPUT);
pinMode(LED_PIN, OUTPUT); // set enable pin as output
Serial.begin(9600);
// turn off all leds
for (int i = 0; i < NUMPIXELS; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
}
}
void loop() {
// *** Read sensor value: on/off, or true/false, 0/1, etc
sensorValue = digitalRead(MOTION_PIN);
if (movement == true) {
for (int j = 1; j < MAXLEVEL; j++) {
for (int i = 0; i < NUMPIXELS; i++) {
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(0, 0, j)); // Bright blue color.
pixels.show(); // This sends the updated pixel color to
movement = true;
}
}
} else if (movement == true) {
firstTimeNoMovement = millis();
movement = false;
// Check time passed since no movement detected
long currentTime = millis();
long timeSinceNoMovement = currentTime - firstTimeNoMovement;
}
if (timeSinceNoMovement > 60000) {
for (int j = MAXLEVEL; j > 0; j--) {
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, pixels.Color(0, 0, j - 1));
pixels.show();
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); // Black/ off
}
}
}
}
int getTimeInMillis(int sec) {
return sec * 1000;
}