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 most likely no translated for the post!




Arduino - All LED e...
 
Share:
Notifications
Clear all

Arduino - All LED effects in one Sketch

Page 3 / 6

(@pierre)
New Member
Joined: 4 years ago
Posts: 0
 

Hello juanchu23 and Hans

 How I've solved and it works, look how long a particular effect. Copier this effect as many times as the time you want to lengthen and paste it under the first next line of the effect, kinda like the effects together, divide or repeat at a different place does not work. VB1 

What probably can put an effect on different places is just the effect on the other place to a new name, but you should not forget to make a copy in the same void and the name. Might work, not tested. VB. 2

Example:

VB1

theaterChase(0xff,0,0,50);

theaterChase(0xff,0,0,50); 

theaterChase(0xff,0,0,50);

FadeInOut(0xff, 0x00, 0x00); // red

FadeInOut(0xff, 0xff, 0xff); // white

FadeInOut(0x00, 0x00, 0xff); // blue

Slower:

Strobe(0xff, 0x77, 0x00, 10, 100, 1000);

Strobe(0xff, 0x77, 0x00, 10, 100, 1000);

Fast:

Strobe(0xff, 0xff, 0xff, 10, 50, 1000);

VB2

theaterChase(0xff,0,0,50);

colorWipe(0x00,0x00,0x00, 50); 

etc.

theChase(0xff,0,0,50); (copy van theaterChase(0xff,0,0,50);)

Now to the void portion:

