November 21, 2024, 08:38:00 AM

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.


Jeopardy Code needed

Started by Don Kuts, December 02, 2011, 02:44:15 PM

Previous topic - Next topic

Don Kuts

December 02, 2011, 02:44:15 PM Last Edit: December 03, 2011, 11:07:30 AM by Don Kuts
inputs
4- momentary switches (that was easy buttons)
1- Momentary switch for reset

Outputs
4 LED's one for each easy button
5 additional LED's for countdown timer
1 Buzzer (to be determined)

The idea is that this will be the ring in system for a mach Jeopardy game.  Each of 4 contestants will have an easy button when they want to answer they will hit their easy button.  At this time the program should light up their LED so we know who rung in, stop anyone else from being able to ring in,pause for 2 seconds then all five countdown LED's will light up with one turning off each second for the five seconds.  If they answer in the 5 seconds correctly the reset button is pushed to go back to be able to ask the next question.  If they do not answer within the 5 seconds after that last light goes out a buzzer is to sound for 2 seconds then shut off and resets it self. 

bsnut

December 05, 2011, 03:02:19 AM #1 Last Edit: December 10, 2011, 10:04:00 PM by bsnut
Welcome to the forums Don.

Here is your program code for a different version of the game of Jeopardy. I tough that Jeopardy had only 3 players ::). This code hasn't been tested so, you will need to let us know if works.

' =========================================================================
'
'   File......jeopardy game.bs2
'   Purpose...4 player Jeopardy buzz in game
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


' -----[ Program Description ]---------------------------------------------
'This will be the ring in system for a mach Jeopardy game.
'Each of 4 contestants will have an easy button when they want to answer they
'will hit their easy button.  At this time the program should light up their
'LED so we know who rung in, stop anyone else from being able TO ring in,
'pause for 2 seconds then all five countdown LED's will light up with one
'turning off each second for the five seconds. IF they answer in the 5 seconds
'correctly the reset button is pushed to go back to be able to ask the next question.
'If they DO NOT answer within the 5 seconds after that last light goes out a
'buzzer is to sound for 2 seconds then shut off and resets it self.
'
' -----[ Revision History ]------------------------------------------------


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

'INPUTS
Player1btn      CON     0                     ' player1 pushbutton on Pin 0
Player2btn      CON     1                     ' player1 pushbutton on Pin 1
Player3btn      CON     2                     ' player1 pushbutton on Pin 2
Player4btn      CON     3                     ' player1 pushbutton on Pin 3
Resetbtn        PIN     4                     ' reset pushbutton on Pin 4

'OUTPUTS
Player1LED      PIN     5                     ' player1 LED connected to V+ and OUT5
Player2LED      PIN     6                     ' player1 LED connected to V+ and OUT6
Player3LED      PIN     7                     ' player1 LED connected to V+ and OUT7
Player4LED      PIN     8                     ' player1 LED connected to V+ and OUT8
Buzzer          PIN     9                     ' buzzer connected to V+ and OUT9

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

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

#SELECT $STAMP
  #CASE BS2, BS2E, BS2PE
    T2400       CON     396
    T38K4       CON     6
  #CASE BS2SX, BS2P
    T2400       CON     1021
    T38K4       CON     45
  #CASE BS2PX
    T2400       CON     1646
    T38K4       CON     84
#ENDSELECT

SevenBit        CON     $2000
Inverted        CON     $4000
Open            CON     $8000
Baud            CON     Open + T38K4            ' fast baud for accessories

Predelay        CON     2000                    ' 2 sec pre-count down
Countdwndelay   CON     1000                    ' 1 sec pause for LEDs turning off
Buzzerdelay     CON     2000

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

btnstate        VAR     Byte      ' pushbutton states
timer           VAR     Byte
ledPin          VAR     Byte      ' Lamp/LED pin pointer for OUT11 to OUT15

' -----[ Initialization ]--------------------------------------------------
Test_Lamps:
  Player1LED = IsOn
  Player2LED = IsOn
  Player3LED = IsOn
  Player4LED = IsOn
  PAUSE 2000
  Player1LED = IsOff
  Player2LED = IsOff
  Player3LED = IsOff
  Player4LED = IsOff
  GOSUB Countdwn_LEDs

Reset:
  OUTH = %00000000                      ' clear all outputs
  DIRH = %00000000                      ' make P0-P13 outputs


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

