14M dice Roll

rolsen

Member
After much trial with random (dice roll always stopped on same number)
I decided change tack and try interupt as a solution. It appears to work!

Momentry Button on input 0 commences action. Code as follows:
dice:
setint %00000001,%00000001

main:
for b0= 0 to 5
low 1 ‘ switch output 1 off
pause 2000
next b0 ‘ wait 2 seconds
goto main
‘ loop back to start
interrupt:
gosub throw ‘ switch output 1 on
if pin1 = 1 then interrupt ‘ loop here until the
‘ interrupt cleared
pause 2000 ‘ wait 2 seconds
setint %00000001,%00000001 ‘ re-activate interrupt
return ‘ return from sub

throw:
faster:
pause 50
let b6=2
goto chase

chase:
for b5=1 to 40
inc b6
pause b6
pins=%00000001
pause b6
pins=%00000010
pause b6
pins=%00000100
pause b6
pins=%00001000
pause b6
pins=%00010000
pause b6
pins=%00100000
pause b6
pins=%00000000
if b6=40 then reveaL
next b5

reveal:
if b0=0 then goto one
if b0=1 then goto two
if b0=2 then goto three
if b0=3 then goto four
if b0=4 then goto five
if b0=5 then goto six
return

one:
high 0
wait 4
low 0
return

two:
high 1
wait 4
low 1
return

three:
high 2
wait 4
low 2
return

four:
high 3
wait 4
low 3
return

five:
high 4
wait 4
low 4
return

six:
high 5
wait 4
low 5
return

I would be interested in any comments.
 

ylp88

Senior Member
Remember that the random number generator is not "random", rather "pseudo-random". As a consequence, you need to start it off with something which is random - continually generating random numbers by placing it in a loop until the user presses the "roll" button (the random element is this bit where the time until the user presses the button is "random") will achieve this. If you don't do this then the numbers generated will be the same after powerup. You've essentially used this "randomness" of the time until the button press to generate your own "random" number - I'm not sure why you have the "pause 2000" command, though.

There are probably some code optimisations you can do, but I will say that given the expanded form it is quite readable.

ylp88
 
Last edited:

hippy

Ex-Staff (retired)
You should be able to do this without interrupts. There's nothing intrinsically different between a button push jumping into the interrupt and calling "throw" or in polling that button and calling "throw" that way.

I suggest backing-up this version, changing it to do polling and getting it to work that way.

Also - and this applies to everyone - when announcing "this works" when something else did not, it always helps to be clear if talking about whether that's on a physical PICAXE, in the simulator, or using VSM. The simulator and VSM are not, and likely never will be, perfect models of a real PICAXE and identifying things which don't work as expected will help get things fixed if broken and make people aware of what differences / problems there may be.
 

rolsen

Member
thanks YLP88 and Hippy. Hippy Your points are really helpful, as allways. I have noted your advice on "it works". Good point. As ever, for myself, picaxe is an ongoing enjoyable learning experience. Much appreciated.
 
Top