PDA

View Full Version : help with program



rr
08-23-2005, 04:53 PM
hello...

i have created a circuit with one button and twelve npn transistors that switch clusters of leds. the button is on pin 0 and the transistors are on pin 1 through 12. the circuit uses a bs2 oem.

i would like to create a program that works like this:
when a person pushes the button the transistors will randomly be switched on and off one at atime untill all the transistors have been on. At the end of the sequence one of the transitors wll randomly switch on·and and blink for 5 seconds then turn off.

basically what i have done is built a decsion maker. i have 12 ligh boxes with words on them and they are back lit with leds. when a viewer pushes the button i want the machine to light up each word randomly for about 2 seconds and then in the end randomly blink one of the words(the answer) for about five seconds.

i know that i can use the random comand and an exteral button with decent random results.·with the random variable i know i can randomly light up the boxes but how do i randomly light up the all the 12 boxes without repeating.

i would like to make the program as random as possible...

any hints, suggestions or ideas would be great.
i greatly value all the help...
·

Jon Williams
08-23-2005, 09:16 PM
The key to using RANDOM is to let it tumble until the external event. If your outputs are connected to P0 - P11, you could do something like this:

Reset:
· flags = 0

Main:
· DO
··· RANDOM selection
· LOOP UNTIL (Play = Pressed)

Get_Card:
· theCard = selection // 12················· ' create card #, 0 - 11
· IF (flags.LOWBIT(theCard) = 1) THEN······· ' already been seen?
··· RANDOM selection························ ' yes, tumble again
··· GOTO Get_Card··························· ' get new card
· ELSE
··· flags.LOWBIT(theCard) = 1··············· ' mark card
· ENDIF

·
Show_Card:
· HIGH theCard
· PAUSE ShowTime
· LOW theCard

· IF (flags < %0000111111111111) THEN Main

Final_Card:
· RANDOM selection
· theCard = selection // 12

· FOR idx = 1 TO 6
··· TOGGLE theCard
··· PAUSE FlashRate
· NEXT

· GOTO Reset

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Post Edited (Jon Williams (Parallax)) : 8/23/2005 1:16:47 PM GMT

metron9
08-23-2005, 10:30 PM
On the new TV Program called "Numbers" the math genius did an experiment. He told 6 people distribute themselves in a random pattern around the room. After they positioned themselves he showed them that what they actually did was evenly distribute themselves around the room with about the same spaceing between them. This relates to your Random problem. As you say you want all of the lights to get the chance to be selected. The program Jon has created will do the job but it wont light up the same word twice for 12 times through the loop. That may be what you are looing for but the player will get better odds of guessing on each time through the loop because he knows the previous selections and has a 50% chance of selecting the final 1 of 2 that is if he can remember the previous 10 words, like card counting.

To solve this problem and make the routine seem more random would be to instead of setting a bit for each answer each time through, you could use a byte counter and increment that byte counter each time the light was selected. Instead of checking if it has been seen you can use another random generator to determin if that box should be selected by using the incremented byte value, taking 10% of that value and getting a random number from the range of the byte. If the number is within the 10% it would use that light again. Using a third random generator for the percentage range of the byte could also be used.

Example:

Light 1 has been shown once, add 10 to byte counter (instead of setting bit flag) - do random on that value of 10, if the number is 1 show it again, byte counter adds 10 and is now 20, if its hit again random on 20 and if its less than 10% of 20 (2) show it again. By using a random number instead of fixed 10% you would start to get a much better random selection but still weighted twards the lights that have not yet been selected so they all will be shown but with previous ones popping up out of the blue.

metron9
08-23-2005, 11:12 PM
Actually after driving to work this morning I started thinking of all the time the above functions would take, more time as each light was selected so I came up with this.

Set 24 bytes of memory aside for 2 - 12 element arrays (like 2 decks of cards of 12 cards)
Plase numbers 1 through 12 in the first 12 locations and 1 - 12 in the second array

When your program starts do a shuffel like a deck of cards like this

for i=1 to 100
x=random(12)
y=random(12)
tmp=array(x) 'swap
x=array(y)
y=tmp
next

do the same for the second array

During your program start with array#1 and just use the values stored starting with array#1(1) thru array#1(12)
Do a swap routine on the unused array in between lines of your other code.
Next game use array#2 and do the swapping on array#1 during program execution

That way you have instant random numbers without all that searching for the next random number.

Jon Williams
08-24-2005, 01:49 AM
Interesting ... let's see that in PBASIC.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

rr
08-24-2005, 02:53 AM
jon thanks for the help

in your code you have a line...




IF (flags < %0000111111111111) THEN Main


wouldnt this be for out puts 4 through 15???

wouldnt i want...

IF (flags < %1111111111110000) THEN Main

for outputs 0 through 11????

also i like metron's ideas but it is a little over my head...

Chris Savage
08-24-2005, 03:59 AM
Jon's code is accurate.· Binary numbers are read from right to left, LSB to MSB.· IOW, the MSB is always on the left.· BTW, if you want to see how Binary numbers are organized, and their relevence to their Decimal counterpart, open your Windows Calculator, delect View/Scientific, and type a number in, the click Binary.· Good learning tool in a way.· You can also see the number in Hex and Octal.


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)

Jon Williams
08-24-2005, 06:49 AM
If you have your PBASIC editor open, you can also do this:

DEBUG BIN16 anyValue

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

Chris Savage
08-24-2005, 06:53 AM
That's true...Why leave the Stamp Editor at all?· =) It's almost a second home...


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com (mailto:csavage@parallax.com)