Page 1 of 1

Simple Sketch Request

Simple Sketch Request

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.
Examples: "MacOS X - Your question", "MS Word - Your Tip or Trick".

Please note that switching to another language when reading a post will not work!
Posts will not have a translated counterpart.




RSS Feed

Home Forums Hardware Arduino Simple Sketch Request

This topic contains 3 replies, has 2 voices, and was last updated by  hans 2 weeks, 2 days ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • 11302

    bbitz01
    Participant

    Hello,

    First just let me say that I have no prior experience with coding on an Arduino. Recently I purchased an Arduino Nano (or a Chinese Knockoff, actually) and a 5M WS2812B LED strip to make some DIY Computer lights. By messing around with the examples from the Adafruit Neopixel library and the “Arduino – LEDStrip effects for NeoPixel and FastLED” post here on Tweaking4All, I was able to get a lot of effects that I liked. There was one, however, that I couldn’t quite figure out.

    I’d like a trail effect, much like the “Meteor Rain” effect in the aforementioned post, except I want it to restart back at the front of the strip smoothly instead of stopping at the end of the strip and then restarting again at the beginning, so that if you placed the two ends of the strip next to one another it would be a smooth loop. It would be essentially identical to the effect in this video: https://www.youtube.com/watch?v=R9wbYRV1AEM. I use the Adafruit Neopixel library, but if it’s easier to do with FastLED I’d be glad to switch.

    Thanks in advance. 

    11304

    hans
    Keymaster

    Hi bbitz01,

    sorry for the late reply – the holidays tend to gobble up a lot of time 

    So you basically want the effect to loop around?
    I do not have my Arduino near me, so I’m just doing a guess here … and this example will never leave the function

    void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
      setAll(0,0,0); int i = 0;
       while(true){
        // 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); i++; if(i=NUM_LEDS+NUM_LEDS) { i=0; }
      }
    }

    Like I said: I haven’t tested this, and I hope I didn’t make a typo 

    11306

    bbitz01
    Participant

    hans,

    If I put in your code correctly (which I’m not sure I did), I have this:

    #include <Adafruit_NeoPixel.h>
    #define PIN 3
    #define NUM_LEDS 81
    
    Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
    
    void setup() {
      strip.begin();
      strip.show(); // Initialize all pixels to 'off'
    }
    
    void loop() {
      meteorRain(0xff,0xff,0xff,10, 64, true, 30);
    }
    
    void meteorRain(byte red, byte green, byte blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {  
      setAll(0,0,0);
      int i = 0;
      
      while(true){
        
        // 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);
    
        i++;
        if(i=NUM_LEDS+NUM_LEDS) { i=0; }
      }
    }
    
    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();
    }
    

    Unfortunately this just lights up the first LED on my strip white. It doesn’t move, fade, etc. Perhaps I’ve put in the code wrong?

    And no need to worry about a late reply, we’ve all got things to do.

    11312

    hans
    Keymaster

    I’ll have to wait until I get home (on a holiday trip right now) to do some testing.
    I may have overlooked something. Unfortunately, I do not have my Arduino with me to do testing  …

    Happy New Year!

Viewing 4 posts - 1 through 4 (of 4 total)



You must be logged in to reply to this topic.