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_aifsequence_aendif button_bifsequence_bendif button_cifsequence_cendif 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. subbutton_a 0get_position500less_than return
subbutton_b 2get_position500less_than return
subbutton_c 4get_position500less_than return
# These subroutines each perform an arbitrary sequence # of servo movements. You should change these to fit # your application. subsequence_a 4000 0servo10delay return subsequence_b 4000 2servo10delay
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.
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