Need a hint on basic coding

Dudes I need a hint. I’ve been really trying to understand the basics but i’m getting frustrated on the easiest of things. Is there any code in HSV format that marches lets say 2 pixels of yellow down the LED strip using time command so I can see what is happening? I was messing with a string of 50 LEDs and the following beginner code and don’t really get what is happening

export function render(index) {
  red = green = blue = 0
  leadPosition = time(0.08) * pixelCount
  red   = abs(leadPosition - index - 0) < 1
  green = abs(leadPosition - index - 4) < 1
  blue  = abs(leadPosition - index - 8) < 1
  rgb(red, green, blue)

but I can’t even figure out what is going on with assigning the variables as epressions.
For example:
red = abs(leadPosition - index - 0) < 1
to me reads set red equal to the absolute value of (50 * 0.08) - (0) <1 which is false so it passes a 0?
and then the next cycle it reads (50 * 0.08) - (1) <1 which is also false so another 0 gets passed to the rgb(red, green, blue) function. It will do that until the 5th pixel count and then it will be true from there on passing a 1 which lights red. Yeah…i don’t think i am grasping the concept.

1 Like

Here’s your hints!

First, time(.08) will evaluate to a number between 0 and 1, again and again - a sawtooth wave over time. The .08 in time(.08) specifies how fast it goes from 0 to 1 over and over. It doesn’t make it output .08 – Now, there’s a chance there’s some moment where the output might be exactly .08000. But it’s just as likely that on one frame time(.08) will give you .07973 and in the next it will give you .08184.

Let’s freeze at a point where time(.08) is almost halfway through it’s ramp up - it’s currently returning exactly .49. In your example, you have 50 pixels. Let’s build a table for this red expression for each of the indices:

  red = abs(leadPosition - index - 0) < 1
index time(.08) leadPosition leadPosition - index abs((prior)) red, I.E.,
(prior) < 1
0 .49 24.5 24.5 24.5 0
23 .49 24.5 1.5 1.5 0
24 .49 24.5 .5 .5 1
25 .49 24.5 -.5 .5 1
26 .49 24.5 -1.5 1.5 0
49 .49 24.5 -24.5 24.5 0

There you have it: For one whole frame, a frame where time(.08) happens to be returning .490, you see that only LEDs 24 and 25 would have red set to true. Those two LEDs would be red.

Does that help?!

1 Like

I see Jeff is replying, and hopefully he’ll link to his awesome video about how to use time() and make pixels do what you want.

1 Like

Good reminder, thanks Scruffy!

1 Like

One thing Jeff didn’t mention is that time(whatevervalue) only has one value each frame, so you’d be better off calling it in before_render(), and then just using the variable result.

So if you do time(.15) and time(.20) those are different, but time(.15) will always be the same value in any given frame.

In a small pattern, doesn’t matter, but as you grow (both in code and pixels) you’ll want to do things in render() that you have to do in render(), and avoid doing repetitive stuff in it that you can avoid doing over and over with the exact same result.

To clarify this:

If you add 5+10 every time you loop thru render, with 100 pixels, you’re always gonna get 15, but you’ll do that math 100 times.

You’d be better off adding 5+10 in before_render(), saving it to a variable like
myanswer = 5+10

And then using myanswer in render()

2 Likes

I will be going over this video after work.
I still need to get 1 more wire run into the attic tonight while the weather is cool and then maybe i can update my sparkfire video so you guys can see the results from all of your help!
Thanks for taking the time to teach!

1 Like