PDA

View Full Version : random out

MR SPARKY
09-16-2008, 03:28 PM
I am trying to get a BS2IC to randomly turn on 5 pins. The senario is that i need 3 sets of 5 pins to go low randomly. one pin from each set at the same time.· EX: "set 1 pin0-4 set 2 pin 5-9 set 3 pin 10-14. I need say pin 1, pin7, and pin11 to go low at the same time. the next set needs to be random." This program is for a game i am trying to build. any help on code would be appreciated.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!

Tracy Allen
09-16-2008, 11:01 PM
something like?...

chance VAR WORD
DIRS = \$7FFF ' pins p0 to p14 are outputs
DO
RANDOM chance
OUTS = (DCD (chance // 5)) << 5 + (DCD (chance >> 3 // 5)) << 5 + (DCD (chance >> 6 // 5)
PAUSE 1000
LOOP

The DCD operator sets one bit in a group of five. For example, DCD 3 would make the pattern %01000. That is done three times, using different bits taken from the random number chance. The // 5 operator gives a number from 0 to 4 to work with the DCD operator.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

Tracy Allen
09-17-2008, 05:21 AM
You have another thread going about multipul (sic) pins at once. The following code is setting all the pins at once by assigning a value to the built-in variable OUTS. Nothing happens at OUTS until it evaluates the whole expression on the right-hand side of the equation. The outputs all change at the same instant. The expression on the right may look complicated, but it is not really if you break it down as I described in the post above. Ask if you have questions about how it works.

I re-read your question and I see that you want one pin in each group of 5 to go low, not one of 5 high. That takes one small change to the program, the addition of a ~ operator, bitwise NOT, which makes everything that was high->low and vice versa.

chance VAR WORD
DIRS = \$7FFF ' pins p0 to p14 are outputs
DO
RANDOM chance
OUTS = ~((DCD (chance // 5)) << 5 + (DCD (chance >> 3 // 5)) << 5 + (DCD (chance >> 6 // 5)) '<-- added ~() to invert bits
PAUSE 1000
LOOP

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

MR SPARKY
09-17-2008, 09:33 AM
Tracy Allen this what i was looking for. the only thing i need to change is that some lights stay on to long. is there a way to make each light stay on for no longer than a preset time. this is imperitive to the control working correctly and make it fair to each player. thankyou for all the help. This is one of the best forums i have been on.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!

MR SPARKY
09-17-2008, 09:39 AM
let me clerify what i mean. iwant a givven led to only light for one cycle. after the next cycle is complete it can light again.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!

Mike Green
09-17-2008, 10:18 AM
You need to define exactly how you want the game to behave. What do you mean by cycle for example? You'll have to keep track of which lights have been on so they won't be turned on for a cycle. How would you do that?

MR SPARKY
09-17-2008, 11:36 AM
that was what i was trying to do. but what I did instead was used the outs comand to send all pins so the lights are off. It will work that way. Doing it that way they can still play that button again and it will still be fair to all other players. I have learned alot in the past 2 nights because of every ones help. Thank you all. When I build the game i will post the code and pictures of the game. The game is still in the planning stage and not sure of the final completion date. plans are 2 years. i will post a thanks to every one that helped me with the coding.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!

Tracy Allen
09-17-2008, 10:20 PM
You ask, "is there a way to make each light stay on for no longer than a preset time". Probably so. Where will the "preset time" come from? It could come from a table of times, or it could depend on something that the players do, or it could be a random number between, say, 0.1 and 1 second, or ???.

Or maybe it has to do with the "cycle" of 5. Do you mean that each light in the group of 5 will come on exactly once in each cycle? That is like drawing cards without replacement so that at the end there is exactly one card left, and then you reshuffle the whole deck and start again. The program above works with replacement, so that often the same light will be on two second in a row.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

MR SPARKY
09-18-2008, 09:17 AM
there is one other thing i would like to do. i want the sequnce to only run when the game starts. i will be using a relay and pin 15 would have to be an input. itryed various combinations of code but none work. i have attached the present code that i have now. if someone can help me out with where i went wrong in the code. what happens is when downloaded to the stamp, the power on·led turns off.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!

Tracy Allen
09-18-2008, 01:40 PM
We really need a better description of what you want this game to do to light up everyone's life!. The way you have it, the loop with the randomly flashing lights will run if pin p15 is high when the program starts, otherwise the program ENDs. Where does the relay come from?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

MR SPARKY
09-18-2008, 01:49 PM
the relay switches the bs2 input. the signal coming from the main control is 24vdc. i would say thats not good for the chip. all this control is doing is providing power to the buttons of the game. as the out put is on and the corresponding button is pressed the what ever advances. the way the program is set up, the buttons would always be able to be pressed. i want the sequnce to only run when the game starts. when the game is over the controler needs to stop sequncing.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!

Tracy Allen
09-18-2008, 01:55 PM
Sorry Sparky, I still don't understand what is supposed to happen, and when, in the flow of events, or play, or control, or whatever.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

MR SPARKY
09-18-2008, 02:06 PM
its a race game like the water gun or wack a mole games at carnivals. but im using plcs for the main controls because i need a few hundred i/o. the plcs run on 24vdc. when the atsrt button to start the race is pressed i want the sequence to start. when the race is over the sequence will stop. the relay is just the switch to initiate the sequence. the sequence provides power for the player feild. if the sequence were to continue after the race there could be damage. once there is a winner the sequnce needs to stop. thats where the relay comes into play. when the game starts the contacts close providing pin 15 with vdd. when the game ends the relay contacts open and tell the sequence to end.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!

Tracy Allen
09-19-2008, 04:30 AM
So, the relay provides a signal both to start and stop the sequence? In that case the test for the relay closed needs to be inside the loop. First, do a test program to be sure that you are getting a good signal from the relay. The relay will need a pullup or pulldown resistor.

OUTPUT 0 <--- connect an LED and resistor to this pin p0
DO
DEBUG BIN1 in15
out0 = in15
LOOP

On the debug screen you should see 1's when the pin p0 is high and 0's when it is low, depending on the state of the relay. If not, there is something wrong with the wiring. Also, the state of out0 will follow in15.

Once you are confident about the signal, then the test of the pin to end the cycle should go into the loop:

' {\$STAMP BS2}
' {\$PBASIC 2.5}
chance VAR Word
DIRS = \$7FFF ' pins p0 to p14 are outputs, p15 is input
main:
OUTS = \$7FFF ' make all outputs high p0 to p14
DO:LOOP UNTIL in15 ' stay here until p15 goes high
DO
RANDOM chance
OUTS = ~((DCD (chance // 5)) << 5 + (DCD (chance >> 3 // 5)) << 5 + (DCD (chance >> 6 // 5)) )
PAUSE 200
OUTS=%111111111111111
PAUSE 1000
LOOP WHILE in15 ' stay in the loop until in15 goes low
GOTO main ' start over in wait loop
END

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

MR SPARKY
09-20-2008, 07:45 AM
for the random chance code is there any way to change the variables to have 4 sets of 4 instead of 3 sets of five. i am trying to understand the code but cant figgure out what each # is telling what to do.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!

Tracy Allen
09-20-2008, 11:47 PM
Sparky,

Here is a way to change it to 4 sets of 4, which of course will use all 16 pins, p0 to p15. If you need more outputs, a shift register like the 74LC595 could expand the possibilities.

OUTS = ~( (DCD (chance // 4)) << 4 + (DCD (chance >> 2 // 4)) << 4 + (DCD (chance >> 4 // 4)) << 4 + (DCD (chance >> 6 // 4)) )

Each segment in the above picks two bits from the random number and converts the value, 0,1,2,3 into the corresponding pattern %0001, %0010, %0100 or %1000, and then shifts the pattern into a multiple of 4 bits to the left.

There are other ways to do the random math. For example, it could be a permutation that hits all the possibilities and never the same one twice in each cycle.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com (http://www.emesystems.com)

werntonb
02-20-2011, 05:03 AM
I can't still don't understand the codes. How are they?