Controlling servos with multiple buttons

I’ve copied and pasted the existing script and have tried to play around with it to get what I’d like.

Is it possible for the servo to return to the start position once I release the button?

Probably something simple but just beginning to get my head around writing the scripts.

Thanks,

Jon

# When the script is not doing anything else,
# this loop will listen for button presses. When a button
# is pressed it runs the corresponding sequence.
begin
button_a if sequence_a endif
button_b if sequence_b endif
button_c if sequence_c endif
repeat

# These subroutines each return 1 if the corresponding
# button is pressed, and return 0 otherwise.
# Currently button_a is assigned to channel 0,
# button_b is assigned to channel 1, and
# button_c is assigned to channel 2.
# These channels must be configured as Inputs in the
# Channel Settings tab.
sub button_a
0 get_position 500 less_than
return

sub button_b
2 get_position 500 less_than
return

sub button_c
4 get_position 500 less_than
return

# These subroutines each perform an arbitrary sequence
# of servo movements. You should change these to fit
# your application.
sub sequence_a
4000 0 servo 10 delay
return

sub sequence_b
4000 2 servo 10 delay

return

sub sequence_c
4000 5 servo 10 delay

return

If you just want the servo to be in one position while the button is pushed and another while it is not, you could add an ELSE condition to each of the button checks, and have the position of the servo be based on the state of the button.

For example, you could modify your button_a check to specify the servo position (and remove the position from sequence_a), which would look something like this:

button_a if
  4000		#if button_a is pushed, put the "pressed" target position on the stack
else
  8000
endif 		#if not, put the starting target position on the stack
sequence_a		#call sequence_a subroutine to update servo position

And the new modified sequence_a to work with that would be:

sub sequence_a
  1 servo 10 delay	#modified sequence_a subroutine to not specify position
return

By the way, I noticed from your script that it looks like you have your buttons on channels 0, 2, and 4, but your sequence_a and sequence_b subroutines are also using channels 0 and 2 for servo commands, so I suspect this is a typo and you meant to have your servos on channels 1, 3 and 5.

Brandon

Hi Brandon,

Im not sure if this is right, im new to script writing :thinking:The script does state error on line 13.2 but im really not sure what im doing im afraid.

begin
  button_a if 4000
else 8000
endif
sequence_a

begin
  button_b if 4000
else 8000
endif
sequence_b

 begin
  button_c if 4000
else 8000
endif
sequence_c


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

  
sub sequence_a

 1 servo 10 delay
 
  return
   
sub sequence_b
 3 servo 10 delay
    return
 
sub sequence_c
  
 5 servo 10 delay
  return

I hope this makes sense.

Thanks, Jon

It looks like that code would result in an error that complains about the BEGIN block never being closed. To fix this, you can remove the BEGIN commands before button_b and button_c, and put a repeat command after sequence_c. This puts all of your button checks within the same main BEGIN/REPEAT loop. For example:

begin			#start of main loop

  button_a if 4000	#check button_a
    else 8000
  endif
  sequence_a


  button_b if 4000	#check button_b
    else 8000
  endif
  sequence_b


  button_c if 4000	#check button_c
    else 8000
  endif
  sequence_c

repeat			#loop back to start of main loop

# subroutines go below this, outside of the main loop

Brandon

1 Like