Page 1 of 1

Arduino – All LED effects in one Sketch

Arduino – All LED effects in one Sketch

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 Arduino – All LED effects in one Sketch

This topic contains 45 replies, has 12 voices, and was last updated by  hans 3 days, 14 hours ago.

Viewing 15 posts - 1 through 15 (of 46 total)
  • Author
    Posts
  • 6192

    hans
    Keymaster

    Based on a request with the Arduino LED Strip Effect article, here a sketch that includes all the effects discussed in that article.

    Please note that you might run out of memory, or not, depending on your Arduino.

    Attached, the FASTLed and the NEOPixel version.
    Both files are untested, but compile just fine, and you’d need to uncomment the effects you’d like to use in the “loop()“.

    Attachments:
    6199

    sweeterr
    Participant

    Hi

    Thanks for reply!

    Memory is not the problem: Arduino IDE 1.6.8 and ATmega2560

    I now have 5 sketch to work together in Framework. I am now happy!

    but, when I try to run your sketch “Blinking Halloween Eyes” alone in your Framework I get an error message: ‘HalloweenEyes’ was not declared in this scope.

    What is wrong here?

    6201

    hans
    Keymaster

    Glad it (mostly) works.

    ehm … in the sketches that I posted here, the function is defined … or did you mean the sketch in the original article?

    6208

    sweeterr
    Participant

    Yes, I mean in your original article. I get the same error in my project.

    6222

    hans
    Keymaster

    Hi Sweeterr!

    Well, I tried the code again, and it compiles just fine on my Arduino (Arduino IDE 1.6.6). (FASTLed example below)

    Are you sure there is not typo, or the wrong number of parameters passed?

    Try this code (might have to set NUM_LEDS to the number you’re using):

    #include "FastLED.h"
    #define NUM_LEDS 60 
    CRGB leds[NUM_LEDS];
    #define PIN 6 
    void setup()
    {
      FastLED.addLeds<WS2811, PIN, GRB>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    }
    void loop() {
      // Fixed:
      // HalloweenEyes(0xff, 0x00, 0x00, 1,4, true, 10, 80, 3000);
      // or Random:
      HalloweenEyes(0xff, 0x00, 0x00, 
                    1, 4, 
                    true, random(5,50), random(50,150), 
                    random(1000, 10000));
    }
    void HalloweenEyes(byte red, byte green, byte blue, 
                       int EyeWidth, int EyeSpace, 
                       boolean Fade, int Steps, int FadeDelay,
                       int EndPause) {
      randomSeed(analogRead(0));
      
      int i;
      int StartPoint = random( 0, NUM_LEDS - (2*EyeWidth) - EyeSpace );
      int Start2ndEye = StartPoint + EyeWidth + EyeSpace;
      
      for(i = 0; i < EyeWidth; i++) {
        setPixel(StartPoint + i, red, green, blue);
        setPixel(Start2ndEye + i, red, green, blue);
      }
      
      showStrip();
      
      if(Fade==true) {
        float r, g, b;
      
        for(int j = Steps; j >= 0; j--) {
          r = j*(red/Steps);
          g = j*(green/Steps);
          b = j*(blue/Steps);
          
          for(i = 0; i < EyeWidth; i++) {
            setPixel(StartPoint + i, r, g, b);
            setPixel(Start2ndEye + i, r, g, b);
          }
          
          showStrip();
          delay(FadeDelay);
        }
      }
      
      setAll(0,0,0); // Set all black
      
      delay(EndPause);
    }
    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();
    }

    6246

    sweeterr
    Participant

    I use a LED-ring like this: RGB LED Ring 16bit WS2812 5050 RGB LED. Purchased on ebay. so I use:
    #define NUM_LEDS 16
    I can see you’re wearing WS2811: FastLED.addLeds <WS2811, PIN, GRB> (leds, NUM_LEDS) .setCorrection (TypicalLEDStrip);
    I also tried to replace WS2811 with WS2812, but that will not work.
    I tried to run your sketch above. You can look at the picture what happens when I run this sketch.
    Maybe I should try using Ardunio IDE 1.6.6?

    I’m waiting for an LED strips with 60 LEDs from eBay.
    Attachments:
    7322

    toastycoast
    Participant

    Related to this one (thank you BTW!)….I’ve been able to compile everything as well as use them independently but I can’t enable them all at the same time and let it cycle through (like the Neopixel strand test). Doesn’t give an error, but many of the functions appear to need to loop to function properly while others have their own FOR statement that will loop them.

    I’ve tried adding FOR statements and have been able to get a few working, but FIRE has me stumped. Since it appears to have multiple functions the FOR statement isn’t working. I’m new to all this so any tips on getting this working would be greatly appreciated. 

    7327

    toastycoast
    Participant

    Figured it out…I needed to move the FOR command up into where the function is called. See below for the example. 

    void loop() {
      uint16_t j;
      for(j=0; j<1000; j++) {
      Sparkle(0xff, 0xff, 0xff, 0);
    }
    
      for(j=0; j<11; j++) {
      SnowSparkle(0x10, 0x10, 0x10, 20, random(100,1500));
    }
    
    
    
    
    7344

    hans
    Keymaster

    Awesome! 

    Sorry for the late reply!

    7369

    mekatronix
    Participant

    Hi Hans,
    and thanks so much for your work with these effects. MAGNIFICENT!
    I’m thinking about using several effects. One would be a kind of standard or idle effect. But then I want this effect to change into another effect upon pressing a button (as a receipt that the button has been pressed), and yet to other effects depending on input from sensors (e.g. serial values).  Can this be done, and if so, what would be the best way to go about it? Can’t figure it out.  Can you steer me in the right direction?

    Thanks, and keep up the good work

    Erik

    7371

    hans
    Keymaster

    Hi Erik,

    That is definitely possible. I guess the first step is to play with a button which “counts” when ever you click.
    So for example start with this Arduino Button example. It’s a good start to become familiar with the use of buttons.
    I’d expand that code by having a global counter go up, say to a max of 4 (assuming 4 LED effects).
    Once the counter wants to go beyond 4, make it reset to 1.

    Now the value of the counter can then be used in the loop to call a particular effect.

    The downside you might run into though is that pressing the button while an effect is running (like the Fire effect), might not be detected/registered. You’ll have to play with that and see how good/bad this works.

    Another option would be a multi position switch, or a rotary switch, where each position represents an effect.

    7384

    nyo
    Participant

    Hello everyone, this forum is great. There is a lot of useful information about LED strips!

    Erik,did you manage to resolve your problem with the button?

     I want to do the same but I have trouble on detecting the button press while an effect is running (just like hans said).

    7390

    hans
    Keymaster

    Welcome Nyo! And thanks for the compliment!

    There are two methods to address this:

    1) Start working with interrupts … which can be hard, confusing and sometimes result in conflicting code.

    2) Rewrite some of the effects to the do just one change and return to loop().

    The “easiest” seems option (1), but this can be rather confusing due to it’s more advanced (and non-standard) approach.
    Option (2) requires us to “save” the state of the LEDs so we continue where we left off when calling the function/effect again.

    Both will require a little work, and hopefully one of these days I find time to actually do this haha … like a v2 LED Effects 

    7396

    nyo
    Participant

    Hello Hans!

    Regarding (1): Do you know if connecting 2 arduinos pin may damage the board? I’m asking because I’ve (wrongly) soldered the pin 4 with my other components but this pin doesn’t allow interrupts on my arduino mini pro. So I want (must) connect the pin 4 with pin 3 and then read the interrupt to pin 3. 

    Number 2 is an interesting approach, I’ll try it! 

    Thank you!

    7398

    hans
    Keymaster

    Hi Nyo!

    Ehm,… interesting question. I don’t know for sure, but I think it will be OK. You might not be able to read a proper signal though.
    As far as I recall you might need GND to be shared as well (for reference). Sharing the same power supply might not be bad either.

    Maybe this post or this post helps.

Viewing 15 posts - 1 through 15 (of 46 total)



You must be logged in to reply to this topic.