Main:
  DO                                    ' checks to see pushbuttons are false
    GOSUB Check_Buttons
    IF btnstate = 0 THEN EXIT
  LOOP

  DO                                    ' waits for player to push their button
    GOSUB Check_Buttons
    IF btnstate.LOWBIT(Player1btn) = IsOn THEN Player1LED_on
    IF btnstate.LOWBIT(Player2btn) = IsOn THEN Player2LED_on
    IF btnstate.LOWBIT(Player3btn) = IsOn THEN Player3LED_on
    IF btnstate.LOWBIT(Player4btn) = IsOn THEN Player4LED_on
  LOOP

Player1LED_on:
  Player1LED = IsOn
  PAUSE Predelay
  GOSUB Countdwn_LEDs
  GOTO Reset

Player2LED_on:
  Player1LED = IsOn
  PAUSE Predelay
  GOSUB Countdwn_LEDs
  GOTO Reset

Player3LED_on:
  Player1LED = IsOn
  PAUSE Predelay
  GOSUB Countdwn_LEDs
  GOTO Reset

Player4LED_on:
  Player1LED = IsOn
  PAUSE Predelay
  GOSUB Countdwn_LEDs
  GOTO Reset

' -----[ Subroutines ]-----------------------------------------------------
Check_Buttons:                               ' Pushbutton debounce subroutine
  btnstate = %00000111
  FOR timer = 1 TO 50
    PAUSE 1
    btnstate = btnstate & INl
  NEXT
  RETURN

Countdwn_LEDs:                               ' Countdown LEDs subroutine
  ledPin = %00011111
  ledPin = ledPin & OUTH
  FOR ledPin = 11 TO 15                      ' loop through lamps/LEDs
    PAUSE Countdwndelay                      ' pause for LEDs turning off
    LOW ledPin                               ' LED on
    IF Resetbtn = IsOn THEN Reset            ' checks Reset pushbutton
  NEXT
    Buzzer = IsOn                            ' turn on Buzzer
    PAUSE Buzzerdelay
    Buzzer = IsOff                           ' turn off Buzzer
  RETURN

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


' -----[ User Data ]-------------------------------------------------------
   
William Stefan
The Basic Stamp Nut

JonnyMac

Here's another version to consider.  As I always wondered what actual game shows do in the case of a tie -- which can happen with button debouncing -- I decided to answer that myself: the device randomly chooses amongst the tied inputs, and nobody is the wiser....

' =========================================================================
'
'   File...... dk_jeopardy.bs2
'   Purpose...
'   Author....
'   E-mail....
'   Started...
'   Updated...
'
'   {$STAMP BS2}
'   {$PBASIC 2.5}
'
' =========================================================================


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


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


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

RstButton       PIN     15                      ' SETUP = DN
Buzzer          PIN     14                      ' SETUP = OUT
P13             PIN     13                      ' SETUP = OUT

CDLight5        PIN     12                      ' SETUP = OUT
CDLight4        PIN     11
CDLight3        PIN     10
CDLight2        PIN     9
CDLight1        PIN     8

Lamp4           PIN     7                       ' V+ / OUT7
Lamp3           PIN     6                       ' V+ / OUT6
Lamp2           PIN     5                       ' V+ / OUT5
Lamp1           PIN     4                       ' V+ / OUT4

EzButton4       PIN     3                       ' P3.W / P3.R
EzButton3       PIN     2                       ' P2.W / P2.R
EzButton2       PIN     1                       ' P1.W / P1.R
EzButton1       PIN     0                       ' P0.W / P0.R


CDLights        VAR     OUTH                    ' pins 8..12
Lamps           VAR     OUTB                    ' pins 4..7
EzButtons       VAR     INA                     ' pins 0..3


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

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

TrOn            CON     1
TrOff           CON     0

Yes             CON     1
No              CON     0


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

scan            VAR     Byte                    ' button inputs
idx             VAR     Byte
idx2            VAR     Byte
check           VAR     Byte

lottery         VAR     Word


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

Reset:
  OUTH = %01011111 : OUTL = %11110000         ' setup outputs
  DIRH = %00000000 : DIRL = %00000000         ' clear everything


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

Main:
  scan = %1111                                  ' arm inputs
  FOR idx = 1 TO 10
    scan = scan & EzButtons                     ' scan pins
    PAUSE 1
    RANDOM lottery
  NEXT

  IF (scan = %0000) THEN Main                   ' wait for input

Cnt_Buttons:
  check = 0
  FOR idx = 0 TO 3                              ' scan button bits
    check = check + scan.LOWBIT(idx)            ' add active inputs
  NEXT
  IF check = 1 THEN Show_Winner

  ' if "tie" then controller randomly selects a winner

Choose_Winner:
  RANDOM lottery                                ' stir random #
  idx = 1 << (lottery & %11)                    ' randomize selection
  check = scan & idx                            ' try randomized player
  IF (check = %0000) THEN                       ' no a winner
    GOTO Choose_Winner                          '  try again
  ELSE
    scan = idx                                  ' set to single output
  ENDIF

