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 - All LED e...
 
Share:
Notifications
Clear all

[Solved] Arduino - All LED effects in one Sketch

76 Posts
22 Users
0 Likes
18.2 K Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2675
Topic starter  

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()".


   
ReplyQuote
(@sweeterr)
New Member
Joined: 8 years ago
Posts: 3
 

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?


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

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?


   
ReplyQuote
(@sweeterr)
New Member
Joined: 8 years ago
Posts: 3
 

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


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

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();
}


   
ReplyQuote
(@sweeterr)
New Member
Joined: 8 years ago
Posts: 3
 

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.

   
ReplyQuote
(@toastycoast)
Active Member
Joined: 7 years ago
Posts: 4
 

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. 


   
ReplyQuote
(@toastycoast)
Active Member
Joined: 7 years ago
Posts: 4
 

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));
}




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

Awesome! 

Sorry for the late reply!


   
ReplyQuote
(@mekatronix)
New Member
Joined: 8 years ago
Posts: 1
 

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


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

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.


   
ReplyQuote
 nyo
(@nyo)
New Member
Joined: 7 years ago
Posts: 3
 

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).


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

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 


   
ReplyQuote
 nyo
(@nyo)
New Member
Joined: 7 years ago
Posts: 3
 

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!


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

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.


   
ReplyQuote
Page 1 / 6
Share: