Random procedure

mokane439

New Member
Hi, I'm using picaxe for my gcse project at school and I have a few issues that are really driving me crazy. I'm making a reaction game with 5 led's and 5 corresponding switches, so when an led lights the corresponding switch must be pressed for it to go off, then triggering another led to light and so on for a period of time. I have each led/switch pair in procedures but need to know if theres any way to produce a random sequence of these procedures as so far can only produce a predetermined sequence, something that doesn't suit my purpose. Does anyone know of any form of solution?
 

westaust55

Moderator
I believe that the Blogs section was intended more for folks to progressively post their progress on their PICAXE projects.

For questions such as yours, you will receive far more feedback by posting in the Active PICAXE forum area.
 

shakespeare

New Member
I did a pair of dice with a picaxe 18A, and also couldn't find a RND() type of function. What I did was to run a counter whilst waiting for the roll button to be pushed. The counter is a B memory, so will be a value between 0 and 255 that is unpredictable ("Random") because it counts so fast.

Like this:

rnd:
b7=b7+1 'b7 will go back to 0 after 255
if pin7=0 then rnd 'when you press the switch, b7 will stop counting

You can add a limit value in to get a smaller range:

rnd:
b7=b7+1
b7 =b7 mod 5 'b7 will go back to 0 after 4
if pin7=0 then rnd 'when you press the switch, b7 will stop counting

Finally, remember that you can generate the number anytime before you use it, so it could be generated from the previous round's play. Say that the 5 switches are normally at logic 0, and are on pins 0 to 4:

rnd:
b7=b7+1
b7=b7 mod 5 'b7 will go back to 0 after 5
if pins and %b00011111=0 then rnd 'when you press any switch, b7 will
'stop counting

Hope this helps
 

westaust55

Moderator
Looking at PICAXE manual 2 (V7.7) page 168 there is a RANDOM fucntion even for the 18A.

Therefor for a dice where the result is to be 1 to 6, you can try:

RANDOM w1
b5 = w1 // 6 + 1 ; result is 1 to 6
 
Top