duration of seven seg
userjyjy
Posts: 30
in BASIC Stamp
I have a code that will cause the seven seg to count random numbers 0-9 once a button is pressed. I am have problems getting this to last for only for a certain amount of seconds though.
This is my code
"index VAR NIB
result VAR WORD
counter VAR WORD
time VAR WORD
duration 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 ? counter
LOOP
ENDIF
LOOP"
This is my code
"index VAR NIB
result VAR WORD
counter VAR WORD
time VAR WORD
duration 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 ? counter
LOOP
ENDIF
LOOP"
Comments
You can count twelve "PAUSE 1000"s, for example. Think about it.
One loop runs while the button is not pressed and displays the counter value.
The second loop runs while the button is pressed and counts from 0 to 9. If you want the display blanked do that before you enter the second loop.
Like kwinn said...
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 ? counter
LOOP
ENDIF
LOOP
index VAR NIB
result VAR WORD
counter VAR WORD
time VAR WORD
duration VAR WORD
result = 12000
DO
RANDOM result
DEBUG ? IN0
IF (IN0 = 1) THEN
FOR counter = 0 TO 9
FOR counter = 0 TO 300
OUTH = %00000000
DIRH = %11111111
RANDOM result
index = result//10
LOOKUP index , [ %11100111, %10000100, %11010011,
%11010110, %10110100, %01110110,
%01110111, %11000100, %11110111, %11110110 ], OUTH
DEBUG " ", DEC2 index, " ", BIN8 OUTH, CR
NEXT
NEXT
ENDIF
DEBUG ? counter
LOOP
DO
RANDOM result
DEBUG ? IN0
IF (IN0 = 1) THEN
FOR counter = 1 TO 1
FOR counter = 1 TO 10
OUTH = %00000000
DIRH = %11111111
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 20
NEXT
FOR counter = 1 TO 5
OUTH = %00000000
DIRH = %11111111
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 40
NEXT
FOR counter = 1 TO 3
OUTH = %00000000
DIRH = %11111111
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 66
NEXT
FOR counter = 1 TO 2
OUTH = %00000000
DIRH = %11111111
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 100
NEXT
FOR counter = 1 TO 1
OUTH = %00000000
DIRH = %11111111
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 200
NEXT
FOR counter = 1 TO 1
OUTH = %00000000
DIRH = %11111111
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 400
NEXT
NEXT
RANDOM result
index = result//10
LOOKUP index , [ %11100111, %10000100, %11010011,
%11010110, %10110100, %01110110,
%01110111, %11000100, %11110111, %11110110 ], OUTH
ENDIF
DEBUG ? counter
LOOP
By the above post and the code you have posted do you mean that when the button is pressed you want to generate a random number using the "random" command and then display it for a specific period of time (12 seconds) and then blank the display?
I added pauses to each instruction to try to simulate a dice slowing down Every 2 seconds and then finally landing on a number at the end of the 12 seconds .
quote="kwinn;c-1450429"]I think I may be misunderstanding what you are trying to do.
By the above post and the code you have posted do you mean that when the button is pressed you want to generate a random number using the "random" command and then display it for a specific period of time (12 seconds) and then blank the display?[/quote]
Loops inside loops:
See what it does. Study it in concert along with re-reading the relevant pages in the BASIC book. Change it to see how the results vary.
You will almost certainly want to fiddle the constants 6 and 4, and likely the constants that end up in period.
I see I wrote loopup when i meant LOOKUP.
When you post more code, be sure and use the big "C" in the formatting bar so the indentations are preserved.
This code Tom posted should get you close to the effect you want. Just keep in mind that pause is in milliseconds so the sum of all the numbers in the lookup should be close to the total time you want (so 12,000 for 12 seconds). The time it takes for the code to execute will add an insignificant bit of time to the total.