Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Arduino LED Effects...
 
Share:
Notifications
Clear all

[Solved] Arduino LED Effects and PIR

4 Posts
2 Users
0 Reactions
1,847 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2791
Topic starter  

From a comment under the Arduino LED Effects article:

Question:

When PIR sensor activates first meteor rain climbs, and colorwipe follows it when done. The problem is meteor rain performs very slow on long strips even tough I set the setting fastest. It works fine for 2m led, but when I connect 7 meters it really slows down. Also the delay time between meteor rain and colorwipe increases when 7m led stip is connected, while it is acceptable with 2 meters.

Is there a way to make the meteor rain really fast with 7m led strip and reduce the delay between two effects? 

Bonus question: Is it possible to combine colorwipe effect to be leaded by meteor rain effect? I mean 2nd effect kicks in before the 1st one is finished. So the beginning of the colorwipe looks like meteor rain. 

 

Code:

Omg, I have been trying to make the corrections that you suggested for few days but couldn't make it work :( I attached the code I use, if you have time can you please edit it?

#include <Adafruit_NeoPixel.h>
#define PIN 2
#define NUM_LEDS 425
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

 
int ledPin = 2;                // choose the pin for the LED
int inputPin = 7;               // choose the input pin (for PIR sensor1)
int inputPin2 = 4;               // choose the input pin (for PIR sensor2)
int pirState = LOW;             // we start, assuming no motion detected
int pirState2 = LOW;            // we start, assuming no motion detected2
int val = 0;                    // variable for reading the pin status
 
void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(inputPin2, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop(){
  if (digitalRead(inputPin) == HIGH) {   // alttan çıkan kontolü
    Serial.println("alttan cikan!");
    meteorRain(0xB7,0x00,0xFE,5, 64, true, 0);
    colorWipe(0xFF,0xB1,0x6E, 10);
    delay(1000);
    colorWipe(0x00,0x00,0x00, 10);// wipe led off
    if (pirState == LOW) {  // we have just turned on
      Serial.println("alttan cikan");  // We only want to print on the output change, not state
      pirState = HIGH;
    } 
  }
 
  if (digitalRead(inputPin2) == HIGH) {
    Serial.println("üstten inen!");
    meteorRaindown(0xFF,0x00,0x00,5, 64, true, 0);
    colorWipedown(0xFF,0xB1,0x6E, 10);
    delay(1000);
    colorWipedown(0x00,0x00,0x00, 10);// wipe led off
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("üstten inen");
      // We only want to print on the output change, not state
      pirState = HIGH;
    } 
  }
}
void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  setAll(0,0,0);
 
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
   
   
    // fade brightness all LEDs one step
    for(int j=0; j<NUM_LEDS; j++) {
      if( (!meteorRandomDecay) || (random(10)>5) ) {
        fadeToBlack(j, meteorTrailDecay );        
      }
    }
   
    // draw meteor
    for(int j = 0; j < meteorSize; j++) {
      if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
        setPixel(i-j, red, green, blue);
      }
    }
   
    showStrip();
    delay(SpeedDelay);
  }
}
void meteorRaindown(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  setAll(0,0,0);
 
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
   
   
    // fade brightness all LEDs one step
    for(int j=0; j<NUM_LEDS; j++) {
      if( (!meteorRandomDecay) || (random(10)>5) ) {
        fadeToBlack(j, meteorTrailDecay );        
      }
    }
   
    // draw meteor
for(int j = 0; j < meteorSize; j++) {
  if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
    setPixel(NUM_LEDS-i-j, red, green, blue); // <-- change here
  } 
}
   
    showStrip();
    delay(SpeedDelay);
  }
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=0; i<NUM_LEDS; i++) {
      setPixel(i, red, green, blue);
      showStrip();
      delay(SpeedDelay);
  }
}
void colorWipedown(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=NUM_LEDS; i < -1; i--) {
      setPixel(i, red, green, blue);
      showStrip();
      delay(SpeedDelay);
  }
}

