November 21, 2024, 11:29:53 PM

News:

Got VSA?  Want to use your Prop-SX?  Now you can!  See the VSA section of the Library forum for Prop-SX code that works with VSA.


Hammer "hi striker" carnival game code

Started by imagineerdan, April 07, 2008, 10:53:29 AM

Previous topic - Next topic

imagineerdan

Hi, I am making a little version of a game where you hit a button and it makes a leds illuminate up a display. In essence I am tying to have 15 LEDS on 15 pin outs, and 1 input pin on 16. So with each time you trigger 16, a pin illuminates, while leaving the previous pin still on. So I trigger pin 16 once, pin1 is on, I trigger 16 again, pins 1 and 2 are on, I trigger a third time, pin1,2,3 are on and so forth. What would the code look like for something like this?

JonnyMac

April 07, 2008, 11:51:35 AM #1 Last Edit: April 07, 2008, 12:41:00 PM by JonnyMac
Here's the easiest version I can think of.  You'll need to reset the controller once you've reached the top.

' =========================================================================
'
'   File...... Bar_Graph.BS2
'   Purpose...
'   Author.... Jon Williams, EFX-TEK (www.efx-tek.com)
'   E-mail.... jwilliams@efx-tek.com
'              Copyright (c) 2008 EFX-TEK
'              Some Rights Reserved
'              see: http://creativecommons.org/licenses/by/3.0/us/
'   Started...
'   Updated... 07 APR 2008
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

Trigger         PIN     15                      ' active-high input


' -----[ Constants ]-------------------------------------------------------

IsOn            CON     1                       ' for active-high in/out
IsOff           CON     0


' -----[ Variables ]-------------------------------------------------------

timer           VAR     Byte
level           VAR     Byte


' -----[ Initialization ]--------------------------------------------------

Reset:
  level = 0


' -----[ Program Code ]----------------------------------------------------

Main:
  timer = 0                                     ' clear debounce timer
  DO
    PAUSE 5
    timer = timer + 5 * Trigger                 ' update timer
  LOOP UNTIL (timer >= 100)                     ' force 0.1 sec input

Release_Trigger:
  IF Trigger = IsOn THEN Release_Trigger

  level = level + 1 MAX 15                      ' update pin
  HIGH (level - 1)                              ' light pin (zero-adjust)

  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

Almost there! Is there any way we can make it so by holding the trigger closed wont make the bar graph climb. I guess what I am saying is so you have to hit the button one time after another and the faster you hit it the faster the bar graph climbs. So each button press is compiling from the last hit it once. pin 1 is high, hit again pin1 and 2 are high, an so on.

JonnyMac

The fix is easy -- see the updated listing (above).
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

I know its not ideal, or the most clean way, but is there a "hard coded" as opposed to a reference-looped way to do this, for my application I need to be able to tweak each pin differently or have the ability to use 15 pins or 6 pins. Also is there a way I can have them PWM fade-on quickly/ I know this is alot but as I am tweaking the design of this little device I need the flexibility. Thanks soo much!

JonnyMac

Try this version, it uses a constant to define the max number of pins used, and it fades the new LED on.  You may have to tweak the LED fade-on loop.

Don't be lulled into "hard coding" paradigms; they're a trap that leads to misery and your ultimate downfall.  Okay, that last part was a little mellow dramatic, but still, hard coded programs are a bad idea.  When you tell me how you might want to "tweak each pin differently" I'll show you the flexible way to do it.  I can only code based on what you provide.  Remember, I'm a good coder, not such a good mind reader.  ;D

' =========================================================================
'
'   File...... Bar_Graph.BS2
'   Purpose...
'   Author.... Jon Williams, EFX-TEK (www.efx-tek.com)
'   E-mail.... jwilliams@efx-tek.com
'              Copyright (c) 2008 EFX-TEK
'              Some Rights Reserved
'              see: http://creativecommons.org/licenses/by/3.0/us/
'   Started...
'   Updated... 07 APR 2008
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'   {$PORT COM1}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

Trigger         PIN     15                      ' active-high input


' -----[ Constants ]-------------------------------------------------------

GraphSize       CON     15                      ' use 15 LEDs

IsOn            CON     1                       ' for active-high in/out
IsOff           CON     0


' -----[ Variables ]-------------------------------------------------------

