in picbasic standard how would you generate a random number 1 - 4 then make leds 1 -4 light up in that order i have been unable to get it working just right i though some on out the could give me some new thoughts on how to do that.
Printable View
in picbasic standard how would you generate a random number 1 - 4 then make leds 1 -4 light up in that order i have been unable to get it working just right i though some on out the could give me some new thoughts on how to do that.
look those thread... more than you need... good luck
http://www.picbasic.co.uk/forum/show...ghlight=random
http://www.picbasic.co.uk/forum/show...ghlight=random
http://www.picbasic.co.uk/forum/show...ghlight=random
Everytime you use Random, you in effect seed a new value in the word variable RandomValue. You only want the bottom two bits (to give you your four possible values) so extract them... Use those bits to illuminate your LED's... simple...
This will give you one-of-four Random selection forever. I haven't got PBC but I'm sure you could convert it simply...Code:LEDA var PortB.0
LEDB var PortB.1
LEDC var PortB.2
LEDD var PortB.3
RandomValue var WORD
LEDValue var BYTE
Start:
Low LEDA
Low LEDB
Low LEDC
Low LEDD
Random RandomValue
LEDValue=RandomValue & $03
If LEDValue=0 then High LEDA
If LEDValue=1 then High LEDB
If LEDValue=2 then High LEDC
If LEDValue=3 then High LEDD
Pause 1000
Goto Start
OK... looking at the PBC manual... here's the conversion... sombody can correct me if I've made a mistake...
Well, actually, you could probably reduce it further...Code:' LEDA is on PortB.0
' LEDB is on PortB.1
' LEDC is on PortB.2
' LEDD is on PortB.3
' RandomValue is variable W0
' LEDValue is variable B2
Start:
Low 0
Low 1
Low 2
Low 3
Random W0
B2=W0 & $03
If B2=0 then High 0
If B2=1 then High 1
If B2=2 then High 2
If B2=3 then High 3
Pause 1000
Goto Start
Code:Start:
Low 0
Low 1
Low 2
Low 3
Random W0
B2=W0 & $03
High B2
Pause 1000
Goto Start