Shop OBEX P1 Docs P2 Docs Learn Events
Random Function using index — Parallax Forums

Random Function using index

I'm working on a project where i need my seven seg to act as a dice and to randomize. I can't seem to figure out how to use the random function with the index. I currently have a button controlling the seven seg. When I press and hold it currently counts from 0 to 9 whenever i release it stops on whatever number it is.

Here is my code

"' {$STAMP BS2}
' {$PBASIC 2.5}
index VAR NIB

DO
DEBUG ? IN0
IF (IN0 = 1) THEN
OUTH = %00000000
DIRH = %11111111
FOR index = 0 TO 9
DO
LOOKUP index , [ %11100111, %10000100, %11010011,
%11010110, %10110100, %01110110,
%01110111, %11000100, %11110111, %11110110 ], OUTH
DEBUG " ", DEC2 index, " ", BIN8 OUTH, CR
PAUSE 1000
LOOP UNTIL IN0=1
NEXT
DIRH = %00000000
ENDIF
LOOP "

the random instruction seems to not be working with it.

Comments

  • Try this:
     ' {$STAMP BS2}
     ' {$PBASIC 2.5}
     index VAR NIB
     result VAR WORD
     
     DO
      RANDOM result
      DEBUG ? IN0
      IF (IN0 = 1) THEN
       OUTH = %00000000
       DIRH = %11111111
       DO
        RANDOM result
        index = result//10
        LOOKUP index , [ %11100111, %10000100, %11010011,%11010110, %10110100, %01110110,%01110111, %11000100, %11110111, %11110110 ], OUTH
        DEBUG " ", DEC2 index, " ", BIN8 OUTH, CR
        PAUSE 1000
       LOOP UNTIL IN0=1
       DIRH = %00000000
      ENDIF
     LOOP
    
  • Thank you so much man. That worked. One last question. What instruction would I use to count how long the button is pressed? Would it be a counter?
    Sapphire wrote: »
    Try this:
     ' {$STAMP BS2}
     ' {$PBASIC 2.5}
     index VAR NIB
     result VAR WORD
     
     DO
      RANDOM result
      DEBUG ? IN0
      IF (IN0 = 1) THEN
       OUTH = %00000000
       DIRH = %11111111
       DO
        RANDOM result
        index = result//10
        LOOKUP index , [ %11100111, %10000100, %11010011,%11010110, %10110100, %01110110,%01110111, %11000100, %11110111, %11110110 ], OUTH
        DEBUG " ", DEC2 index, " ", BIN8 OUTH, CR
        PAUSE 1000
       LOOP UNTIL IN0=1
       DIRH = %00000000
      ENDIF
     LOOP
    

  • Yes, a variable that you increment each time after the Pause would count how long the button is pressed.
  • what if i need the random to be high for only 12 seconds?
    Sapphire wrote: »
    Yes, a variable that you increment each time after the Pause would count how long the button is pressed.

Sign In or Register to comment.