Scheduling Patterns to execute at specific times


I am a newbie. Say I have a song that is 3:00 minutes in length. Every x number seconds I would like to have a different pattern execute. I want to determine when the pattern change takes place based on a change in the musical phrasing, chorus, etc. The LED patterns vary in length for example from 2 - 10 seconds before they repeat/loop. For example I would like the “sparkfire” pattern to keep looping for 12-seconds after which “Plasma 3D” would execute for 8-seconds followed by “sinus” for 15-seconds.

Time (seconds): Pattern Pattern Length
0 - 12 sparkfire 2
13 - 20 Plasma 3D 3
21 - 45 Sinus 5

One possible solution: Create a single pattern that contains all three patterns possibly using a for loop + the JS timer function to make this work.

Note: I considered the Sequencer but I understand that it has a fixed timing interval.

Any advice on how to approach this would be greatly appreciated.


You’re pretty much there. Create one pattern and put all your patterns within it, then use a chain of IF statements to determine which one to use based on the timing.

Thanks. I appreciate your response.

Yup, that should work, and to add, the chain of ifs can be else if…
(And per wizard’s response, elapsed time should be in seconds)

if (elapsed_time < 12) { value = mode[0](index) }
else if (elapsed_time < 20) {value = mode[1](index) }
else if (elapsed_time < 45) {value = mode[2](index) }

Looks like you are all on the right track, I just want to give a heads up that the maximum value that can be stored in the 16.16 variables is ± 32767. The same is true for literal numbers as well.

If you accumulate milliseconds from the delta in beforeRender you’ll only get up to about 32 seconds before it wraps. It’s super precise for animations, but for timekeeping you might want to convert to another unit, like seconds, which would give you about 9 hours of time before it wraps.

Using minutes would still give you pretty good resolution since variables hold fractional values, and would take 22 days before wrapping. Here’s some code that counts in minutes and updates every second:

export var minutes
export var milliseconds
export function beforeRender(delta) {
  milliseconds += delta;
  if (milliseconds >= 1000) {
    milliseconds -= 1000
    minutes += 1/60
1 Like

Thanks, I’m still struggling but this is helpful

I’d be happy to help you with some specifics.

That would be great! What is the best way to contact you?

Did you get it figured out?

Yes, I got it figured out. Thx