void fadeToBlack(int ledNo, byte fadeValue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
    // NeoPixel
    uint32_t oldColor;
    uint8_t r, g, b;
    int value;
   
    oldColor = strip.getPixelColor(ledNo);
    r = (oldColor & 0x00ff0000UL) >> 16;
    g = (oldColor & 0x0000ff00UL) >> 8;
    b = (oldColor & 0x000000ffUL);

    r=(r<=10)? 0 : (int) r-(r*fadeValue/256);
    g=(g<=10)? 0 : (int) g-(g*fadeValue/256);
    b=(b<=10)? 0 : (int) b-(b*fadeValue/256);
   
    strip.setPixelColor(ledNo, r,g,b);
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[ledNo].fadeToBlackBy( fadeValue );
 #endif  
}

void showStrip() {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.show();
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   FastLED.show();
 #endif
}

void setPixel(int Pixel, byte red, byte green, byte blue) {
 #ifdef ADAFRUIT_NEOPIXEL_H
   // NeoPixel
   strip.setPixelColor(Pixel, strip.Color(red, green, blue));
 #endif
 #ifndef ADAFRUIT_NEOPIXEL_H
   // FastLED
   leds[Pixel].r = red;
   leds[Pixel].g = green;
   leds[Pixel].b = blue;
 #endif
}

void setAll(byte red, byte green, byte blue) {
  for(int i = 0; i < NUM_LEDS; i++ ) {
    setPixel(i, red, green, blue);
  }
  showStrip();
}

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2791
Topic starter  

Well, first thing we should do is move to FastLED ... I did this quickly based on your code.
So ... this is most likely not the final answer to your question, but it will get us there eventually 😊 

Note: I wasn't able to test, since I do not have the hardware laying around, but it compiles without errors.

Cleaned up formatting and migrated to FastLED:

#define FASTLED_INTERNAL       // just used to mute the Pragma messages when compiling
#include "FastLED.h"
#define PIN 2
#define NUM_LEDS 425
CRGB leds[NUM_LEDS];

int inputPin = 7;              // choose the input pin (for PIR sensor1)
int inputPin2 = 4;             // choose the input pin (for PIR sensor2)
int pirState = LOW;            // we start, assuming no motion detected
int pirState2 = LOW;           // we start, assuming no motion detected2
int val = 0;                   // variable for reading the pin status
 
void setup() {
  FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  
  FastLED.clear();  // clear all pixel data
  FastLED.show();
  
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(inputPin2, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop(){
  if (digitalRead(inputPin) == HIGH) {   // alttan çıkan kontolü
    Serial.println("alttan cikan!");
    
    meteorRain(0xB7,0x00,0xFE,5, 64, true, 0);
    colorWipe(0xFF,0xB1,0x6E, 10);
    delay(1000);
    colorWipe(0x00,0x00,0x00, 10);// wipe led off
    
    if (pirState == LOW) {  // we have just turned on
      Serial.println("alttan cikan");  // We only want to print on the output change, not state
      pirState = HIGH;
    } 
  }
 
  if (digitalRead(inputPin2) == HIGH) {
    Serial.println("üstten inen!");
    
    meteorRaindown(0xFF,0x00,0x00,5, 64, true, 0);
    colorWipedown(0xFF,0xB1,0x6E, 10);
    delay(1000);
    colorWipedown(0x00,0x00,0x00, 10);// wipe led off
    
    if (pirState == LOW) {
      // we have just turned on
      Serial.println("üstten inen");
      // We only want to print on the output change, not state
      pirState = HIGH;
    } 
  }
}

void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  FastLED.clear();
 
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {   
    // fade brightness all LEDs one step
    for(int j=0; j<NUM_LEDS; j++) {
      if( (!meteorRandomDecay) || (random(10)>5) ) {
        leds[j].fadeToBlackBy( meteorTrailDecay );        
      }
    }
   
    // draw meteor
    for(int j = 0; j < meteorSize; j++) {
      if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
        leds[i-j] = CRGB(red, green, blue);
      }
    }
   
    FastLED.show();
    delay(SpeedDelay);
  }
}