Show_Winner:
  Lamps = scan
  PAUSE 2000                                    ' wait 2 seconds


Count_Down:
  FOR idx = 0 TO 4                              ' 5 secs + off
    READ CD_Bits+idx, CDLights
    FOR idx2 = 1 TO 10
      PAUSE 10
      IF RstButton = IsOn THEN
        GOTO Reset
      ENDIF
    NEXT
  NEXT

  CDLights = IsOff                              ' kill lights
  Buzzer = IsOn
  PAUSE 2000

  GOTO Reset


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


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


' -----[ User Data ]-------------------------------------------------------

CD_Bits         DATA    %11111, %01111, %00111, %00011, %00001

Jon McPhalen
EFX-TEK Hollywood Office

Don Kuts

is there a test program we can put in this to test led's, inputs, and buzzer

bsnut

December 09, 2011, 04:38:14 PM #4 Last Edit: December 09, 2011, 08:00:54 PM by bsnut
Quote from: Don Kuts on December 09, 2011, 01:12:06 PM
is there a test program we can put in this to test led's, inputs, and buzzer
I don't understand what you want to do. Both of these programs are based on your first post and should work, since both of these programs will let you know if it works the way you want it to after you have connected everything up.
William Stefan
The Basic Stamp Nut

BigRez

Don,

Do you mean a special routine added to the program that runs at power-up to show each LED, input button, and buzzer is working before the game starts?  Yes, I think that would be a good idea.

bsnut

Don,

I updated my program so it will light your LEDs at power-up which let's you know if they are working correctly. If you want to test the inputs then we will need to add some more code that I already add.

William Stefan
The Basic Stamp Nut

The Chad

I am working with Don on this project, doing the programing.  I am still unable to get your program to work but have made my own crude program, the only thing I am missing is dealing with the ties.


' {$STAMP BS2}
' {$PBASIC 2.5}

'=========================Pin Config===============================
RstButton       PIN     15                      ' SETUP = DN
Buzzer          PIN     14                      ' SETUP = OUT

CDLight6        PIN     13
CDLight5        PIN     12                      ' SETUP = OUT
CDLight4        PIN     11
CDLight3        PIN     10
CDLight2        PIN     9
CDLight1        PIN     8

Lamp4           PIN     7                       ' V+ / OUT7
Lamp3           PIN     6                       ' V+ / OUT6
Lamp2           PIN     5                       ' V+ / OUT5
Lamp1           PIN     4                       ' V+ / OUT4

EzButton4       PIN     3                       ' P3.W / P3.R
EzButton3       PIN     2                       ' P2.W / P2.R
EzButton2       PIN     1                       ' P1.W / P1.R
EzButton1       PIN     0                       ' P0.W / P0.
'========================States==================================

IsOff           CON     0
IsOn            CON     1

command         VAR     Byte



'=========================Program================================
Main:
  IF (EzButton1 = IsOn) THEN Player_1
  IF (EzButton2 = IsOn) THEN Player_2
  IF (EzButton3 = IsOn) THEN Player_3
  IF (EzButton4 = IsOn) THEN Player_4


  GOTO Main

Player_1:
  HIGH CDLight1
  HIGH Lamp1
  PAUSE 1000
  LOW CDLight1
  HIGH CDLight2
  PAUSE 1000
  LOW CDLight2
  HIGH CDLight3
  PAUSE 1000
  LOW CDLight3
  HIGH CDlight4
  PAUSE 1000
  LOW CDLight4
  HIGH CDLight5
  PAUSE 1000
  LOW CDLight5
  HIGH CDLight6
  PAUSE 1000
  LOW CDLight6
  LOW Lamp1
    IF (RstButton = IsOn) THEN Main
  HIGH Buzzer
  PAUSE 500
  LOW Buzzer
  GOTO Main

