Using OR to detect two switches to control same subroutines

Hi

I wonder if anyone can point me in the right direction on how to program the following for the 6 ch micro Maestro

I have two switches. One is a momentary push switch and the other is controlled by a transmitter using the RC digital sw. These are to control retracts and doors on a model plane. I can get the sequence to work fine if I just have one sw connected - either the momentary sw or the transmitter but I can’t figure out how to program to control the sub routines if either sw is activated. ie if the transmitter is off I can sequence the servos by pressing the sw or if the transmitter is on I can sequence by using the rc digital sw or the momentary sw. So far my attempts have resulted in either only one sw will control or I get stack overflows. I have used the sample sw program as a basis and modified that to my needs. If anyone can help it would be much appretiated :slight_smile:

My script is below

1 4 acceleration
80 4 speed

2000 delay
geardown
goto main_loop

sub switch
		0 get_position 500 less_than
return
 
# This subroutine uses the BUTTON subroutine above to wait for a button press,
# including a small delay to eliminate noise or bounces on the input.
sub wait_for_tx_toggle
		wait_for_sw_rear_10ms
		wait_for_sw_forward_10ms
return
 
# Wait for the button to be NOT pressed for at least 10 ms.
sub wait_for_sw_forward_10ms
		get_ms # put the current time on the stack
		begin
			# reset the time on the stack if it is pressed
			switch
			if
				drop get_ms
			else
				get_ms over minus 10 greater_than
				gearup
				if drop return endif
			endif
		repeat
return
 
# Wait for the button to be pressed for at least 10 ms.
sub wait_for_sw_rear_10ms
		get_ms
		begin
			# reset the time on the stack if it is not pressed
			switch
			if
				get_ms over minus 10 greater_than
				geardown
				if drop return endif
			else
				drop get_ms
			endif
		repeat

sub geardown
		4000 4 servo #door open
		moving_wait
  	3600 5 servo #retract down
		led_on
return

sub gearup
		8000 5 servo #retract up
		2500 delay
		8000 4 servo #door shut
		led_off
return

sub moving_wait
		begin
			get_moving_state
		while
		repeat
return

main_loop:
begin
		wait_for_tx_toggle
repeat

Thanks

Mark

This can be easily implemented using logical_or:

Sub check_2_switches
  0 get_position 512 less_than #check switch on ch0
  1 get_position 512 less_than #check switch on ch1
  logical_or if
    take_action #sub to execute if one of the switches is toggled
  endif
return

Thanks. I’ll try to get my head around how to integrated that into my code above :slight_smile:

The issue I had was stack overflows when having two get_positions one after the other.

I may be back with more newbie questions :slight_smile:


Mark

The stack shouldn’t overflow if you use the values from get_position right away with logical_or, as it takes 2 off the stack and returns 1(which you then use with an if statement). Just be careful if you split your readings in different subroutines…