#picaxe 08M2
' Ladder Game
' Ver 4, nearly there !
' 12 LEDs Charlieplexed from pins C.0, C.1, C.2, C.4 ( Legs 7,6,5,3 )
' In Charlieplexing each pin has three states: High, Low, Tristate
' On the 08M pin C.0 is 'output only', so a pseudo-PWM trick is used to make it 'Tristate'.
' See http://www.user.dccnet.com/wrigter/picaxe/12LEDpyramid2.gif
symbol Eaddr = b0 ' Pointer into EEPROM
symbol LEDanode = b1 ' Pin number for Anode of lit LED
symbol LEDcathode = b2 ' Pin number for Cathode of lit LED
symbol BPressed = b3 ' Button pressed flag
symbol Height = b4 ' Number of current step
symbol LEDduration = b5 ' Duration of LED lit time
symbol LEDtimer = b6 ' Timer for LEDduration
symbol PBenable = b7 ' Enables button if > 0
symbol temp = b8
symbol Pbutton = pinC.3 ' Push button ( Leg 4 ) pulled low, switched to high
' Store pin numbers for Anodes and Cathodes of each LED
' 0 1 2 3 4 5 6 7 8 9 10 11
EEPROM 0,(4,0, 4,1, 4,2, 0,4, 0,1, 0,2, 1,4, 1,0, 1,2, 2,4, 2,0, 2,1 )
' Store flash durations for each rung.
EEPROM 24, (250,250,250,250,250,250,250,250,250,250,250,250)
' Code starts here
' ----------------
' Main loop
do
' Wait for button not pressed
do
PBenable = 0 ' Disable PB detection during LightLED routine
gosub LightLED
BPressed = Pbutton
loop until BPressed = 0
' Get flash duration for this rung
Eaddr = Height + 24 ' Make pointer to duration
read Eaddr, LEDduration ' Get duration
PBenable = 1 ' Re-enable PB
' Flash LED until button pressed
do
gosub LightLED
if BPressed > 0 then exit ' Exit flash loop if button was pressed
gosub BlankLED
if BPressed > 0 then exit ' Exit flash loop if button was pressed
loop ' End of flash loop
PBenable = 0
' Was button pressed while LED off ?
if Bpressed = 2 then
gosub DisplayFailed
Height = 0 ' Go to lowest rung
endif
' Was button pressed while LED on ?
if Bpressed = 1 then
inc Height ' Go to next rung
endif
' Made it to the top !
if Height > 11 then
gosub MadeItToTheTop
Height = 0 ' Go to lowest rung
endif
loop ' Loop forever
' ---------------------------------------------------------------------
' Subroutines
' ---------------------------------------------------------------------
LightLED: ' Lights LED for time set by LEDduration, scans for Pbutton if enabled
Eaddr = Height * 2 ' Make pointer to pin numbers
read Eaddr , LEDanode, LEDcathode ' Get pins for current LED
dirs = %00000000 ' Switch off any previous LEDs
high LEDanode : low LEDcathode ' Switch on current LED
LEDtimer = LEDduration ' Set timer
do
if LEDanode > 0 and LEDcathode > 0 then ' LED does not need C.0, so 'tristate' it
toggle 0 ' Make square wave on C.0
endif
' Check if button pressed
if Pbutton = 1 and PBenable > 0 then
BPressed = 1 ' 1 means button was pressed when LED on
return ' Return to main loop
endif
dec LEDtimer
loop until LEDtimer = 0
return
BlankLED: ' Blanks LEDs for time set by LEDduration, scans for Pbutton if enabled
LEDtimer = LEDduration ' Set timer
dirs = %00000000 ' Switch off any previous LEDs
do
toggle 0 ' LED does not need C.0, so 'tristate' it
' Check if button pressed
if Pbutton = 1 and PBenable > 0 then
BPressed = 2 ' 2 means button was pressed when LED off
return ' Return to main loop
endif
dec LEDtimer
pause 3
loop until LEDtimer = 0
return
MadeItToTheTop:
' Put victory display routine here
LEDduration = 10
for temp = 0 to 5
for height = 0 to 11
gosub lightLED
next
next
return
DisplayFailed:
' Put failed display routine here
LEDduration = 10
for temp = 0 to 3
for height = 11 to 0 step -1
gosub lightLED
next
next
return