Player_2:
HIGH CDLight1
  HIGH Lamp2
  PAUSE 1000
  LOW CDLight1
  HIGH CDLight2
  PAUSE 1000
  LOW CDLight2
  HIGH CDLight3
  PAUSE 1000
  LOW CDLight3
  HIGH CDlight4
  PAUSE 1000
  LOW CDLight4
  HIGH CDLight5
  PAUSE 1000
  LOW CDLight5
  HIGH CDLight6
  PAUSE 1000
  LOW CDLight6
  LOW Lamp2
    IF (RstButton = IsOn) THEN Main
  HIGH Buzzer
  PAUSE 500
  LOW Buzzer
  GOTO Main

  Player_3:
  HIGH CDLight1
  HIGH Lamp3
  PAUSE 1000
  LOW CDLight1
  HIGH CDLight2
  PAUSE 1000
  LOW CDLight2
  HIGH CDLight3
  PAUSE 1000
  LOW CDLight3
  HIGH CDlight4
  PAUSE 1000
  LOW CDLight4
  HIGH CDLight5
  PAUSE 1000
  LOW CDLight5
  HIGH CDLight6
  PAUSE 1000
  LOW CDLight6
  LOW Lamp3
    IF (RstButton = IsOn) THEN Main
  HIGH Buzzer
  PAUSE 500
  LOW Buzzer
  GOTO Main

  Player_4:
  HIGH CDLight1
  HIGH Lamp4
  PAUSE 1000
  LOW CDLight1
  HIGH CDLight2
  PAUSE 1000
  LOW CDLight2
  HIGH CDLight3
  PAUSE 1000
  LOW CDLight3
  HIGH CDlight4
  PAUSE 1000
  LOW CDLight4
  HIGH CDLight5
  PAUSE 1000
  LOW CDLight5
  HIGH CDLight6
  PAUSE 1000
  LOW CDLight6
  LOW Lamp4
    IF (RstButton = IsOn) THEN Main
  HIGH Buzzer
  PAUSE 500
  LOW Buzzer
  GOTO Main

  intro:
  HIGH CDLight1
  HIGH CDLight6
  HIGH lamp1
  LOW lamp4
  PAUSE 500
  LOW CDLight1
  LOW CDLight6
  HIGH CDLight2
  HIGH CDLight5
  LOW lamp1
  HIGH lamp2
  PAUSE 500
  HIGH CDLight3
  HIGH CDLight4
  LOW CDLight2
  LOW CDLight5
  LOW lamp2
  HIGH lamp3
  PAUSE 500
  LOW CDLight4
  LOW CDLight3
  LOW lamp3
  HIGH lamp4
  IF command = "2" THEN GOTO Main
  GOTO main


The last part of this is playing with serial controls.  I can get the serial control to work but I loose all of the inputs from the pins.  Can I due both?  I just want to send a basic code via serial to prop2 but if the serial cable is plugged in I loose any inputs from the ez buttons.

bsnut

Welcome to the forums The Chad.

The reason why the program you wrote don't work as you want to, is how fast the Basic Stamp 2 scans it's code and what is happening it is missing when you push the buttons. One way to take care of this problem is placing a PAUSE 100 in the beginning of your main loop. You also can use my code or Jon's code instead.
William Stefan
The Basic Stamp Nut

The Chad

actually the code works, as long as I am not trying to combine with serial control.  The programs that are not working are the two posted above.  I am just having issues with the tie, and accepting serial commands and inputs from the easy buttons.   Is it possible to get inputs from both sources at the same time?  Thanks for your patience I am still a beginner.

JonnyMac

The BASIC Stamp 2 -- core processor of the Prop-2 controller -- does not buffer serial input so unless you're sitting on a SERIN statement when your serial input comes, it will be missed.
Jon McPhalen
EFX-TEK Hollywood Office

The Chad

Yes I was using a serin, but when I had it in the code the ezbuttons did not work.  It seemed as though I could use serin or pins.

JonnyMac

SERIN -- when used without a timeout -- will hold up the program until some input arrives.
Jon McPhalen
EFX-TEK Hollywood Office

The Chad

Here is my code with the SERIN could you help me with the time out?

Thanks,
The Chad



' {$STAMP BS2}
' {$PBASIC 2.5}

'=========================Pin Config===============================
RstButton       PIN     15                      ' SETUP = DN
Buzzer          PIN     14                      ' SETUP = OUT

CDLight6        PIN     13
CDLight5        PIN     12                      ' SETUP = OUT
CDLight4        PIN     11
CDLight3        PIN     10
CDLight2        PIN     9
CDLight1        PIN     8

Lamp4           PIN     7                       ' V+ / OUT7
Lamp3           PIN     6                       ' V+ / OUT6
Lamp2           PIN     5                       ' V+ / OUT5
Lamp1           PIN     4                       ' V+ / OUT4

EzButton4       PIN     3                       ' P3.W / P3.R
EzButton3       PIN     2                       ' P2.W / P2.R
EzButton2       PIN     1                       ' P1.W / P1.R
EzButton1       PIN     0                       ' P0.W / P0.
'========================States==================================

IsOff           CON     0
IsOn            CON     1

command         VAR     Byte



