Multiple switches to control servos - how to combine two types of movement

Hi,

I have not found solution for my problem. What I want to do:

Channel 0 - input (switch “a”)
Channel 1 - servo
Channel 2 - input (switch “b”)
Channel 3 - servo

Switch “a” move servo from position A to B and return to B
Switch “b” move serwo from position A to B and stay in this position (second press of switch “b” will move servo back to A position)

My problem is that after press “b” button, switch “a” is no longer working. Switch “a” works until switch “b” is press. I noticed that this red triangle which is scanning code after press switch “b” is no longer scanning subroutine for button “a”.

Here is my code for now:

begin
  button_a if sequence_a endif
  button_b if sequence_b endif
repeat

sub button_a
  0 get_position 500 less_than
  return
 
sub button_b
  2 get_position 500 less_than
  return

sub wait_for_button_b_press
  wait_for_button_b_open_10ms
  wait_for_button_b_closed_10ms
  return
sub wait_for_button_b_open_10ms
  get_ms
  begin
    button_b
    if
      drop get_ms
    else
      get_ms over minus 10 greater_than
      if drop return endif
    endif
  repeat
sub wait_for_button_b_closed_10ms
  get_ms
  begin
    button_b
    if
      get_ms over minus 10 greater_than
      if drop return endif
    else
      drop get_ms
    endif
  repeat

sub sequence_a
  1000 delay
  4000 1 servo 900 delay
  8000 1 servo 900 delay
return

sub sequence_b

begin
  4000 frame
  5000 frame
repeat
 
sub frame
wait_for_button_b_press
  3 servo
  return  

Maybe there is simpler method to do this what I want ? Most of channels I want to set to move A > B > A but one of it I want to be A > B stay > press > A stay etc.

Thanks for help

Hello.

Thank you for posting your code. I noticed that you used a BEGIN/REPEAT block in your sequence_b subroutine. This means that the script never returned to the main loop in the code. Also, your frame subroutine was waiting for another button push.

You should be able to do what you described by using the GET_POSITION command to check the current target position being sent to the servo, and send it to the other position instead. For example, sequence_b could check what position servo channel 3 is at, and if it equals 5000, set the new target position to 4000. If it does not equal 5000, set the new position to 5000. If you did that, your new sequence_b subroutine would look something like this:

sub sequence_b
  3 get_position  #see what position the servo is in
  5000 equals if  #if it is at position 5000
    4000 3 servo  #set it to position 4000
  else
    5000 3 servo  #otherwise, set it to 5000
  endif
  500 delay       #small delay
return

If you try making that modification and it does not work for you, can you post your updated code as well as a description of what it does and what you want to happen instead?

Brandon

Thank you so much Brandon. Now it works as it should, everything is fine. Thank you again :slight_smile:

1 Like