I wish to operate a pair of standard servos (operating some points on a model railway layout) in accordance with the inputs from two simple on/off toggle switches. There are three required combinations of the two servo positions -- one pulling while the other is pushing, one pushing while the other is pulling, and both pulling.
The two switches are connected to pins 3 and 4 of a PICAXE 08M2 via standard resistor dividers, and the two servos' signal leads are connected directly to pins c.1 and c.2. I've been using circuits based on the same principle to operate single servos with a single switch for some time now with no problems, but I can't get this particular program to work properly for two switches and two servos.
In this circuit the switch on pin4 is supposed to override the switch on pin3 when it's closed, and when it's open the switch on pin3 is supposed to operate the servos, but what was happening is the servos moved from pull/push to push/pull in response to pin4 (i.e. they're switching from "mainline" to "crossline"), but the switch on pin3 has no effect, and I'm getting myself in such a twist that right now when I power up the device one servo cycles back and forth continuously between the min_pulse and max_pulse limits while the other remains stationary, and neither switch has any effect!
Here's the code as it stands at the moment, if anyone is willing to help me out:
The two switches are connected to pins 3 and 4 of a PICAXE 08M2 via standard resistor dividers, and the two servos' signal leads are connected directly to pins c.1 and c.2. I've been using circuits based on the same principle to operate single servos with a single switch for some time now with no problems, but I can't get this particular program to work properly for two switches and two servos.
In this circuit the switch on pin4 is supposed to override the switch on pin3 when it's closed, and when it's open the switch on pin3 is supposed to operate the servos, but what was happening is the servos moved from pull/push to push/pull in response to pin4 (i.e. they're switching from "mainline" to "crossline"), but the switch on pin3 has no effect, and I'm getting myself in such a twist that right now when I power up the device one servo cycles back and forth continuously between the min_pulse and max_pulse limits while the other remains stationary, and neither switch has any effect!
Here's the code as it stands at the moment, if anyone is willing to help me out:
Code:
symbol position_A = b4 ; servo A current position
symbol position_B = b5 ; servo B current position
symbol min_pulse = b6
symbol max_pulse = b7
symbol servo_slow = b8
symbol set_route = b9 ; flag to denote which route is set -- 1=main route 2=cross 3=slip
symbol whereis_A = b10 ; flag to denote servo resting position -- 1=pushed 2=pulled
symbol whereis_B = b11 ; flag to denote servo resting position -- 1=pushed 2=pulled
let position_A = 0
let position_B = 0
let whereis_A = 0
let whereis_B = 0
let min_pulse = 125 ; change this value to alter left throw limit
let max_pulse = 175 ; change this value to alter right throw limit
let servo_slow = 20 ; increase this value to slow down the servos
; Input switches are on pin3 and pin4
; Servos are on c.1 and c.2
read b0,set_route
main:
if pin4 = 1 and set_route<>2 then
goto crossline
elseif pin3 = 0 and pin4=0 and set_route<>1 then
goto mainline
elseif pin3 = 1 and pin4=0 and set_route<>3 then
goto slipline
endif
goto main
crossline:
if whereis_A<>2 then
gosub pull_A
endif
if whereis_B<>1 then
gosub push_B
endif
let set_route=2
write b0,set_route
goto main
mainline:
if whereis_A<>1 then
gosub push_A
endif
if whereis_B<>2 then
gosub pull_B
endif
let set_route=1
write b0,set_route
goto main
slipline:
if whereis_A<>2 then
gosub pull_A
endif
if whereis_B<>2 then
gosub pull_B
endif
let set_route=3
write b0,set_route
goto main
pull_A:
let position_A=min_pulse
do
servo c.1,position_A
inc position_A
pause servo_slow
loop while b4<max_pulse
let position_A=max_pulse
let whereis_A=2
pause 100
servo c.1,OFF
return
push_A:
let position_A=max_pulse
do
servo c.1,position_A
dec position_A
pause servo_slow
loop while position_A>min_pulse
let position_A=min_pulse
let whereis_A=1
pause 100
servo c.1,OFF
return
pull_B:
let position_B=min_pulse
do
servo c.2,position_B
inc position_B
pause servo_slow
loop while position_B<max_pulse
let position_B=max_pulse
let whereis_B=2
pause 100
servo c.2,OFF
return
push_B:
let position_B=max_pulse
do
servo c.2,position_B
dec position_B
pause servo_slow
loop while position_B>min_pulse
let position_B=min_pulse
let whereis_B=1
pause 100
servo c.2,OFF
return