#picaxe 14m2
' Magic Switchboard - original source by "Technical" from PicAxe Forum
' Modified for PicAxe 18x by Wayne Thomason of Addison, TX USA
' 7/15/2009
' Modified for Picaxe 14m2 By Bill Brown
' 12/8/2012
'
' mods: 1. Now is easily configurable via switch? and bulb? variables
' 2. "timeout" functions even without learning all 4 switches
' 3. starting point and sequence direction dependent on last switch turned off
' 4. Now has Audience_lockdown feature. If power is turned on while switch-4
' is set, each light will respond only to corresponding switch position
' until circuit is reset.
'
' Assumptions
' 1. Times out after 10 seconds of all switches in the off position
' regardless of whether all switches are learned yet
' 2. All switches must be off at start
' (If switch 4 on when started, it starts up in audience-mode.)
' 3. All 4 switches must be switched on before that sequence is learned
' 4. Set bulb/LED outputs using bulb1, bulb2, bulb3 & bulb4
' 5. Set switch inputs using switch1, switch2, switch3 and switch4
' 6. first pattern is left to right, bulbs 1, 2, 3, 4
' 7. subsequent patterns are determined by last SWITCH turned off:
' Switch 1 = 1234 order (bulb 1 first, then right in sequence)
' Switch 2 = 2143 order (bulb 2 first, then left in sequence, wrapping after first)
' Switch 3 = 3412 order (bulb 3 first, then right in sequence, wrapping after last)
' Switch 4 = 4321 order (bulb 4 first, then left in sequence)
symbol switch1 = pinc.3 ' input pin for switch 1 (pinc.0, pinc.1, pinc.2 and pinc.3) For 18m2 - pinc.1
symbol switch2 = pinc.2 ' input pin for switch 2 pinc.0
symbol switch3 = pinc.1 ' input pin for switch 3 pinc.6
symbol switch4 = pinc.0 ' input pin for switch 4 pinc.7
symbol bulb1 = b.3 ' output for bulb 1 (b.0, b.1, b.2 and b.3) For 18m2 B.4
symbol bulb2 = b.2 ' output for bulb 2 b.5
symbol bulb3 = b.1 ' output for bulb 3 b.6
symbol bulb4 = b.0 ' output for bulb 4 b.7
symbol timeout = 250 ' each loop takes a little more than twice as long as original code
' by "Technical (about 2ms per loop). 450 x 2ms x 10ms = about 10
' seconds. Your mileage may vary - tweak timeout if needed.
'Change "timeout" to smaller number when simulating or you will get bored!
symbol flags = b0 ' flags to remember whether a switch has been learnt yet
symbol flag0 = bit0
symbol flag1 = bit1
symbol flag2 = bit2
symbol flag3 = bit3
symbol light0 = b1 ' variable to remember which switch is which light
symbol light1 = b2
symbol light2 = b3
symbol light3 = b4
symbol bulb = b7
symbol starting_lite = b6 ' variable to set starting light position (and direction)
symbol position = b5 ' position counter
symbol timeout_counter = w6 ' timeout counter
if switch4=1 then audience_lockdown
starting_lite = 1
' Start of program
do_reset:
' ' reset position counter
if starting_lite = 1 then ' if starting with bulb 1, position reset to 0.
position = 0
end if
if starting_lite = 2 then ' if starting with bulb 2, position reset to 1.
position = 1
end if
if starting_lite = 3 then ' if starting with bulb 3, position reset to 2.
position = 2
end if
if starting_lite = 4 then ' if starting wtih bulb 4, position reset to 3.
position = 3
end if
flags = 0 ' reset flags
' Learning loop
waiting_to_learn_loop:
' if not learnt switch learn it
if switch1 = 1 and flag0 = 0 then learn0
if switch2 = 1 and flag1 = 0 then learn1
if switch3 = 1 and flag2 = 0 then learn2
if switch4 = 1 and flag3 = 0 then learn3
' we have learnt that switch so light output accordingly
if flag0 = 1 then
if switch1 = 1 then
high light0
else
low light0
end if
end if
if flag1 = 1 then
if switch2 = 1 then
high light1
else
low light1
end if
end if
if flag2 = 1 then
if switch3 = 1 then
high light2
else
low light2
end if
end if
if flag3 = 1 then
if switch4 = 1 then
high light3
else
low light3
end if
end if
if switch2=0 and switch1=0 and switch3=0 and switch4=0 then ' count down to timeout even while in learning loop
pause 10 'wait 10ms
inc timeout_counter 'inc timeout
if timeout_counter > timeout then
timeout_counter=0
goto do_reset
end if
else
timeout_counter=0
end if
goto waiting_to_learn_loop
' Learn a light position and set flag so we know that switch is done
learn0:
gosub bulbset
' position gives you the output for this switch
flag0 = 1 ' set flag
light0 = bulb ' remember position for this switch
goto learn_end
learn1:
gosub bulbset
' position gives you the output for this switch
flag1 = 1 ' set flag
light1 = bulb ' remember position for this switch
goto learn_end
learn2:
gosub bulbset
' position gives you the output for this switch
flag2 = 1 ' set flag
light2 = bulb ' remember position for this switch
goto learn_end
learn3:
gosub bulbset
' position gives you the output for this switch
flag3 = 1 ' set flag
light3 = bulb ' remember position for this switch
'goto learn_end
learn_end:
if starting_lite = 1 then 'if starting with 1st lamp, sequence = 1-2-3-4
inc position
if position > 3 then have_learnt_all
goto waiting_to_learn_loop
end if
if starting_lite = 2 then 'if starting with 2nd lamp, sequence = 2-1-4-3
if position > 0 then 'don't dec if position=0, will cause error
dec position
else
position = 3 'position was 0 before dec, set to "3"
end if
if position = 1 then have_learnt_all 'if position=1 then we have come full circle
goto waiting_to_learn_loop 'not finished, loop and learn more switches
end if
if starting_lite = 3 then 'if starting with 3rd lamp, sequence = 3-4-1-2
inc position
if position > 3 then 'if position greater greater than 4th lamp, reset to "0"
position = 0
end if
if position = 2 then have_learnt_all 'if position=2 then we have come full circle
goto waiting_to_learn_loop 'not finished, loop and learn more switches
end if
if starting_lite = 4 then 'if starting with 4th lamp, sequence = 4-3-2-1
if position > 0 then 'don't dec if position=0, will cause error
dec position
else
goto have_learnt_all 'position is "0", we have learned last position.
end if
goto waiting_to_learn_loop
end if
' now simply loop reacting to the switches
' timeout_counter value will increment every 10ms
' however if any light is on the timeout_counter is reset
' so this means the timeout will only
' occur after 10 secoonds of all switches off
have_learnt_all:
if switch1 = 1 then
high light0
timeout_counter = 0
else
low light0
end if
if switch2 = 1 then
high light1
timeout_counter = 0
else
low light1
end if
if switch3 = 1 then
high light2
timeout_counter = 0
else
low light2
end if
if switch4 = 1 then
high light3
timeout_counter = 0
else
low light3
end if
'Set which bulb will start next round. (only if switches all learned)
if b0 = 15 and switch1=1 and switch2=0 and switch3=0 and switch4=0 then ' if switch 1 on alone, set lamp 1 first
starting_lite=1
end if
if b0 = 15 and switch1=0 and switch2=1 and switch3=0 and switch4=0 then ' if switch 2 on alone, set lamp 2 first
starting_lite=2
end if
if b0 = 15 and switch1=0 and switch2=0 and switch3=1 and switch4=0 then ' if switch 3 on alone, set lamp 3 first
starting_lite=3
end if
if b0 = 15 and switch1=0 and switch2=0 and switch3=0 and switch4=1 then ' if switch 4 on alone, set lamp 4 first
starting_lite=4
end if
pause 10 ' wait 10ms
inc timeout_counter ' inc timeout
' if timed out then reset else loop
if timeout_counter > timeout then do_reset
goto have_learnt_all
bulbset:
if position=0 then
bulb = bulb1
end if
if position=1 then
bulb = bulb2
end if
if position=2 then
bulb = bulb3
end if
if position=3 then
bulb = bulb4
end if
return
audience_lockdown:
if switch1 = 1 then
high bulb1
else
low bulb1
end if
if switch2 = 1 then
high bulb2
else
low bulb2
end if
if switch3 = 1 then
high bulb3
else
low bulb3
end if
if switch4 = 1 then
high bulb4
else
low bulb4
end if
goto audience_lockdown