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

result  VAR     Word
final    VAR     Byte
row3  CON 0
row2  CON 1
r1  CON 2
LED1  CON 11
LED2  CON 10
LED3  CON 9
LED4  CON 8
LED5  CON 7
LED6  CON 6
LED7  CON 5
LED8  CON 4
LED9  CON 3
spk CON 12
spgnd CON 13


OUTPUT 0
OUTPUT 1
OUTPUT 2
OUTPUT 3
OUTPUT 4
OUTPUT 5
OUTPUT 6
OUTPUT 7
OUTPUT 8
OUTPUT 9
OUTPUT 10
OUTPUT 11
OUTPUT 12
OUTPUT 13

HIGH r1
HIGH row2
HIGH row3
LOW LED1
LOW LED2
LOW LED3
LOW LED4
LOW LED5
LOW LED6
LOW LED7
LOW LED8
LOW LED9
LOW spgnd

DO
counter VAR  Byte

 result = 1111            ' set initial "seed" value


Main:
FOR counter = 1 TO 27

final =((result//27) +1)
RANDOM result                    ' generate random number
PAUSE 100
DEBUG DEC ? final              ' show the result on screen


 IF (final = 1) THEN
   LOW row3
   HIGH led1
   FREQOUT Spk,100,1000
   HIGH r1
   LOW led1
  ELSEIF (final = 2) THEN
   LOW row3
   HIGH led2
   FREQOUT Spk,100,2000
   HIGH r1
   LOW led2
  ELSEIF (final = 3) THEN
   LOW row3
   HIGH led3
   FREQOUT Spk,100,3000
   HIGH r1
   LOW led3
     ELSEIF (final = 4) THEN
   LOW row3
   HIGH led4
   FREQOUT Spk,100,4000
   HIGH r1
   LOW led4
     ELSEIF (final = 5) THEN
   LOW row3
   HIGH led5
   FREQOUT Spk,100,5000
   HIGH r1
   LOW led5
     ELSEIF (final = 6) THEN
   LOW row3
   HIGH led6
   FREQOUT Spk,100,6000
   HIGH r1
   LOW led6
     ELSEIF (final = 7) THEN
   LOW row3
   HIGH led7
   FREQOUT Spk,100,7000
   HIGH r1
   LOW led7
     ELSEIF (final = 8) THEN
   LOW row3
   HIGH led8
   FREQOUT Spk,100,8000
   HIGH r1
   LOW led8
    ELSEIF (final = 9) THEN
   LOW row3
   HIGH led9
   FREQOUT Spk,100,9000
   HIGH r1
   LOW led9

  ELSEIF (final = 10) THEN
   LOW row2
   HIGH r1
   HIGH led1
   FREQOUT Spk,100,10000
   HIGH row2
   HIGH r1
   LOW led1
        ELSEIF (final = 11) THEN
   LOW row2
HIGH r1
   HIGH led2
   FREQOUT spk,100,11000
   HIGH row2
   LOW led2
        ELSEIF (final = 12) THEN
   LOW row2
   HIGH r1
   HIGH led3
   FREQOUT spk,100,12000
   HIGH row2
   HIGH r1
   LOW led3
        ELSEIF (final = 13) THEN
   LOW row2
   HIGH r1
   HIGH led4
   FREQOUT spk,100,13000
   HIGH row2
   HIGH r1
   LOW led4
        ELSEIF (final = 14) THEN
   LOW row2
   HIGH r1
   HIGH led5
   FREQOUT spk,100,14000
   HIGH row2
   HIGH r1
   LOW led5
        ELSEIF (final = 15) THEN
   LOW row2
   HIGH r1
   HIGH led6
   FREQOUT spk,100,15000
   HIGH row2
   LOW led6
        ELSEIF (final = 16) THEN
   LOW row2
   HIGH r1
   HIGH led7
   FREQOUT spk,100,16000
   HIGH row2
   HIGH r1
   LOW led7
           ELSEIF (final > 16) THEN
    IF (final = 17) THEN
   LOW row2
   HIGH r1
   HIGH led8
   FREQOUT spk,100,17000
   HIGH row2
   HIGH r1
   LOW led8
        ELSEIF (final = 18) THEN
   LOW row2
   HIGH led9
   HIGH r1
   FREQOUT spk,100,18000
   HIGH row3
   HIGH r1
   LOW led9
        ELSEIF (final = 19) THEN
   HIGH row3
   HIGH row2
   LOW r1
   HIGH led1
   FREQOUT spk,100,19000
   HIGH row3
   HIGH row2
   HIGH r1
   LOW led1
        ELSEIF (final = 20) THEN
   HIGH row3
   HIGH row2
   LOW r1
   HIGH led2
   FREQOUT spk,100,20000
   HIGH row3
   HIGH row2
   HIGH r1
   LOW led2
        ELSEIF (final = 21) THEN
   HIGH row3
      HIGH row2
   LOW r1
   HIGH led3
   FREQOUT spk,100,21000
   HIGH row3
      HIGH row2
   HIGH r1
   LOW led3
        ELSEIF (final = 22) THEN
   HIGH row3
      HIGH row2
   LOW r1
   HIGH led4
   FREQOUT spk,100,22000
   HIGH row3
      HIGH row2
   HIGH r1
   LOW led4
        ELSEIF (final = 23) THEN
   HIGH row3
      HIGH row2
   LOW r1
   HIGH led5
   FREQOUT spk,100,23000
   HIGH row3
      HIGH row2
   HIGH r1
   LOW led5
        ELSEIF (final = 24) THEN
   HIGH row3
      HIGH row2
   LOW r1
   HIGH led6
   FREQOUT spk,100,24000
   HIGH row3
      HIGH row2
   HIGH r1
   LOW led6
        ELSEIF (final > 24) THEN
    IF (final = 25) THEN
   LOW row2
   HIGH led7
      HIGH row2
   LOW r1
   FREQOUT spk,100,25000
   HIGH row2
      HIGH row2
   HIGH r1
   LOW led7
        ELSEIF (final = 26) THEN
   HIGH row3
      HIGH row2
   LOW r1
   HIGH led8
   FREQOUT spk,100,26000
   HIGH row3
      HIGH row2
   HIGH r1
   LOW led8
        ELSEIF (final = 27) THEN
   HIGH row3
      HIGH row2
   LOW r1
   HIGH led7
   FREQOUT spk,100,27000
   HIGH row3
      HIGH row2
   HIGH r1
   LOW led7

   HIGH row3
HIGH row2
HIGH row3
LOW LED1
LOW LED2
LOW LED3
LOW LED4
LOW LED5
LOW LED6
LOW LED7
LOW LED8
LOW LED9

   ENDIF
   ENDIF
   ENDIF

NEXT
LOOP