Can someone please help me with a problem I am having...

Can someone please help me with a problem I am having…

I have simplified the code down to the very basics in order to show the problem.

It seems that when I use beatsin16 to calculate a position, my static variable decides to take on values of its own.

Please take a look at the code to see if you can reproduce the fault…

When beatsin16 position is calculated, and assigned to the leds[] array the Timer that I have made a static variable decides to take on odd values.

This may be me overlooking something as I am new to code, but take a look at the screenshots in the link below. I would appreciate any help given…

There are a couple of bugs in this code. First, you can’t let the index value “pos1” get the value NUM_LEDS. The largest index you can use is NUM_LEDS - 1. What’s probably happening is that leds[NUM_LEDS] is clobbering the value of the static variable “Timer”.

Second, do you want “Timer” to be updated with the value of millis() every time through the loop? Right now, that’s not what the code does.

Ahhh yes I see that now you point it out. I think I was experimenting with another function and left that in without the -1. Thanks for the help Sam!

Not quite, timer was just an easy name to put in when I minimised the code for this example. I actually want a time stamp of when the function first runs. I then have another variable currentMillis updated every loop to compare to the time that we started.

@dalemorris164 Note that “Timer” will always have a value around 1000 because you delay 1000ms in setup. You could just get rid of Timer altogether and use millis()

It would probably help if I explained what I was doing a little bit better.

I’m making a 20m LED beep test…

…I need to send a block of LEDs down a 20m section of strip (wired in parallel back to an octoWS2811 adapter) within an accurate time period.

It needs to hit the end of the strip in exactly 9 seconds… so I’m taking a time stamp of when this particular function starts, then I compare currentMillis with the time stamp in order to get the position of the current LED (mapped between 0 and NUM_LEDS-1)

I also have a sensor at the end so I can tell if the person ran within the time or not. Is this the right approach or should I be thinking differently…?

Thanks for your help Sam, I appreciate it

@dalemorris164 Makes sense to me.