Modes Timing Question! I'm using the latest version of FastLED.

Modes Timing Question!

I’m using the latest version of FastLED.

I want to automatically switch from one mode to the next at a certain time point in a song. I’ve been doing this with for() loops, but it’s kind of tricky and problematic since all the modes take a different amount of time to run:

void autoplaymode() {
for(int i=0; i<60; i++) {Rainbow();}
for(int i=0; i<10; i++) {Solid();}
for(int i=0; i<35; i++) {Gradient();}

Is there a better way to do this? I’m looking at the ChangeMe function in Andrew Tuline’s funkboxing update:

void ChangeMe()
{
uint8_t secondHand = (millis() / 10000) % 60;
static uint8_t lastSecond = 99;

if( lastSecond != secondHand) {
lastSecond = secondHand;
if (secondHand == 0) {twinkrun = 1; thisrot = 1; thiscutoff=254; STEPS=8;}
if (secondHand == 5) {thisrot = 0; thisdir=1;}
if (secondHand == 10) {HUE = 255;}
if (secondHand == 15) {twinkrun = 0;}
if (secondHand == 20) {STEPS = 16;}
}

Is there a way to use something like this to change modes? My main loop right now looks something like this:

void loop() {
switch (ledMode) {
case 999: break;
case 0: Rainbow(); break;
case 1: Solid(); break;
case 2: Gradient(); break;

And I’d love to have case 0 run at 0 seconds, case 1 at 5 seconds, case 2 at 10 seconds, etc. It seems like it should work but I must be missing something because it DOESN’T.

Here’s my attempt at the combined code… the modes play fine but when I click to case 3, no lights come on at all:

void loop() {
switch (ledMode) {
case 999: break;
case 0: Rainbow(); break;
case 1: Solid(); break;
case 2: Gradient(); break;
case 3: autoplaymode(); break;
}
}

void ChangeMode()
{
uint8_t secondHand = (millis() / 10000) % 60; // Increase this if you want a longer demo.
static uint8_t lastSecond = 99; // Static variable, means it’s only defined once. This is our ‘debounce’ variable.

if( lastSecond != secondHand) {
lastSecond = secondHand;
if (secondHand == 0) {Rainbow();}
if (secondHand == 5) {Solid();}
if (secondHand == 10) {Gradient();}
}
}

void autoplaymode()
{
ChangeMode();
show_at_max_brightness_for_power();
delay_at_max_brightness_for_power(loopdelay*2.5);
Serial.println(LEDS.getFPS());
}

Thanks so much for any advice!!

Stay tuned. New sample code is on the way!

Thanks Mark!!

Looking forward to seeing it, @Mark_Kriegsman !
Has it maybe something to do with

while ( millis() < timemark1 ) {
animation1();
}

or is it more sophisticated?

OK, so what’s coming (a bit later today, I hope) is a ‘playlist’ controller that will help you do the thing you’re shooting for. But as a warm-up take a quick look at this, which is going to become one of the new FastLED “Demo Reels”.

This doesn’t have a scheduled playlist of animations, but it does start by putting the animations into an array (of function pointers – which you can mostly ignore) so that they can be called by pattern number.

The code coming later shows the next step of moving to a scheduled playlist with different times for each animation. I need to clean the code up a bit for comprehensibility – and help a certain middle-schooler with her homework.

An array of functions - that appears brilliant to a coding newbie like me. Just great and the fulfillment of a unspoken dream of mine!

OK, here’s the sample code to work from. It’s based on some things I’ve been developing to try and make it easier to say “Do THIS for a minute and then do THAT for ten seconds and then (etc)”. I put in a few things that make it more relevant for performance-oriented animations playlists, e.g., one-time through the playlist and then stop (or loop forever), and the ability to trigger the start of the animation playlist from an external trigger, like a button. Should be helpful for music+LED type performances, I hope.

You’re my hero!!! :slight_smile:

I put a few more notes here https://plus.google.com/112916219338292742137/posts/1ByAGPKLLDp
I’m definitely thinking about what’s needed for music-sync’d performances now. I’m not sure that second-level resolution is good enough, but maybe milliseconds is too picky. I sort of want to define everything in terms of beats and measures, but then I get a headache thinking about all the issues. I’ll sleep on it, but it’s a fun area to explore – thank you for the inspiration and all the great videos!

I want to be able to use time code. Also I want to be able to pass arguments and set variables in the playlist… I want it to work like this:

00:00:00.000 HUE=0; Solid();
00:00:45.234 Rainbow();
00:00:54:333 HUE=160; Solid();
00:01:23.123 twinkle(5, 300, HUE);

Is this sort or thing possible?

I had a similar thought this morning. Definitely possible. Let me ponder a bit.

When you envision it like this, do you also think about loops and/or macros? E.g., intro is ABC, verse1 is PDQ, chorus is XYZ, verse2 is JKL, and then the chorus is XYZ again?

Timecode is a great idea. Do you think 1/1000th of a second resolution is needed, or would 1/100th of a second be sufficient? I ask because if we have to use 1/1000ths, the biggest timecode we can hold in 16 bits is 65.535 seconds – too short for a pop song. So we’d have to go to 32 bits for timecode… which might be ok, now that I think about it, especially if we can force all this stuff to be in PROGMEM instead of RAM.

Let me ponder a bit more.

But: I am curious if you think about light/choreography performance playlists as having loops or macros or subroutines, or if it’s all basically a straight list of timecodes and actions? I realize this is a little theoretical, but I’m curious how you think about it, or how you’d envision it working.

First, I think .000 is unnecessary – .00 is totally sufficient.

Second… macros/loops. I think it’s a neat idea but I don’t think it’ll work in practice. Or rather, it’ll work if I’m dancing to Techno but not if I’m dancing to the Rolling Stones – I have a 60s Psychedelic themed gig this weekend and I’m programming the wings to She’s A Rainbow (!!!) and their time counts are ALL OVER the place. Even techno dance stuff has retards in weird places. I’m kind of a perfectionist about having it change right on the beat, so I think I’d end up not using loops because they’d be off by a bit, most of the time.

Copy/paste and add timecode to repeat a section is fine. If I’ve gone through the song and written down all the timecodes ahead of time, which I have, then it’s easy.

Ok! Thank you!

See new approach here, millisecond timecode and all: https://plus.google.com/112916219338292742137/posts/TVzTPbnJNE7