One more step with my project till is finished.I have 2 pir sensors (one upstairs and one downstairs) and I want to adapt a line of code found online with my line of code.
THIS IS MY CODE:↓
# include <Adafruit_NeoPixel.h>
# ifdef __AVR__
# include <avr/power.h>
# endif
#define LED_PIN 6
//#define LED_COUNT 664
#define LED_COUNT 664
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int k;
int val;
const int RELAY_PIN = 2;
int16_t cur_bright = 255;
unsigned long msecPeriod;
unsigned long msecLst;
int idx;
int idxMax;
int color;
//vars used for rainbow
long CurrentPixelHue = 0;
int CurrentPixel = 0;
int CurrentAni = 0;
unsigned long msecLastAniStep = 0;
int AniStepDelay = 5;
uint32_t CurrentColor = 0;
long RainBowDone = (5 * 65536);
bool AniDone = true;
int motionPin = 8; // PIR Input pin 8, UPSTAIRS
int motionPin2 = 7; // PIR 2 Input pin 7, DOWNSTAIRS
int senseMotion = 0; // variable to hold current state of motion detector
int senseMotion2 = 0; // 2nd variable for 2nd sensor
void setup()
{
pinMode(RELAY_PIN, OUTPUT);
pinMode(motionPin, INPUT); //PIRs declared as inputs
pinMode(motionPin2, INPUT);
Serial.begin(9600);
Serial.println("Ready");
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
strip.begin();
strip.show();
strip.setBrightness(255);
}
void loop(void)
{
PumpAni();
if (Serial.available ()) {
char c = Serial.read ();
switch (c) {
case 'z':
AniDone = false;
CurrentPixelHue = 0;
CurrentPixel = 0;
rainbow(2);
break;
case 'r':
Serial.println("RED");
AniDone = false;
CurrentPixel = 0;
colorWipe (strip.Color(255, 0, 0), 0);
break;
case 'g':
AniDone = false;
CurrentPixel = 0;
colorWipe (strip.Color(0, 255, 0), 0);
break;
case 'b':
AniDone = false;
CurrentPixel = 0;
colorWipe (strip.Color(0, 0, 255), 0);
break;
case 'w':
AniDone = false;
CurrentPixel = 0;
colorWipe (strip.Color(255, 255, 255), 0);
break;
case 'y':
AniDone = false;
CurrentPixel = 0;
colorWipe (strip.Color(255, 255, 0), 0);
break;
case 'o':
AniDone = false;
CurrentPixel = 0;
colorWipe (strip.Color(255, 0, 255), 0);
break;
case 't':
AniDone = false;
CurrentPixel = 0;
colorWipe (strip.Color(0, 255, 255), 0);
break;
case '.':
digitalWrite(RELAY_PIN, HIGH);
break;
case ',':
digitalWrite(RELAY_PIN, LOW);
break;
case '+':
cur_bright = cur_bright + 25;
if (cur_bright > 255)
cur_bright = 255;
strip.setBrightness(cur_bright);
break;
case '-':
cur_bright = cur_bright - 25;
if (cur_bright < 0)
cur_bright = 10;
strip.setBrightness(cur_bright);
break;
default:
break;
}
}
}
void PumpAni ()
{ //see if it's time for a step..
unsigned long currMilli = millis();
if (currMilli - msecLastAniStep >= AniStepDelay)
{
msecLastAniStep = currMilli;
switch (CurrentAni) {
case 0: colorWipe(CurrentColor, AniStepDelay); break;
case 1: rainbow(AniStepDelay); break;
}
}
}
void rainbow(int wait) {
AniStepDelay = wait;
CurrentAni = 1;
if (CurrentPixel < strip.numPixels())
{ CurrentPixel++;
}
else
{
CurrentPixel = 0;
if (CurrentPixelHue < RainBowDone) {
CurrentPixelHue += 256;
} else AniDone = true;
}
if (!AniDone)
{
int pixelHue = CurrentPixelHue + (CurrentPixel * 65536L / strip.numPixels());
strip.setPixelColor(CurrentPixel, strip.gamma32(strip.ColorHSV(pixelHue)));
strip.show();
}
}
void colorWipe(uint32_t color, unsigned long wait)
{
CurrentAni = 0;
AniStepDelay = wait;
CurrentColor = color;
if (!AniDone)
{
strip.setPixelColor(CurrentPixel, CurrentColor);
strip.show();
}
if (CurrentPixel < strip.numPixels())
{
CurrentPixel++;
} else
{
CurrentPixel = 0;
AniDone = true;
}
}
and the code I found on the internet
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(150, 6, NEO_GRB + NEO_KHZ800);
int motionPin = 8; // PIR Input pin 8, UPSTAIRS
int motionPin2 = 7; // PIR 2 Input pin 7, DOWNSTAIRS
int senseMotion = 0; // variable to hold current state of motion detector
int senseMotion2 = 0; // 2nd variable for 2nd sensor
void setup() {
pinMode(motionPin, INPUT); //PIRs declared as inputs
pinMode(motionPin2, INPUT);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
rainbow (30000);
rainbowCycle (30000);
strip.show();
}
void loop() {
senseMotion = digitalRead (motionPin); //set variables equal to what our sensors are reading (on or off)
senseMotion2 = digitalRead (motionPin2);
if (senseMotion == HIGH || senseMotion2 == HIGH) { // Testing both sensors at same time, if high, go to apropriate line.
if (senseMotion2 == HIGH) { // If Upstairs motion sensor is triggered...
colorWipe (strip.Color (30, 50, 255), 5); // White/blue color sweep going up stairs (Towards the arrow on the strip)
strip.show(); // This being enabled or not, doesnt seem to make a difference. NVM it looks like its more responsive when trying to activate the PIRs one after another
delay (10000); // Wait so someone can get to the bottom or top of stairs, will remove this when 2nd sensor is implemented with an interrupt
//colorChase (strip.Color(255, 25, 25), 5); // Color chase animations, red, green, blue indicating that the light are about to shut down
//colorChase2(strip.Color(25, 255, 25), 5);
//colorChase (strip.Color(25, 25, 255), 5);
scanner(200, 0, 255, 1);
colorWipe (strip.Color (0, 0, 0), 0); // This is needed for turn everything off, clear all pixels. More efficient with this here
strip.show();
}
if (senseMotion == HIGH) { // If motion sensor 2 (Downstairs) if high go to next line
colorWipe2 (strip.Color(30, 50, 255), 5);
strip.show();
delay(10000);
scanner(0, 200, 250, 1);
//colorChase (strip.Color(255, 25, 25), 5);
//colorChase2 (strip.Color(25, 255, 25), 5);
//colorChase (strip.Color(25, 25, 255), 5);
colorWipe2 (strip.Color(0, 0, 0), 0);
strip.show();
}
}
else { // no motion = no lights or animate til off
digitalWrite (motionPin, LOW); //turning motion pins low, doesnt seem to make a difference either.
digitalWrite (motionPin2, LOW);
//colorWipe (strip.Color(0, 0, 0), 30);
strip.show();
}
}
//Function time!
// LED animation fuction, Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// LED animate the other direction
void colorWipe2(uint32_t c, uint8_t wait) {
for(uint16_t i=149; i<strip.numPixels(); i--) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// Every other LED is on (from arduino forum)
void everyOther(uint32_t c, uint8_t wait) {
for(uint16_t k=0; k<strip.numPixels(); k++) {
strip.setPixelColor(2*k, c);
strip.show();
delay(wait);
}
}
//Opposite from above function (from arduino forum)
void everyOther2(uint32_t c, uint8_t wait) {
for(uint16_t k=0; k<strip.numPixels(); k++) {
strip.setPixelColor((2*k+1), c);
strip.show();
delay(wait);
}
}
//Rainbow fade effect (from adafruit)
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
// Chase one dot down the full strip.
void colorChase(uint32_t c, uint8_t wait) {
int i;
// Start by turning all pixels off:
for(i=0; i<strip.numPixels(); i++)
strip.setPixelColor(i, 0);
// Then display one pixel at a time:
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c); // Set new pixel 'on'
strip.show(); // Refresh LED states
strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
delay(wait);
}
strip.show(); // Refresh to turn off last pixel
}
// Chase one dot down the full strip, opposite direction.
void colorChase2(uint32_t c, uint8_t wait) {
int i;
// Start by turning all pixels off:
for(i=0; i<strip.numPixels(); i++)
strip.setPixelColor(i, 0);
// Then display one pixel at a time:
for(i=59; i<strip.numPixels(); i--) {
strip.setPixelColor(i, c); // Set new pixel 'on'
strip.show(); // Refresh LED states
strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
delay(wait);
}
strip.show(); // Refresh to turn off last pixel
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}