void meteorRaindown(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
  FastLED.clear();
 
  for(int i = 0; i < NUM_LEDS+NUM_LEDS; i++) {
   
   
    // fade brightness all LEDs one step
    for(int j=0; j<NUM_LEDS; j++) {
      if( (!meteorRandomDecay) || (random(10)>5) ) {
        leds[j].fadeToBlackBy( meteorTrailDecay );        
      }
    }
   
    // draw meteor
    for(int j = 0; j < meteorSize; j++) {
      if( ( i-j <NUM_LEDS) && (i-j>=0) ) {
        leds[NUM_LEDS-i-j]=CRGB(red, green, blue); // <-- change here
      } 
    }
   
    FastLED.show();
    delay(SpeedDelay);
  }
}

void colorWipe(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=0; i<NUM_LEDS; i++) {
      leds[i] = CRGB(red, green, blue);
      FastLED.show();
      delay(SpeedDelay);
  }
}
void colorWipedown(byte red, byte green, byte blue, int SpeedDelay) {
  for(uint16_t i=NUM_LEDS; i < -1; i--) {
      leds[i] = CRGB(red, green, blue);
      FastLED.show();
      delay(SpeedDelay);
  }
}

 

Next thing I noticed is that your void loop() is a little bit mixed up it seems.

You defined 2 variables for the PIR states, but for example "pirState" has a value assigned and you set it to HIGH at some point, but never back to LOW or anything like that. I suspect this was intended to store the PIR state of the first PIR. Likewise the variable pirState2 was defined but never used. It is a little confusing what is going on there.

Maybe you're looking for something like this, which is just a wild guess, since I wasn't able to test this.

void loop(){
  pirState = digitalRead(inputPin);
  
  if (pirState == HIGH) {   // alttan çıkan kontolü
    Serial.println("alttan cikan!");
    
    meteorRain(0xB7,0x00,0xFE,5, 64, true, 0);
    colorWipe(0xFF,0xB1,0x6E, 10);
    delay(1000);
    colorWipe(0x00,0x00,0x00, 10);// wipe led off
  }

  pirState2 = digitalRead(inputPin2);
  
  if (pirState2 == HIGH) {
    Serial.println("üstten inen!");
    
    meteorRaindown(0xFF,0x00,0x00,5, 64, true, 0);
    colorWipedown(0xFF,0xB1,0x6E, 10);
    delay(1000);
    colorWipedown(0x00,0x00,0x00, 10);// wipe led off
  }
}

 

Note: I do realize that during testing and tinkering, code can become a little messy. 
When things do not quite work as expected, it is always a good idea to clean up and format code, so it becomes more readable. 😊 


   
ReplyQuote
(@Anonymous)
Joined: 1 second ago
Posts: 0
 

@hans Thank you for your help and time. 

I get the codes you shared on the effects post and try to change them such as meteor rain up and down and PIR sensors etc. Did not write them myself since my knowledge about coding is limited, just copied them from another projects that is close to what I want. 

If you see something that is odd, it's probably my mistake not intended, but the code I posted seems to be working without any issues (or I haven't noticed any mulfunction yet) just slow on long strips.

About the PIR states, I have one at the bottom of the stairs and another at the top. (I wrote "alttan cikan and üstten inen to find out which one is which meaning walking up or down) So their codes teorically should be the same, just inverse. I haven't noticed the lack of usage for PIR state2   🙄 

I'll try the codes that you edited for me in a short time and see if it works as planned.

Thanks again appriciate it.

This post was modified 3 years ago 2 times by Anonymous

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2791
Topic starter  

Cool - let me know if you run into a problem or not 😊 


   
ReplyQuote
Share: