Go Down

Topic: LED Strip on Stair - Waterfall Effect help need (Read 165 times) previous topic - next topic

emersonvier

Hi I need some direct how to change the direction of the Waterfall Effect on the code below, I install the LED in a different position now my Waterfall running to upside, is not easy to change the LED Strip position now.


Code: [Select]
// "Bling" up your Staircase By Simon Jowett November 2014
// Thanks to the Neopxel Library by Adafruit


#include <Adafruit_NeoPixel.h>

#define PIN 6

int LEDQuantity = 76;


Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDQuantity, PIN, NEO_GRB + NEO_KHZ800);


// Set up Variables
 unsigned long timeOut=60000; // timestamp to remember when the PIR was triggered.
 int downUp = 0;              // variable to rememer the direction of travel up or down the stairs
 int alarmPinTop = 10;        // PIR at the top of the stairs
 int alarmPinBottom =11;      // PIR at the bottom of the stairs
 int DoorSW = 9;
 int alarmValueTop = LOW;    // Variable to hold the PIR status
 int alarmValueBottom = LOW; // Variable to hold the PIR status
 int alarmDoorSW = HIGH;
 int ledPin = 13;           // LED on the arduino board flashes when PIR activated
 int LDRSensor = A0;        // Light dependant resistor
 int LDRValue = 0;          // Variable to hold the LDR value
 int colourArray[350];      // An array to hold RGB values - LEDQuantity * 3
 int change = 1;            // used in 'breathing' the LED's
 int breathe = 0;           // used in 'breathing' the LED's

void setup() {
   strip.begin();
   strip.setBrightness(40); //adjust brightness here
   strip.show(); // Initialize all pixels to 'off'
   Serial.begin (9600);  // only requred for debugging
   pinMode(ledPin, OUTPUT);  // initilise the onboard pin 13 LED as an indicator
   pinMode(alarmPinTop, INPUT_PULLUP);     // for PIR at top of stairs initialise the input pin and use the internal restistor
   pinMode(alarmPinBottom, INPUT_PULLUP);  // for PIR at bottom of stairs initialise the input pin and use the internal restistor
   pinMode(DoorSW, INPUT_PULLUP);  // for Door SW
   delay (2000); // it takes the sensor 2 seconds to scan the area around it before it can
   //detect infrared presence.
   
   for (int i=0 ;i < 299; i++)  { // initilise the colourArray to zero WHY 299
   colourArray[i]=0;
   }
}

void loop() {

  alarmValueTop = digitalRead(alarmPinTop);         // Constantly poll the PIR at the top of the stairs
  alarmValueBottom = digitalRead(alarmPinBottom);   // Constantly poll the PIR at the bottom of the stairs
  alarmDoorSW = digitalRead(DoorSW);                // Constantly poll Door Sensor -  read the Door Sensor input pin:

   
  //LDRValue = analogRead(LDRSensor);
  LDRValue = 700; //BYPAASS LDR SENSOR
  //Serial.println(LDRValue);
 
 
   // idle state - 'breathe'
  if (timeOut+57000 < millis()) {        // idle state - 'breathe' the top and bottom LED to show program is looping
   
     breathe = breathe + change;
     strip.setPixelColor(0,0,0,breathe);
     strip.setPixelColor((LEDQuantity-1),0,0,breathe); //Add you last LED #
     strip.show();
     if (breathe == 100 || breathe == 0) change = -change;      // breathe the LED from 0 = off to 100 = fairly bright
     if (breathe == 100 || breathe == 0) delay (300);           // Pause at beginning and end of each breath
     delay(25);
  }
 // End of idle state - 'breathe'
 
  if (LDRValue > 600) {        // only switch on LED's at night when LDR senses low light conditions - you may have to change the number for your circumstances!
 
       
    if (alarmValueTop == HIGH && downUp != 2)  {      // the 2nd term allows timeOut to be contantly reset if one lingers at the top of the stairs before decending but will not allow the bottom PIR to reset timeOut as you decend past it.
      timeOut=millis();  // Timestamp when the PIR is triggered.  The LED cycle wil then start.
      downUp = 1;
      topdown();         // lights up the strip from top down
    }

    if (alarmValueBottom == HIGH && (downUp != 1))  {    // the 2nd term allows timeOut to be contantly reset if one lingers at the bottom of the stairs before decending but will not allow the top PIR to reset timeOut as you decend past it.
    //if (alarmValueBottom == HIGH && downUp != 1)  {    // the 2nd term allows timeOut to be contantly reset if one lingers at the bottom of the stairs before decending but will not allow the top PIR to reset timeOut as you decend past it.
      timeOut=millis();    // Timestamp when the PIR is triggered.  The LED cycle wil then start.
      downUp = 2;
      bottomup();         // lights up the strip from bottom up
    }
   
   
    if (timeOut+10000 < millis() && timeOut+15000 < millis()) {    //switch off LED's in the direction of travel.
       if (downUp == 1) {
         
          colourWipeDown(strip.Color(0, 0, 0), 50); // Off,  The 1st 3 numbers are the RGB colour and the 4th (40ms in this example) is the delay between updates
       }
       if (downUp == 2)  {
        colourWipeUp(strip.Color(0, 0, 0), 50);   // Off, The 1st 3 numbers are the RGB colour and the 4th (40ms in this example) is the delay between updates
       }
      downUp = 0;
     
      //   for (int i=0 ;i < 299; i++)  {          // Depending on your preference you may want to include this loop to clear out the colourArray
      //    colourArray[i]=0;
      // }
    }
   
    if (timeOut+15000 < millis() && timeOut+54999 > millis()) waterfall();    // Waterfall effect to play between these times after a PIR trigger.
       
    if (timeOut+55000 < millis() && timeOut+56999 > millis()) fade();   // Fade/switch off LED's
  }
}

 void topdown() {
    Serial.println ("detected top");                // Helpful debug message
    colourWipeDown(strip.Color(100, 100, 100), 40);    // Warm White The 1st 3 numbers are the RGB colour and the 4th (40ms in this example) is the delay between updates
    for(int i=0; i<3; i++) {                        // Helpful debug indication flashes led on Arduino board twice
      digitalWrite(ledPin,HIGH);
      delay(200);
      digitalWrite(ledPin,LOW);
      delay(200);
    }
 }

 void bottomup() {
    Serial.println ("detected bottom");          // Helpful debug message
    colourWipeUp(strip.Color(100, 100, 100), 40);   // Warm White
    for(int i=0; i<4; i++) {                     // Helpful debug indication flashes led on Arduino board twice
      digitalWrite(ledPin,HIGH);
      delay(200);
      digitalWrite(ledPin,LOW);
      delay(200);
    }
  }



 // Fill the dots one after the other with a color
 void colourWipeDown(uint32_t c, uint8_t wait) {
   for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
 }

 // Fill the dots one after the other with a color
 void colourWipeUp(uint32_t c, uint8_t wait) {
   for(uint16_t i=strip.numPixels(); i < -1; i--) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
 }

 // Play the Waterfall effect
 void waterfall(){
   
   
    for(int i=((LEDQuantity*3)-4); i>-1; i--) {            // Shift the RGB colours down in the colourArray[]
        colourArray[i+3]=colourArray[i];
        }
      colourArray[0] = random(0,20);        // Genearate the red component of the 1st LED at the top of the stairs
      colourArray[1] = random(0,40);        // Genearate the green component of the 1st LED at the top of the stairs
      colourArray[2] = random(10,70);       // Genearate the blue component of the 1st LED at the top of the stairs
   
      for (int k=0; k<(LEDQuantity*3); k=k+3)   {       // Set and send the colours to the strip
       uint32_t c = strip.Color(colourArray[k],colourArray[k+1],colourArray[k+2]);
       strip.setPixelColor(((k+3)/3)-1,c);
       }
   
      strip.show();      // and display the result
      delay(35);         // delay to simulate running water
 }
 
 // Play the LED fade out
 void fade(){
  //Serial.println ("Play the LED fade out");
 for (int j = 0; j <70; j++) {
   for(int i=((LEDQuantity*3-1)); i>-1; i--) {
     colourArray[i]=colourArray[i]-1;      // reduce intensity of light by 1
     if (colourArray[i] <= 0 ) colourArray[i] = 0;
   }
   for (int k=0; k<(LEDQuantity*3); k=k+3)   {
     uint32_t c = strip.Color(colourArray[k],colourArray[k+1],colourArray[k+2]);
     strip.setPixelColor(((k+3)/3)-1,c);
   }
  strip.show();
  delay(60);
  //Serial.println ("strip.show");
  }
 breathe = 0;
 change = 1;
 }

Grumpy_Mike

Code: [Select]
)  { // initilise the colourArray to zero WHY 299
Well it is your code so you tell me why.
You seem to be writing beyond the bounds of the array and that could cause all sorts of problems.

Quote
how to change the direction of the Waterfall Effect
When ever you set an LED, do not use the number but use the maximum number of LEDs minus the number you were going to use.

Go Up