Hi,
I would like to convert this code to arduino but Im really not great so yeah.
#picaxe 14m
' Assumptions
' Times out after 10 seconds of all switches in the off position
' All switches must be off at start
' All 4 switches must be switched on before that sequence is learnt
' bulbs/LEDs on outputs 0 to 3
' switches on inputs 0 to 3
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 position = b5 ' position counter
symbol timeout_counter = w6 ' timeout counter
symbol timeout = 1000 ' timeout set to 10s = 10 000ms = 1000 x 10ms
'Change to smaller number when simulating or you will get bored!
' Start of program
do_reset:
position = 0 ' reset position counter
flags = 0 ' reset flags
' Learning loop
waiting_to_learn_loop:
' if not learnt switch learn it
if pin0 = 1 and flag0 = 0 then learn0
if pin1 = 1 and flag1 = 0 then learn1
if pin2 = 1 and flag2 = 0 then learn2
if pin3 = 1 and flag3 = 0 then learn3
' we have learnt that switch so light output accordingly
if flag0 = 1 then
if pin0 = 1 then
high light0
else
low light0
end if
end if
if flag1 = 1 then
if pin1 = 1 then
high light1
else
low light1
end if
end if
if flag2 = 1 then
if pin2 = 1 then
high light2
else
low light2
end if
end if
if flag3 = 1 then
if pin3 = 1 then
high light3
else
low light3
end if
end if
goto waiting_to_learn_loop
' Learn a light position and set flag so we know that switch is done
learn0:
' position gives you the output for this switch
flag0 = 1 ' set flag
light0 = position ' remember position for this switch
goto learn_end
learn1:
' position gives you the output for this switch
flag1 = 1 ' set flag
light1 = position ' remember position for this switch
goto learn_end
learn2:
' position gives you the output for this switch
flag2 = 1 ' set flag
light2 = position ' remember position for this switch
goto learn_end
learn3:
' position gives you the output for this switch
flag3 = 1 ' set flag
light3 = position ' remember position for this switch
'goto learn_end
learn_end:
inc position ' add 1 to position counter
if position > 3 then have_learnt_all ' all done
goto waiting_to_learn_loop ' not all done so back to loop
' 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 pin0 = 1 then
high light0
timeout_counter = 0
else
low light0
end if
if pin1 = 1 then
high light1
timeout_counter = 0
else
low light1
end if
if pin2 = 1 then
high light2
timeout_counter = 0
else
low light2
end if
if pin3 = 1 then
high light3
timeout_counter = 0
else
low light3
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