' ========================================================================= ' File....... SW21-EX06-Las_Vegas.SXB ' Purpose.... Bar or Dot Graph with LEDs ' Author..... (C) 2000 - 2005, Parallax, Inc. ' E-mail..... support@parallax.com ' Started.... 01 SEP 2005 ' Updated.... 23 MAR 2007 by TSaavik for SX28 ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' ' This program simulates a very simple slot machine game, complete with ' sound FX. The constants TAdj and FAdj may require adjustment when using ' on faster BASIC Stamp modules. '------{ Device Settings ]------------------------------------------------- DEVICE SX28,TURBO,BANKS8,OSCHS3,SYNC,OPTIONX FREQ 50_000_000 ID "EX03" ' -----[ I/O Definitions ]------------------------------------------------- LEDs VAR RB ' LEDs on RB.0 - RB.7 'Pot VAR RA.0 ' Pot circuit IO Speaker VAR RC.0 ' speaker output PlayBtn VAR RA.1 ' button input to play ' -----[ Constants ]------------------------------------------------------- TAdj CON $100 ' time adjust factor FAdj CON $100 ' frequency adjust factor no CON 1 yes CON 0 ' -----[ Variables ]------------------------------------------------------- rndVal VAR Byte ' random number pattern VAR Byte ' light pattern tone VAR Word ' tone output swData VAR Byte ' workspace for BUTTON delay VAR Word ' delay while "spinning" spin1 VAR Byte ' loop counter spin2 VAR Byte ' loop counter adjustedFreq VAR Byte ' to hold tone */ FAdj Time VAR Byte tmpB1 VAR Byte TmpB2 VAR Byte TmpW1 VAR Word ' -----[ Initialization ]------------------------------------------------- PROGRAM Start watch rndVal, 8, UBIN watch pattern watch tone watch swData watch delay watch spin1 watch spin2 watch adjustedFreq watch Time watch tmpB1 watch TmpB2 watch PlayBtn watch LEDs, 8, UBIN ' -----[Subs ]------------------------------------------------------------ BREAKS Sub ' -----[ Program Code ]---------------------------------------------------- Start: LEDS = %00000000 ' start with right LED on Tris_B = %00000000 ' make LEDs outputs Main: DO 'GOSUB Get_Random ' get random number/tone 'adjustedFreq = tone */ FAdj 'FREQOUT Speaker, adjustedTime, adjustedFreq ' FREQOUT pin, duration(0-65535), Freq1(0-32767) Sound Speaker, 90, 10 ' Sound pin, Note(0-127), Duration(1-255) Sound Speaker, 110, 10 ' Sound pin, Note(0-127), Duration(1-255) Sound Speaker, 90, 10 ' Sound pin, Note(0-127), Duration(1-255) PAUSE 100 ' delay between rolls play: 'DO WHILE PlayBtn = no ' loop goto Spin Pause 10 LOOP Spin: LEDs = %00111111 ' simulate machine reset PAUSE 750 LEDs = %00000000 PAUSE 500 delay = 75 ' initialize delay FOR spin1 = 1 TO 25 ' spin the wheel GOSUB Get_Random ' get random number 'FREQOUT Speaker, 25 */ TAdj, 425 */ FAdj ' wheel click Sound Speaker, 110 , 30 ' wheel click PAUSE delay ' pause between clicks delay = delay */ $0119 ' multiply delay by 1.1 NEXT IF pattern = %00111111 THEN ' if all lit, you win FOR spin1 = 1 TO 4 FOR spin2 = 0 TO 3 LOOKUP spin2, $00, $0C, $12, $21, LEDs LOOKUP spin2, 80, 90, 100, 110, tone 'FREQOUT Speaker, 35 */ TAdj, tone */ FAdj Sound Speaker, 35, tone PAUSE 65 NEXT NEXT ELSE Sound Speaker, 80, 75 ' otherwise, groan... Sound Speaker, 60, 150 ENDIF Clear_Game: LEDs = %00000000 ' clear LEDs GOTO Play ' do it again ' -----[ Subroutines ]----------------------------------------------------- Get_Random: RANDOM rndVal ' get pseudo-random number tone = rndVal & $7FF ' keep in reasonable range pattern = rndVal & %00111111 ' mask out unused bits LEDs = pattern Breaks RETURN BREAKS: Break Return