Getting an LED to blink 10 random numbers #4660
Replies: 1 comment
-
Posted at 2018-08-09 by @gfwilliams Ahh - I think you're hitting a few issues here... One is that things like The other one is that because all those Timeouts are executing after the loop has finished, For that issue, you can call setTimeout from a function:
Since there's a new function call for each timeout, You can also pass data in to setTimeout, so for instance you could do:
Both should work. For what you're want to do with the blinking, it might be worth trying something a bit different. You could have a single function that you call that actually alters the array it works from. So for instance the code below runs Finally if the array is empty it stops - there's no more work to do...
Posted at 2018-08-09 by AdaMan82 Thanks so much for your help. No joke I have probably logged 10 hours into trying to solve this problem on my own, learning the hard way that setIntevals with the same name execute concurrently from a loop instead of sequentially and that one clearInterval blows the whole thing up. Definitely not an efficient use of time but I learned a lot. One thing with your last example though is that I want to retain the data in memory. If I do a for counter with that code, would I still accomplish the overall goal? (alternatively I could always transfer the data into a new array and then pull/destroy the data from new that array, but that seems inefficient? Posted at 2018-08-09 by @gfwilliams
It depends how big your array is going to be. If it's only a few items long like your example, it's really not going to make any real difference. It'll only use the data for a short period of time, and then it's gone. If you do want to copy the array, all you need to do is That works out well for you as well, because the Uint8Array you were using can't have
I'm not sure I know exactly what you mean, but a I've just reworked the code above so it doesn't mess with the array:
It's basically the same, but instead of comparing everything against 0 and empty arrays it's using Realistically, that's probably a lot more understandable anyway! Posted at 2018-08-09 by AdaMan82 Right. So the problem with my execution was it was just smashing through it all at once and thus not giving it the time to properly execute. That's what I didn't fully grasp in your first explanation. If I understand correctly, by using if/else within a function using the timers, it will execute in a time appropriate fashion with proper controls, instead of firing everything at once and then having it wait until later to find all the variables have changed as part of the initial for execution.. Ironically your last code is similar to something else I tried which used setInterval and a clearInterval inside of a setTimeout, but as the loop executed, the intervals conflicted with each other due to their method of execution. Again, thanks so much for your help! Posted at 2018-08-10 by @gfwilliams Yes, you're totally right. I think it's the bit of JavaScript that takes the most effort getting your head around. While you can use JS like any other language, where delays are involved you're much better off using callback functions as above - which is pretty alien if you're used to just writing procedural code that executes linearly. The real bonus is that Espruino can keep doing other things inbetween running the code above. If you implemented it all as delays in something like C, the device wouldn't be able to handle much else while the LEDs were flashing. Posted at 2018-08-10 by @allObjects @AdaMan82, with the code you just mastered, you may very much enjoy lines If you want to run, study and observe yourself the section I'm talking about, upload the snippet below. After uploading, you enter in the console It does not matter how many time and in what frequency you call Setting Instead of using a queue (append to string), sequencing could also be solved using Promises ('time recursion').
Posted at 2018-08-11 by AdaMan82 Nice! Thanks again! Yeah, see all my life I've coded with linear execution in mind, so it took a while to wrap my head around it. The first thing I went for was delay when I couldnt make it work which is obviously wrong. Ultimately the big picture is to design something that lights up 10 random lights on a strip of WS2812B LEDs, of a pre-determined length, the length of which is determined by pressing a button to increment the length of the strip, a button to decrease the length of the strip, and a button to randomize 10 lights within the boundaries of the strip and illuminated them for 3 minutes. The above piece was me just trying to work with the built in LED for now, and then using the output on the LEDs once I get it all figured out. Posted at 2018-08-11 by @allObjects Ic... almost... What I do not understand is the button to increment/decrement the length of the strip... because the strip has at runtime obviously at all times the same length, does it? Or do you you think with the button to move the then lit LEDs back and forth on the strip? Try to understand the big pic here (I also noticed the related conversation titled [v issues],(WS2812B issues), where you do your first steps w/ WS2812B. WS2812B strips have some timing sensitivity... therefore, you will always have to prep all values in an array and feed it to the write..., in other words - when I understand you correct - there will be, for example, 5 0-triplets which will go out first and keep in the end the last 5 LEDs dark, then 10 mixed triplets for the 10 lit LEDs in various colors, and then, finally another 3 0-triplets to keep the first 3 LEDs of a strip of total 18 LEDs in the dark. Does that sound about right? Can you make a char-graphic of various sets of states the strip would show in? ...for example
Is that about what you have in mind? Posted at 2018-08-11 by AdaMan82 No way more simple than that. See graphic below. [+] increases the length of the strip flashing the next light in the strip to show where the "max" is at for the random (green dot in this case). [-] decreases the length of the strip as per above. [rnd] lights up 10 random "full" slots (cyan dots as an example) for 3 minutes. If you press the button again, it lights up a new random 10 lights for 3 minutes. Attachments: Posted at 2018-08-11 by @allObjects I guess i get it. The three cols of dots are stringed together, one after the other? Posted at 2018-08-11 by AdaMan82 That's right. It is one whole strip. zig zagging through the assembly. Ultimately 3 functions. One to increase the number (ideally illuminating the light its at), one to reduce the number (illuminating the spot its at) and one to generate a random 10 numbers up to and including where its at and leave them on for 3 mins. Its pretty straightforward (I think) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2018-08-09 by AdaMan82
So I'm trying to design a circuit to blink out 10 randomly generated numbers in a row on the Pico onboard LED, with a break between each number. Being that I know very little about java, it's been a bit of a struggle. I've been using combinations of setTimeout and setInterval, but I can't seem to figure out how to blink the numbers one after the other.
I've come to the point where I'm using an array to generate concurrent timers as per below. Both g and rando are Uint8ClampedArray(10). h is declared globally but doesn't work any better locally. I've started with 4 to keep it simple.
outputs the following:
WTF am I doing wrong and/or what am I missing to get this thing to blink out 10 random numbers with a space in between?
Any help would be appreciated
Beta Was this translation helpful? Give feedback.
All reactions