#picaxe 28x2
#no_table
#no_data
setfreq em32
; drop LED Strip light control
; .--------------------------> Prox Switch
; | 28X2 5V -.- Light strip
; | .-----__-----. |
; | | RST B.7 | `--> RED +V
; '--= A0 = .--> SCK --------> BLU CK
; | C.2 C.5 |--|--> SDO --------> GRN DI
; .--| C.3 C.4 | | .--> YEL 0V
; | | | | 0V _|_
; | `------------' |
; `------------------'
; Set how many LED modules in the strip
Symbol HOW_MANY_LEDS = 60
`
Symbol HOW_MANY_LEDS_MINUS_1 = HOW_MANY_LEDS - 1
Symbol HOW_MANY_LEDS_TIMES_3 = HOW_MANY_LEDS * 3
; Set the maximum brightness of the LED while testing
; Use small values to keep current consumption low
Symbol MAX_BRIGHTNESS = 31 ; 0 to 255 (full)
; Initialise the HSPI interface
#macro init()
hspisetup spimode00, spifast
#endmacro
; Send a four byte packet out via HSPI
#macro sendPacket( n1, n2, n3, n4 )
hspiout( n1, n2, n3, n4 )
#endmacro
; Send the start of data header
#macro head()
sendPacket( $00, $00, $00, $00 )
#endmacro
; Send a LED controlling command
#macro send( red, green, blue)
sendPacket( $FF, blue, green, red )
#endmacro
; Send the end of data tail
#macro tail()
sendPacket( $FF, $FF, $FF, $FF )
#endmacro
PowerOnReset:
; Initialise the HSPI interface
init
; Turn all LED modules off
head
for w0 = 1 To HOW_MANY_LEDS
send( $00, $00, $00 ) ; 1 to last = Off
next
tail
BounceLed:
; Set one LED at a time
; 'w0' indicates which LED to set
do
do
for w0 = HOW_MANY_LEDS_MINUS_1 to 2 step -1
Gosub SetLeds
'Gosub SetLeds2
if pina.0 = 0 then
b15 = 1
endif
next
loop until b15 = 1
' for w0 = HOW_MANY_LEDS_MINUS_1 to 2 step -1
' Gosub SetLeds
' next
b15 = 0
gosub TurnAllLedsOn
pause 3000
for b30 = 1 to 2
for w0 = 1 to HOW_MANY_LEDS
Gosub SetLeds2
next
next b30
gosub VaryLeds
loop
SetLeds:
; Set a specific LED and keep the others off
head
for w1 = 1 to HOW_MANY_LEDS
; Light only the lED we want lit
if w1 = w0 then
' for b20 = 0 to 4
b10 = 0
b11 = 50
b12 = 0
send( b10, b11, b12 ) ; varying colour
b10 = 50
b11 = 0
b12 = 0
send( b10, b11, b12 ) ; varying colour
b10 = 0
b11 = 0
b12 = 50
send( b10, b11, b12 ) ; varying colour
'next b20
else
send( $1, $00, $1 ) ; off
end if
pause 4
next
tail
; Update the colour
b10 = b10 + 1 // MAX_BRIGHTNESS; Red
b11 = b11 + 3 // MAX_BRIGHTNESS; Green
b12 = b12 + 5 // MAX_BRIGHTNESS; Blue
return
TurnAllLedsOn:
; Turn all LED modules on
' do
head
for w0 = 1 to HOW_MANY_LEDS
send( MAX_BRIGHTNESS, $00, $00 ) ; red
send( $00, MAX_BRIGHTNESS, $00 ) ; green
send( $00, $00, MAX_BRIGHTNESS ) ; blue
send( MAX_BRIGHTNESS, MAX_BRIGHTNESS, $00 ) ; red + green
send( MAX_BRIGHTNESS, $00, MAX_BRIGHTNESS ) ; red + blue
send( $00, MAX_BRIGHTNESS, MAX_BRIGHTNESS ) ; green + blue
send( MAX_BRIGHTNESS, MAX_BRIGHTNESS, MAX_BRIGHTNESS ) ; white
next
tail
pause 3500
' loop
return
SetLeds2:
; Set a specific LED and keep the others off
head
for w1 = 1 to HOW_MANY_LEDS
; Light only the lED we want lit
if w1 = w0 then
' for b20 = 0 to 4
b10 = 50
b11 = 50
b12 = 0
send( b10, b11, b12 ) ; varying colour
b10 = 50
b11 = 0
b12 = 30
send( b10, b11, b12 ) ; varying colour
b10 = 0
b11 = 50
b12 = 50
send( b10, b11, b12 ) ; varying colour
'next b20
else
send( $00, $01, $00 ) ; off
end if
next
tail
; Update the colour
b10 = b10 + 1 // MAX_BRIGHTNESS; Red
b11 = b11 + 3 // MAX_BRIGHTNESS; Green
b12 = b12 + 5 // MAX_BRIGHTNESS; Blue
return
VaryLeds:
for b30 = 1 to 250
; Randomly update some LED values
for w0 = 1 to 5
random w1
ptr = w1 / 5 // HOW_MANY_LEDS_TIMES_3 'Set array size and set data - Number of LEDs * 3
@ptr = @ptr + 10 & 10 'Set Brightness of LED
next
; Output the LED data
ptr = 0 'Set pointer to first location of data
head 'Send start data to LEDs - ($00, $00, $00, $00)
for w0 = 1 to HOW_MANY_LEDS
send( @ptrinc, @ptrinc, @ptrinc ) 'Send 3 bytes of data to each leds (Blue, Green, Red)
next
tail 'Send End data to LEDs - ($FF, $FF, $FF, $FF)
next b30
return