'=========================Program================================
Main:
  IF (EzButton1 = IsOn) THEN Player_1
  IF (EzButton2 = IsOn) THEN Player_2
  IF (EzButton3 = IsOn) THEN Player_3
  IF (EzButton4 = IsOn) THEN Player_4
  SERIN 16, 16468, [STR command\1]
  IF command = "1" THEN GOTO intro


  GOTO Main

Player_1:
  HIGH CDLight1
  HIGH Lamp1
  PAUSE 1000
  LOW CDLight1
  HIGH CDLight2
  PAUSE 1000
  LOW CDLight2
  HIGH CDLight3
  PAUSE 1000
  LOW CDLight3
  HIGH CDlight4
  PAUSE 1000
  LOW CDLight4
  HIGH CDLight5
  PAUSE 1000
  LOW CDLight5
  HIGH CDLight6
  PAUSE 1000
  LOW CDLight6
  LOW Lamp1
    IF (RstButton = IsOn) THEN Main
  HIGH Buzzer
  PAUSE 500
  LOW Buzzer
  GOTO Main

Player_2:
HIGH CDLight1
  HIGH Lamp2
  PAUSE 1000
  LOW CDLight1
  HIGH CDLight2
  PAUSE 1000
  LOW CDLight2
  HIGH CDLight3
  PAUSE 1000
  LOW CDLight3
  HIGH CDlight4
  PAUSE 1000
  LOW CDLight4
  HIGH CDLight5
  PAUSE 1000
  LOW CDLight5
  HIGH CDLight6
  PAUSE 1000
  LOW CDLight6
  LOW Lamp2
    IF (RstButton = IsOn) THEN Main
  HIGH Buzzer
  PAUSE 500
  LOW Buzzer
  GOTO Main

  Player_3:
  HIGH CDLight1
  HIGH Lamp3
  PAUSE 1000
  LOW CDLight1
  HIGH CDLight2
  PAUSE 1000
  LOW CDLight2
  HIGH CDLight3
  PAUSE 1000
  LOW CDLight3
  HIGH CDlight4
  PAUSE 1000
  LOW CDLight4
  HIGH CDLight5
  PAUSE 1000
  LOW CDLight5
  HIGH CDLight6
  PAUSE 1000
  LOW CDLight6
  LOW Lamp3
    IF (RstButton = IsOn) THEN Main
  HIGH Buzzer
  PAUSE 500
  LOW Buzzer
  GOTO Main

  Player_4:
  HIGH CDLight1
  HIGH Lamp4
  PAUSE 1000
  LOW CDLight1
  HIGH CDLight2
  PAUSE 1000
  LOW CDLight2
  HIGH CDLight3
  PAUSE 1000
  LOW CDLight3
  HIGH CDlight4
  PAUSE 1000
  LOW CDLight4
  HIGH CDLight5
  PAUSE 1000
  LOW CDLight5
  HIGH CDLight6
  PAUSE 1000
  LOW CDLight6
  LOW Lamp4
    IF (RstButton = IsOn) THEN Main
  HIGH Buzzer
  PAUSE 500
  LOW Buzzer
  GOTO Main

  intro:
  HIGH CDLight1
  HIGH CDLight6
  HIGH lamp1
  LOW lamp4
  PAUSE 500
  LOW CDLight1
  LOW CDLight6
  HIGH CDLight2
  HIGH CDLight5
  LOW lamp1
  HIGH lamp2
  PAUSE 500
  HIGH CDLight3
  HIGH CDLight4
  LOW CDLight2
  LOW CDLight5
  LOW lamp2
  HIGH lamp3
  PAUSE 500
  LOW CDLight4
  LOW CDLight3
  LOW lamp3
  HIGH lamp4
  IF command = "2" THEN GOTO Main
  GOTO main

JonnyMac

December 28, 2011, 03:47:47 PM #14 Last Edit: December 28, 2011, 03:51:24 PM by JonnyMac
Pretty easy: you add how long you're willing to wait (in milliseconds) and where to go if there is a timeout.  Please consult the PBASIC help file for additional details.

Main:
  IF (EzButton1 = IsOn) THEN Player_1
  IF (EzButton2 = IsOn) THEN Player_2
  IF (EzButton3 = IsOn) THEN Player_3
  IF (EzButton4 = IsOn) THEN Player_4

  SERIN 16, 16468, 250, Main, [command]

  IF (command = "1") THEN
    GOTO Intro
  ELSE
    GOTO Main
  ENDIF


You do not need to use STR in the SERIN function for a single character -- this just slows things down.  And, just to be clear, doing what you want to do is going to be tricky at best because you have a very short window in which to get the character to be received.
Jon McPhalen
EFX-TEK Hollywood Office