danners430
Member
Hey folks,
I'm trying to write a program to randomnly flash 16 LEDs one at a time, each time using one of 4 random colours with a random delay between each flash. The idea is to simulate shell flashes behind a model of a WWI trench.
The 4 colours are achieved by 12 potentiometers in groups of 3 (RGB), connected to A.0-A.3 (PNP transistors). The low sides are all connected to A.4, B.0-7, C.0-6. There's also a potentiometer connected to C.7 which adds an element of manual control to the flashing frequency.
Notes: chip is a PICAXE-28X2 running at stock, internal frequency.
simu is just a startup test for the LEDs - useful for fault-finding
play0 is the PWM simulation to allow dimming of the LEDs
The problem I'm having is that line 35:
Is there a problem with my implementation of
I'm trying to write a program to randomnly flash 16 LEDs one at a time, each time using one of 4 random colours with a random delay between each flash. The idea is to simulate shell flashes behind a model of a WWI trench.
The 4 colours are achieved by 12 potentiometers in groups of 3 (RGB), connected to A.0-A.3 (PNP transistors). The low sides are all connected to A.4, B.0-7, C.0-6. There's also a potentiometer connected to C.7 which adds an element of manual control to the flashing frequency.
Code:
init: {
symbol retrievedDelayValue = b6
symbol retrievedLedValue = b7
symbol flashRate = b5
symbol pauseTime = w4
symbol i = b54
symbol highSide = b4
let dirsC = %11101111
let dirsB = 255
let dirsA = 255
let pinsa = 15
let pinsb = 0
let pinsc = 0
w0 = 1
for i = 0 to 15
pinsB = b0
pinsC = b1
gosub simu
w0 = w0 << 1
next i
pinsB = 0
pinsC = 0
high a.4
gosub simu
low a.4
}
main: {
pinsA = 15
random w3
call readspeed
b7 = b7 // 15
pause w5
call maths
call play0
goto main
}
end
readSpeed: {
readadc C.7, flashRate
'flashrate = flashrate rev 8 'Invert speed controller direction (optional)
flashRate = flashRate / 2
if flashRate = 0 then
flashRate = 1
end if
w5 = b6 + 100
w5 = w5 * flashRate
return }
play0: {
call setLEDs
pause 6
call clearLEDs
pause 3
call setLEDs
pause 50
call clearLEDs
pause 5
call setLEDs
pause 20
call clearLEDs
pause 5
call setLEDs
pause 15
call clearLEDs
pause 5
call setLEDs
pause 10
call clearLEDs
pause 5
call setLEDs
pause 10
call clearLEDs
pause 8
call setLEDs
pause 10
call clearLEDs
pause 7
call setLEDs
pause 7
call clearLEDs
pause 9
call setLEDs
pause 5
call clearLEDs
pause 10
call setLEDs
pause 4
call clearLEDs
pause 11
call setLEDs
pause 3
call clearLEDs
pause 15
call setLEDs
pause 1
call clearLEDs
pause 20
call setLEDs
pause 1
call clearLEDs
return }
setLEDs: {
pinsB = b0
pinsC = b1
pinsA = b2
return }
clearLEDs: {
pinsB = 0
pinsC = 0
low a.4
return }
simu: {
low a.0
pause 100
toggle a.0
toggle a.1
pause 100
toggle a.1
toggle a.2
pause 100
toggle a.2
toggle a.3
pause 100
high a.3
return }
maths: {
w0 = 0
w1 = %0000000000001111
w0 = dcd b7
bit20 = bit15
bit15 = bit14
bit14 = bit13
bit13 = 0
b4 = b7 // 4
select b4
case 0
clearbit b2, 0
case 1
clearbit b2, 1
case 2
clearbit b2, 2
case 3
clearbit b2, 3
endselect
return }
simu is just a startup test for the LEDs - useful for fault-finding
play0 is the PWM simulation to allow dimming of the LEDs
The problem I'm having is that line 35:
random w3
is being a little funny: it seems to increment instead of being in any way random. When I've tested it, it's started at 2, and incrementing upwards at random intervals. Even seeding w3 with 0 or the timer value didn't alter this behaviour much.Is there a problem with my implementation of
random
incorrect, or is there a fundamental problem here?