How do I sequence LEDs? Plus other ?'s
PoundSign2
Posts: 129
Hello friends,
I have downloaded some very interesting LED .spin files from the OBEX, but I do not understand how they generate patterns or sequences. I would like to start off small and do this for example, blink all odd pinned LEDs, and while the odd LEDs are on, the evens are off, and vice verse. I was thinking a not statement would suffice paired with an if statement. However, I do not know how to conveniently assign a variable/constant for even numbered pins, or for that matter, how to assign multiple pins in general. Forgive me for my lack of knowledge I've only had this Propeller chip for a few days! Anyways, any help is appreciated!
I have downloaded some very interesting LED .spin files from the OBEX, but I do not understand how they generate patterns or sequences. I would like to start off small and do this for example, blink all odd pinned LEDs, and while the odd LEDs are on, the evens are off, and vice verse. I was thinking a not statement would suffice paired with an if statement. However, I do not know how to conveniently assign a variable/constant for even numbered pins, or for that matter, how to assign multiple pins in general. Forgive me for my lack of knowledge I've only had this Propeller chip for a few days! Anyways, any help is appreciated!
Comments
another way could be to start with &01010101 shift the bits one to the left and then shift back one to the right etc. etc.
best regards Stefan
This is very helpful. Although when I uploaded you supplied code only the odd numbered pins lit up; and it never switched back and forth between even/odd. I do not know how to debug it but I tried, and got no results. I understand now that [xx..yy] is how you list multiple pins. This will be very helpful in the future! One thing I am still confused about is how to shift the bits left/right. I know you use a << operator and specify how many bits to shift but how do you tell the dira to now turn on those pins? So if I had 001111 and shifted left 1111, how does it know that dira[xx..yy] := 001111 is now dira[xx..yy] := 110000 ?
As for your code only lighting odd LEDs, I don't know. Can you attach your program so I/we can have a look? re: shifting, ususally you setup dira once and do the rest with outa, e.g. In general a cog should only ever touch pins (dira) it is going to drive (outa).
What's the final goal here?
So do I use the % symbol for binary or the $ symbol for binary? I thought the manual suggested % for indicating binary? Also I do not know what "WYSIWYG" means, sorry. The end goal of all of this is being able to com up with sequences for LEDs. Essentially patterns for lights that I can incorporate into projects for my kids. I thought the propeller would be ideally suited for this task.
WYSIWYG, What You See Is What You Get. This is one of three editor types you can chose to edit your postings. Unfortunately this particular one is broken (I use the standard version). This can be changed in your profile settings.
Oh well that clears some things up! I have tried to bit shift, which I got to work a couple times, but overall not so much. So what I did was use binary for every outa call. Please examine my code and tell me what you think. Please don't be too harsh!
EDIT:
I also uploaded it to the OBEX, it can be found at...
http://obex.parallax.com/object/714
How do I use the DAT object to do this? Any tutorials you could possibly point me to?
The thing I am after is learning how to generate these patterns more accurately and using less code (more efficient).
I've also written a lot of LED sequencer code for places like Disneyland.
If I could put you into a time machine and take you forward a year, I think you'd write a program more like the one that's attached.
Keys:
-- make your code flexible
-- make your code easy to read
-- make your code easy to modify
All keys involve not embedding "magic numbers" into your code. Changing the timing of your program would be a lot of work because of the style. Please trust me that when you're writing code for others they will want changes -- sometimes before you've finished implementing the last request.
What Marko suggested is something I do with LED sequencers: put the patterns and sequences into tables (in DAT sections). Here's how I translated your patterns (not all shown).
Honestly, the hardest part is coming up with meaningful names for the patterns! Note that my table includes the number of elements in the pattern -- this lets me use any value in the table. We can play a the pattern with this bit of code Updated: 04 OCT):
It's a little involved, but gives you a lot of flexibility. You pass the address of the pattern, the pins to use, the timing for each element, and the number of cycles to play. By passing the pins as parameters you can use the same table on different sets of outputs. You could even play them in different cogs (I did this on a Disney project).
LED output is easy:
This sets your outputs -- wherever they are and makes sure that the IO pins are in output mode.
Here's my translation of your program (Updated: 04 OCT.
Easy, right? Need to speed up a pattern? No problem, change the timing parameter in the call. Done!
There's an additional feature in my program that let's you define sequences that include the timing for each step. I'll leave you to explore that.
Have fun!
WHOOPS! When I updated the program to allow pins as parameters I neglected to fully edit the set play_pattern() and play_sequence() methods. Those are fixed now and the demo updated to show you how to launch them into their own cogs so you can have patterns running in the background while your foreground code is doing other things. To facilitate this feature, I added a flag to each method that gets set to true when the pattern or sequence is playing, and then to false when done. By using a flag we don't have to calculate the duration of a pattern or sequence (steps * ms * cycles).
I realize that some of this code looks a little funky, but that's only because you're new. There will be an "Aha!" moment where it all comes together and you're free to be as creative as you like.
At some point you may want to add a little more pizazz while keeping the simplicity of storing patterns in DAT tables. This is what we did for the Disneyland Buzz Lightyear hand stamper. That device plays audio and a little light show at the same time. When the trigger is activated one of the audio clips is selected along with the pattern that goes with it. The main loop launches the WAV player cog, then the light show cog. The main loop waits monitors the audio and when finished, kills the light show cog if it has not already stopped.
We achieved a small level of brightness control by using quarternary numbers instead of binary patterns. For eight outputs this gives us four levels of brightness, though one assumes 0 will always be one of those levels. Have a look at the attached program.
BTW... did you see this post:
-- http://forums.parallax.com/showthread.php/150675-For-League-of-Legend-and-Wes-Borland-(Limp-Bizkit)-Fans?p=1211781#post1211781
I'm still worn out from several late nights (after my day job) programming that costume.