timer           VAR     Byte                    ' debounce timer
level           VAR     Byte                    ' current level
newPin          VAR     Byte                    ' new pin to activate
duty            VAR     Byte


' -----[ Initialization ]--------------------------------------------------

Reset:
  level = 0


' -----[ Program Code ]----------------------------------------------------

Main:
  timer = 0                                     ' clear debounce timer
  DO
    PAUSE 5
    timer = timer + 5 * Trigger                 ' update timer
  LOOP UNTIL (timer >= 100)                     ' force 0.1 sec input

Release_Trigger:
  IF Trigger = IsOn THEN Release_Trigger

  level = level + 1 MAX GraphSize               ' update level
  newPin = level - 1                            ' z-adjust for new pin

Fade_Up:
  FOR duty = 0 TO 255 STEP 10
    PWM newPin, duty, 2
  NEXT
  HIGH newPin                                   ' keep it on

  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office

imagineerdan

You Have a good point Jon! Well lets see then if we can make this work. I guess the next step is when the graph reaches the top of the "graphSize" say 10, then the next times that the the trigger is triggered, pin 11 turns on then off which each triggering.

And is there a way to work in pin 14 as a show reset input?

JonnyMac

Okay... I wrote it, you study and learn from it.  This one is more complicated as you now want two button inputs that both need to be debounced.  Please download (from Parallax) and read my book StampWorks as this has lots of good tricks for the BS2, including the simultaneous debouncing of multiple inputs.

I have a tester board with eight LEDs so that's what I used in the program; you'll want to update the BlinkPin and GraphSize constants.

' =========================================================================
'
'   File...... Bar_Graph_v2.BS2
'   Purpose...
'   Author.... Jon Williams, EFX-TEK (www.efx-tek.com)
'   E-mail.... jwilliams@efx-tek.com
'              Copyright (c) 2008 EFX-TEK
'              Some Rights Reserved
'              see: http://creativecommons.org/licenses/by/3.0/us/
'   Started...
'   Updated... 08 APR 2008
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'   {$PORT COM1}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------


' -----[ Revision History ]------------------------------------------------


' -----[ I/O Definitions ]-------------------------------------------------

Trigger         PIN     15                      ' active-high input
RstInput        PIN     14
BlinkPin        PIN     7                       ' blink P7 when full


' -----[ Constants ]-------------------------------------------------------

GraphSize       CON     7                       ' use 7 LEDs

IsOn            CON     1                       ' for active-high in/out
IsOff           CON     0

Yes             CON     1
No              CON     0


' -----[ Variables ]-------------------------------------------------------

state           VAR     Byte                    ' program state
idx             VAR     Byte                    ' loop controller

btns            VAR     Byte
startNow       VAR     btns.BIT1               ' 1 for valid start
rstPgm         VAR     btns.BIT0               ' 1 for reset

level           VAR     Byte                    ' current level
newPin          VAR     Byte                    ' new pin to activate
duty            VAR     Byte


' -----[ Initialization ]--------------------------------------------------

Reset:
  OUTS = $0000                                  ' clear all outputs
  state = 0                                     ' reset to climbing
  level = 0


' -----[ Program Code ]----------------------------------------------------

Main:
  btns = %11                                    ' assume pressed
  FOR idx = 1 TO 5
    btns = btns & (INH >> 6)                    ' scan P14 & P15
    PAUSE 5
  NEXT

  IF (rstPgm = Yes) THEN Reset                  ' check for reset input
  IF (startNow = No) THEN Main                  ' wait for start input

Release_Trigger:
  IF (Trigger = IsOn) THEN Release_Trigger

  BRANCH state, [Climb, Blink]
  state = 0

Climb:
  level = level + 1                             ' increment level
  IF (level > GraphSize) THEN Blink             ' blink if graph full

Fade_Up:
  newPin = level - 1                            ' z-adjust for new pin
  FOR duty = 0 TO 255 STEP 10
    PWM newPin, duty, 2
  NEXT
  HIGH newPin                                   ' keep it on
  GOTO Main

Blink:
  state = 1                                     ' set new state
  TOGGLE BlinkPin
  GOTO Main


' -----[ Subroutines ]-----------------------------------------------------


' -------------------------------------------------------------------------


' -----[ User Data ]-------------------------------------------------------
Jon McPhalen
EFX-TEK Hollywood Office