Posts: 11456
-
Karma: 634
'{$STAMP BS2}
'{$PBASIC 2.5}
'----------Title----------------------
'File: Magic 8 Ball
'Author: Erich Sparks
'Revision: 1
'Date: 10/03/06
'----------Program Description---------
'----------I/O Definitions-------------
LEDs VAR OUTL 'light control outputs
LEDsDirs VAR DIRL 'DIRS control for LEDs
LED0 PIN 0 'Led outputs
LED1 PIN 1 'Led outputs
LED2 PIN 2 'Led outputs
LED3 PIN 3 'Led outputs
LED4 PIN 4 'Led outputs
LED5 PIN 5 'Led outputs
LED6 PIN 6 'Led outputs
LED7 PIN 7 'Led outputs
PlayBtn PIN 8 'button input to play
Speaker PIN 10 'speaker output
'----------Constants-------------------
TAdj CON $100 'time adjust factor
FAdj CON $100 'Frequeny adjust factor
'----------Variables-------------------
rndVal VAR Word 'random number
pattern VAR Byte 'light pattern
tone VAR Word 'tone output
swData VAR Byte 'workspace for BUTTON
offset VAR Byte ' offset into patterns
mode VAR Byte
delay VAR Word ' time between patterns
'----------EEPROM--------------
SeqA DATA %000001, %000010, %000100, %001000, %010000
DATA %100000
SeqB DATA %100000, %010000, %001000, %000100, %000010
DATA %000001, %000010, %000100, %001000, %010000
SeqC DATA %000000, %001100, %010010, %100001
SeqD DATA %100100, %010010, %001001
SeqE DATA %0
AMax CON SeqB - SeqA 'calculate length
BMax CON SeqC - SeqB
CMax CON SeqD - SeqC
DMax CON SeqE - SeqD
'----------Initialization--------------
Reset:
LedsDirs = %00111111 'make outputs
'----------Main Code-------------------
Main:
DO
GOSUB Get_Random 'get random number/tone
FREQOUT Speaker, 25 */ TAdj, tone */ FAdj 'sound the tone
PAUSE 100
BUTTON PlayBtn, 0, 255, 10, swData, 1, SPin 'Check for play
LOOP
Spin:
RANDOM rndVal 'get random number
LEDs = rndVal & %00111111 'light random channels
tone = rndVal & $7FF 'get random tone
FREQOUT Speaker, 25 */ TAdj, tone */ FAdj 'random output of sound
'----------Subroutines-----------------
Get_Random:
RANDOM rndVal 'get pseudo-random number
tone = rndVal & $7FF 'keep in reasonable range
pattern = rndVal & %00111111 'mask out unused bets
LEDs = pattern 'show the pattern
RETURN