SX/B random led sequencer
Spragmatic
Posts: 59
Built·a circuit/project with 4 different colored led's and would like them to fade in and out randomly(at·random speeds if possible)·when one of my
switches·is tripped.· Not quite sure how to write the program.· I'm using the SX28 protoboard.··Thanks!!·
switches·is tripped.· Not quite sure how to write the program.· I'm using the SX28 protoboard.··Thanks!!·

Comments
What is your level of programming experience? Have you ever written any code, or is this your first project?
Your specifications already tell you what you need to do. Look at what you wrote:
1 - LEDs that fade in and out. Okay, you need to write some PWM code to control the brightness of the LEDs and let you fade them in and out.
2 - Randomly. You need a random number generator. Have you looked in the help file to see if SX/B has a random number generator? IF so, then use it. IF not, then search the forums for anyone who has written a random number generator.
3 - Do this when switches are tripped. That's a switch, a pullup resistor, and reading an input.
If you have no experience, then start with reading a switch. If you already know how to do that, then start on the random number issue followed by the PWM code. Both should be located on the forum in other postings.
Thanks,
PeterM
·
This program is a bit sophisticated and doesn't do random things, but it has all the framework components to ultimately get you where you want to go. What this program does do is cross-fade between LED channels. It starts with channel 1 on. When you press a button, if that channel's LED is off then there is a cross-fade between the channel presently on and the newly-selected channel. If you press multiple buttons then it resets directly to channel 1.
I whipped this up of a couple cups of Starbucks but have tested it on a PDB and know that it works. Have fun.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
-Paul
Check_Buttons: myButtons = %0000_1111 FOR idx = 1 TO 10 myButtons = myButtons & RA DELAY_MS 5 NEXTYou can then check myButtons.0 instead of RA.0, etc.
http://i77.photobucket.com/player.swf?file=http://vid77.photobucket.com/albums/j72/spragmatic/MOV04895.flv"
Main: RANDOM seed ' stir random value myButtons = GET_BUTTONS ' 50ms delay IF myButtons = %0000 THEN ' default Leds = %0001_0000 ' Led1 only GOTO Main ENDIF IF myButtons.3 = IsOn THEN Led1 = seed.7 Led2 = seed.3 Led3 = seed.1 Led4 = seed.5 DELAY_MS 100 GOTO Main ENDIF Led1 = IsOff Led2 = myButtons.0 Led3 = myButtons.1 Led4 = myButtons.2 GOTO MainThe only special cases are no buttons or the fourth button -- after than you're simply moving the button status to an LED so this version is cleaner. You could actually reduce the final section to these two lines:
...but this is not as easy to understand.