Does random(max-min)+min work for values of max and min that are less than 1?
I figure that people can post partial solutions if they get stuck… It’ll help everyone to see where folks get stuck.
IMO, posting a litte code and asking for help is great! The object here is for everybody to get better. For folks a little further along the path, having to come up with good, simple explanations for why “this” and not “that” helps them improve their skills and understanding too.
I consider myself a relatively awful teacher – @jeff and @scruffynerf, among others, are much better – but still, most of my IRL work lately involves designing things, then explaining them to people at sometimes vastly different levels of technical knowledge. It’s a skill well worth practicing.
It’s absolutely a skill, and teaching often helps you get better at it, especially if requires you to break it down and see it anew. I have the sort of brain (and always have, since I learned to code from a book and occasional RadioShack visits to type on a store TRS-80) where I can visualize code in my head. But that also requires understanding the pattern(s) and how to code.
All of these skills are intertwined…
I won’t teach you to code, there are many better teachers and videos and books. But a way to see how to see the task at hand, and ways to solve it, that’s helpful for all of us.
There are many ways to solve this task. I intentionally left it vague. And funny enough, a single random pixel is much harder than just randomly lighting a bunch of pixels, then lighting a new bunch, that’s a super trivial task, making static is both easy and “not art”. Focusing on a single one forces you to examine the elements in detail, and not shotgun it.
I’m gong to say that you should try some examples and answer this for yourself. Also negative numbers? (Which shouldn’t be possible if Max is bigger than Min, meaning you might need to be sure it never is…)
And if random with small values, doesn’t work as expected, how could you work around that? Hint: If random(1), aka between 0…1 works, you have a random percentage.
There are many of them. How do you keep track of time? Maybe you count down (or up) a variable? Maybe you use a timer or a delta, maybe you use a waveform, and when it cycles, it’s counted down and started over. Maybe it’s a slider, so you can control it. (Yes, random doesn’t need a slider, but figuring out the right range of random values, maybe you need a slider to manually get in the right neighborhood. Hmm, that’s 1/20 of a second, too short, that’s 5 minutes, too long, ah that’s 1-5 seconds, maybe that’s Just Right?)
As they say in the Hitchhikers Guide to the Galaxy… “Don’t PANIC!”
I’m making progress. I am on my third rewrite and have gone thru a few more examples.
exporting variables and enabling variable watching is a really splendid idea
Eureka!!! “Lightbulb moment!!!”
Just need to get the random delay working. I keep getting “Execution steps exhausted” does that mean I am looping too fast? I tried to make a delay using
while (time(0.015) < 0.9) {
}
and it blows up with that error. My idea from you peeps is to use a wave function with a certain period… waves got from 0 … 1 so if I keep checking for my value being less than 0.9 I would think that would be good… but I may be checking too fast…
Rather then a while, let’s look at the “engine cycle”
BeforeRender intentionally tracks the delta of how long since the last BeforeRender… Why?
Because we don’t know exactly how long Rendering will take. It could be a split second, it could be longer. Depends on how long it takes to calculate all of those pixels. We’re only lighting one pixel, But it still has to look thru all of the pixels. [This is why adding a huge amount of pixels slows down frame rates. Even if you only light up one pixel.]
Figure out a way to use that delta.
Your error is because that “while” is running out of available execution time, and the “engine” is stalling due to that pause.
Btw, using a time() function is another good way to track a timed change, it’s the while that got you into trouble. Try an If… Depends on length of your loop, and how precise you need the timing, of course. But if you check value of the time and it’s near zero, or maybe less than the last time you looked at it (see how that works?), then you know it’s a new cycle.
So I decided to ponder this over night. I couldn’t see how to simulate a delay using time with only an if statement. I kept coming back to needing some kind of looping while, for, do, or something… but then I got to thinking about the functions that @wizard gave us in the PB… being the beforeRender and render… One could use the function beforeRender itself as a type of looping device as that is what it is doing. Then using an if statement to see if the time wave had gone far enough if not then just exit out of beforeRender… Then I think I had another “lightbulb moment” after reading @Scruffynerf’s hint above … beforeRender has a delta that is the amount of TIME (delta) since the last time it was called. So if you had a global variable delay you could keep adding the delta’s and check to see if enough time has elapsed… if not drop out of the beforeRender and render again… While this exercise is taking me MUCH longer than I expected, I have learned a bunch and had fun. I can’t wait to see share what I did and see how other’s accomplished it as well.
Yes! But is your pixel twinkling?
OMG always triple read the requirements… I am so out of practice. I am indeed only worrying about fading out not twinkling. hmmm well so muh for rev 4 being the one. Rev 5+ here I come! Luckily it is working as I expect now so modifications are a little easier… before I had what best could be described at random static and was clueless for the longest time about why… lol
Hey @devoh, when it comes to the different ways to track time and delays, I thought you might find this interesting – search the forum for the word “accum”:
https://forum.electromage.com/search?q=accum
And for what it’s worth - your whole thing about getting “execution steps exhausted” while trying to make a delay is exactly the first issue I emailed Wizard about a little over a year ago, and shortly after that I was hooked.
Some of us should golf this! We could go for minimum Logical Lines of Code, or [edited based on experience] maximum FPS @ 1000 pixelCount (for a given HW v2 or v3, and “no LEDs” setting).
I’ve got a minimal version going, an aesthetic version, and one that’s readable
I think a golfing section for advanced users sounds like a great idea!
For those who haven’t heard the term:
In golf, less strokes (hits of the ball) the better.
Golfing code, reducing the number of characters/size of the code to the minimal needed to do the task.
For the old folks in the room, “I can name that tune in 5 notes” “4 notes…”
Lacking (yet) a controller I’m gonna wing it , ask stupid questions.
I get the need for “dwell time”, for sure. Reminds me of the fascinating talk I attended given by the developers of Windmap… https://earth.nullschool.net/ Dwell was absolutely their key to superb visualization. I hope this point is not lost on me.
For your second point, I’m not sure what is meant here by using “a single value” to potentially control a bunch of LEDs but I guess that’s the point… figure it out. a good challenge it seems!
Since we’re getting later in the week and you don’t have a board yet, @JustPete, I thought I’d add some commentary to Zranger1’s:
The alternative requires the storage of only a single value, no matter how many pixels you may eventually want to light.
I interpreted this as an invitation to think about how to generate multiple random values that can persist for the many cycles of render(), but without needing a lot of memory (such as an array, or pointer into it). The challenge seems to call for picking a random color, random LED, random duration, perhaps random twinkling, and random interval between each LED. We could store 5 variable for these random values from random()
as well as some timers, but his point is that this scales linearly if we ever want to set random colors for 5000 LEDs.
Procedural generative art holds some clues, as does the PRNG wikipedia page and the “Static random colors” pattern in the pattern library. A deterministic random number is generated from the random number that came before it (all the way back to the first, which is called the “seed”).
But if you’re new to PixelBlaze, you’ll learn a ton with the 5+2 variable approach to this problem!
In thinking about next week’s task, I’m likely going to build on this… That thinking to scale is important.
But I don’t want people to get lost in the weeds: designing for 5000 pixels can only happen when you understand how to design (and code) for 5 or 50…
Thanks. This is very helpful. And I really do like the background on how "randomness’ is generated and used here and elsewhere @jeff
I had figured the basic logic, just the syntax is new, along with definitions of terms.
For now, I am assuming/guessing that the +2 you refer to is WRT the BeforeRender/Render cycling. Perhaps.
Anyway, thanks for your kind replies.