random out
MR SPARKY
Posts: 23
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!!!!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!
Comments
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
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!
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:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I light up·everyones life!!!!!
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.
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