Basically the same question...
I've got a working circuit that runs the code pasted below. It runs a motor in a random direction, w/ random motor on duration for a random number of loops. It's a cat toy that moves a captured ball via an arm connected to the motor.
I'm using logicator, flowsheet attached. When I run the simulation in the programing tool I get good behavior out of the random. When I down load the program to the chip the random behavior isn't so random anymore... chip seems to favor the forward direction on the motors.
The code below is used to give a 50/50 shot at either going forward or reverse:
Code:
label_16: random w6 'store random number in w6 (b13:b12)
let varB = b13 'Random command
if varB > 128 then label_17 'Compare command
gosub prc_REVERSE 'Do Procedure
The simulation does this perfectly... the chip always goes forward 90% of the time.
It's not just happening in that loop... when Forward is called the chip will repeat the forward loop 3-5x and when Reverse is called maybe 1 or twice and both loops are set up exactly the same other than the motor command sets pin 1 high.
So how do I get the chip to behave more random?
Also, I'm using compare instead of repeat/loop. Using repeat with a repeat until causes an error at download (duplicate lable name used?) that isn't detected by syntax check.
Code:
symbol varA = b0
symbol varB = b1
symbol varC = b2
symbol varD = b3
symbol varE = b4
symbol varF = b5
symbol varG = b6
symbol varH = b7
let dirs = %00010111
main:
label_1:
label_2:
if pin3 = 1 then label_14 'Decision command
goto label_2
label_14: random w6 'store random number in w6 (b13:b12)
let varA = b13 'Random command
if varA < 245 then label_16 'Compare command
goto label_1
label_16: random w6 'store random number in w6 (b13:b12)
let varB = b13 'Random command
if varB > 128 then label_17 'Compare command
gosub prc_REVERSE 'Do Procedure
label_42:
low 0
low 1
let varC = 0 'Expression command
let varD = 0 'Expression command
goto label_14
prc_REVERSE:
label_37:
if varD < 215 then label_43 'Compare command
return 'Return
label_43: random w6 'store random number in w6 (b13:b12)
let varD = b13 'Random command
low 0
high 1
pause 500 'Wait command
goto label_37
label_17: gosub prc_FORWARD 'Do Procedure
goto label_42
prc_FORWARD:
label_27:
if varC < 215 then label_28 'Compare command
return 'Return
label_28: random w6 'store random number in w6 (b13:b12)
let varC = b13 'Random command
low 1
high 0
pause 500 'Wait command
goto label_27