Shop OBEX P1 Docs P2 Docs Learn Events
SX/B random led sequencer — Parallax Forums

SX/B random led sequencer

SpragmaticSpragmatic Posts: 59
edited 2008-12-23 17:04 in General Discussion
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!!·

Comments

  • PJMontyPJMonty Posts: 983
    edited 2008-12-13 08:42
    Spragmatic,

    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
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-13 17:45
    I am sort of new to this, but have written a few simple codes in SX/B·to control some led's and such.· My problem is writing all these steps in one single program.· I try and it doesn't seem to work correct for me.· I guess practice and time will have to do.· I will look at the forums for assistance.· Thanks again!

    ·
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-12-14 20:16
    Happy Holidays from Hollywood -- attached is my gift to you. [noparse];)[/noparse]

    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.
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-14 21:06
    This is amazing! Thanks so much for the help! Happy Holidays to you as well!! I'm going to set this up right now and watch the fun. Thanks again JonnyMac!
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-16 00:48
    Here's a·very simple·version I did that does not have PWM, but all other features.· Works Great!!
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-18 04:52
    I finally got this project almost·finished.· I have one issue though.· I changed·my mind on the switches I was going to use to the ones in the photo and I·now have a debounce problem.· I'm not quite sure·how to add·a software debounce(to each switch)·to the program that I have listed in this post?· Any help would be greatly appreciated!· I got to get this done before X-Mas.·Thanks!!
    480 x 640 - 156K
  • VelocitVelocit Posts: 119
    edited 2008-12-18 07:00
    You could just use the PULSIN command. If the value of the incoming pulse is above a certain threshold, continue with your program, otherwise, do nothing. You'll probably have to do a little experimentation to find which threshold values work best.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    -Paul
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-12-18 15:32
    If you look in the PWM program I posted above you'll see the that [noparse][[/noparse]active-low] buttons are debounced as part of the interrupt.
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-18 16:02
    Thanks! I will play around with this today and see if it works.·
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-12-18 18:25
    If you're not using interrupts you can do it with a simple loop and ANDing -- if your buttons on RA are active-high you could do this:

    Check_Buttons:
      myButtons = %0000_1111
      FOR idx = 1 TO 10
        myButtons = myButtons & RA
        DELAY_MS 5
      NEXT
    


    You can then check myButtons.0 instead of RA.0, etc.
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-18 18:50
    Thanks JonnyMac! I'll try this first.
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-12-18 18:57
    Make sure you understand what it does before blindly incorporating it into your code -- that's a quick way into a program that you have no control over! [noparse];)[/noparse]
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-18 20:14
    O.K. Thanks!
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-20 23:55
    Actually, I'm having a tough time understanding how to incorporate this debounce feature into my program from above. I understand what debounce is used for, but I don't get how to add it into the code for all (4) switches. HELP! I'm probably making this to complicated for myself! Thanks!!
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-12-21 00:52
    I don't understand what you want your program to do. If you'll explain it (your code is a bit mysterious to me) then I'll write you a program that shows you how to use the debounce segment.
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-21 01:40
    Here is a video so you can see it in action.· I would just like the eyes to not blink when I switch·from·the different colors as you can see.· I guess I'm not to worried about the last switch which is PButton4 in my program because that's the random part.· So i need a debounce code for switches 1-3.· All switches are active high and on pins RA.0, RA.1 and RA.2.· Hope all this made sense?· Thanks!!

    http://i77.photobucket.com/player.swf?file=http://vid77.photobucket.com/albums/j72/spragmatic/MOV04895.flv"
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-22 23:23
    I worked with the debounce for my code for the OWL project and this is what I came up with.· It works just fine with no more flicker from the switches.· Just wanted everyone to look at it to see what they thought.· Thanks!·
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-12-23 00:44
    Have a look at what I've attached -- I *think* it does what you want but can't be sure as you seem unwilling to write a full description of what your program is supposed to do....
  • SpragmaticSpragmatic Posts: 59
    edited 2008-12-23 04:03
    Thanks again JonnyMac! I tried your program and it does exactly what I wanted. It works just like the program I did except your debounce is quicker. The switches I'm using are ones from Radioshack and I don't think I'll be using these on any other projects. They are cheap junk! Have a Merry X-Mas!!
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-12-23 17:04
    Glad it worked. Looking at it again this morning I found that there are unnecessary IF-THENs; here's a more efficient version of the Main loop:

    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 Main
    


    The 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:

    Leds = myButtons << 5
    Led1 = IsOff
    


    ...but this is not as easy to understand.
Sign In or Register to comment.