void theChase(byte red, byte green, byte blue, int SpeedDelay) { ….

(copy van theaterChase)


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1840
Topic starter  

Awesome - good to hear that Pierre! 

Timing is probably not the most accurate, but for most purposes probably sufficient.
When using timers, one would let an effect loop until the timer reaches it's goal time, which would then abort an effect and go to the next one.
I haven't had time these past weeks to play with it, but maybe it's something for my next LED project 


ReplyQuote
(@1234abcd)
New Member
Joined: 3 years ago
Posts: 0
 

"I have a question: how can make a effect running in a delimited time (ej. 10 seconds)?"

You could use FastLED's "EVERY_N_MILLISECONDS" or "EVERY_N_SECONDS" for that, see the library wiki on GitHub. The good thing with this is that you eliminate delay(time), because that blocks the code from executing anything else during that time.

I don't know why the code here breaks up like this when using shift + enter for a new line, it should be one code block.

 

void loop() {
// Other things that should run periodically inside loop() go here
EVERY_N_MILLISECONDS(time) { // Or EVERY_N_SECONDS(time)
function(); // Call to an effect function
}
} // loop()

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1840
Topic starter  

Thanks 1234abcd ... nice catch!

The breaking up of the code probably is caused by entering the code after pressing the "Source code" button. As a workaround I usually enter the code first, select it, and then click the "Source Code" button. It may depend on the OS if SHIFT+Enter works (on my Mac it does).


ReplyQuote
(@1234abcd)
New Member
Joined: 3 years ago
Posts: 0
 

Hm, I did just that. Pasted code directly from the Arduino IDE underneath what I wrote, then used your button (Crome and Safari). Then I when I put the cursor in the now block-formatted code and want to create an empty line, I get separate code blocks, not an empty line inside the code.


ReplyQuote


 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1840
Topic starter  

Yeah, one of these days I'll need to go find me a bett6er Forum (and editor) ... 

I usually paste the code, select the code and then press the button. When I need to add lines Shift+Enter ... I use Google Chrome by the way under macOS.


ReplyQuote
(@1234abcd)
New Member
Joined: 3 years ago
Posts: 0
 

Just to add for completeness; according to Mark Kriegsman, who does the FastLED code, one can use MILLIS, SECONDS, MINUTES, HOURS and DAYS to time animations with the EVERY_N_# approach. One can apparently also use multiple timers of that sort in an Arduino sketch to time various animations concurrently.

Also, Stefan Petrick shows on GitHub how one can quite simply overlay one animation (effect) with another; that really opens up interesting possibilities (using one timer for one animation, using another timer for another animation, and then overlaying them).

ReplyQuote
(@1234abcd)
New Member
Joined: 3 years ago
Posts: 0
 

Here is a condensed version of Stefan Petrick's suggestion of how two animations run independently on a single strip; short red stripes in one direction, longer green stripes in the other direction. The idea is - why not simply use more than just one CRGB object? Two EVERY_N_# timers would add independent speeds also. Think a slow sunset to night city glow animation where occasionally white stars are twinkling on top...

#include <FastLED.h>
const byte pinData = 3;
const byte pinClock = 4;
const byte ledCount = 144;
CRGB leds[ledCount];
CRGB leds2[ledCount];
CRGB leds3[ledCount];
void setup() {
  FastLED.addLeds<APA102, 3, 4, BGR>(leds, ledCount);
}
void loop() {
  animationA();
  animationB();
  for (int i = 0; i < ledCount; i++) {
    leds = blend( leds2, leds3, 127 ); // Mix both animations
  }
  FastLED.show();
}
void animationA() { // Red stripes forwards
  for (uint16_t i = 0; i < ledCount; i++) {
    uint8_t red = (millis() / 3) + (i * 5);
    if (red > 64) red = 0;
    leds2 = CRGB(red, 0, 0);
  }
}
void animationB() { // Green stripes backwards
  for (uint16_t i = 0; i < ledCount; i++) {
    uint8_t green = (millis() / 3) + (i * 5);
    if (green > 128) green = 0;
    leds3[ledCount - i] = CRGB(0, green, 0);
  }
}

ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1840
Topic starter  

Awesome, and good idea to mix two (or more) arrays. I may need that for one of my new LED strip ideas 


ReplyQuote
 mack
(@mack)
New Member
Joined: 3 years ago
Posts: 0
 

This Loop Show LED Again and Again 

void loop() {

  animationA();

  animationB();

  for (int i = 0; i < ledCount; i++) {

    leds = blend( leds2, leds3, 127 ); // Mix both animations

  }

  FastLED.show();

}

void animationA() { // Red stripes forwards

  for (uint16_t i = 0; i < ledCount; i++) {

    uint8_t red = (millis() / 3) + (i * 5);

    if (red > 64) red = 0;

    leds2 = CRGB(red, 0, 0);

  }

}

void animationB() { // Green stripes backwards

  for (uint16_t i = 0; i < ledCount; i++) {

    uint8_t green = (millis() / 3) + (i * 5);

    if (green > 128) green = 0;

    leds3[ledCount - i] = CRGB(0, green, 0);

  }

}


ReplyQuote


(@kevlarr)
New Member
Joined: 2 years ago
Posts: 0
 

Awesome sketch hans! Was looking for the better part of a day for something EXACTLY like this to run my LEDs for my Christmas lights. I'm a total newbie when it comes to sketches but I have managed to figure out how to change colors and re-arrange the effects for a pretty cool display. So far I've got 

"CylonBounce(0xff, 0, 0, 4, 10, 50); //red

   colorWipe(0xff,0,0, 50); //red

   RunningLights(0xff,0,0, 50); // red

   CylonBounce(0xff, 0, 0, 4, 10, 50); //red

   CylonBounce(0x00,0x80,0x00, 4, 10, 50); //green

   colorWipe(0x00,0x80,0x00, 50); //green

   RunningLights(0x00,0x80,0x00, 50); //green

   CylonBounce(0x00,0x80,0x00, 4, 10, 50); //green

   CylonBounce(0xff,0xff,0xff, 4, 10, 50); //white

   colorWipe(0xff,0xff,0xff, 25); // white

   RunningLights(0xff,0xff,0xff, 50); // white

   CylonBounce(0xff,0xff,0xff, 4, 10, 50); //white "

 But I've run into a couple of problems with some effects. 

Twinkle, Sparkle and SnowSparkle only last for a second before it switches to the next effect. Is there a way to make it pause on those effects for say a minute or so?

Also if I use the bouncing balls effect (which looks awesome with red, green and white) it gets stuck there and never loops again.

Any help or if you could just point me in the right direction would be greatly appreciated.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1840
Topic starter  

Hi Kevlarr,

Thank you very much for the compliment ...

To let one of the effects you mentioned (Twinkle, Sparkle, SnowSparkle) run for example for a minute, you can call the function multiple times.

For example, in the example we simply call Twinkle() as such;

void loop() {
  Twinkle(0xff, 0, 0, 10, 100, false);
}

You could (if time is not important) run Twinkle several times in a for-loop, in this example 10 times, but you can modify that;

void loop() {
  for(int i; i<=10; i++) { Twinkle(0xff, 0, 0, 10, 100, false); }
}

If time is more critical, you can use the millis() function. Note that this keeps repeating the function until a certain interval value has been reached. This does not mean that the loop stops exactly at the indicated time. With that I mean; say you want the function to repeat for 2 seconds, and function call actually takes 1.5 seconds, then the loop will run the function twice. 

void loop() {
  unsigned long starttime; 
  starttime = millis();
  while ((millis() - starttime) <= 10000) // do this loop for up to 10000mS = 10 seconds
  {
    Twinkle(0xff, 0, 0, 10, 100, false);
  }
}

I have not tested this code, but I'm pretty sure it will work just fine.

Hope this helps 


ReplyQuote
(@kevlarr)
New Member
Joined: 2 years ago
Posts: 0
 

Thanks so much, that worked perfectly! Now if I could just figure out why it gets stuck on the bounce effect.


ReplyQuote
 Hans
(@hans)
Noble Member Admin
Joined: 8 years ago
Posts: 1840
Topic starter  

Awesome! 

So some of the effects are "endless" due to the nature of the effect. Bouncing balls is one of those.
I did publish an article where all effects are placed in one sketch, with the ability to toggle between effects with a button. There I had the same problem as well. You can read about it how I resolved that here. In essence we need to define where the effect "ends" and make it leave the function. You'll find the code example there as well.

Hope this helps 


ReplyQuote
(@kevlarr)
New Member
Joined: 2 years ago
Posts: 0
 

Thanks again Hans! 


ReplyQuote


Page 3 / 6

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: