Add an additional Button

I have a button connected to a Maestro Mini 6 on P0 and a servo connected to P2 and P3. When pressing the button initially the code below executes the, Extend_Left subroutine, which rotates servo 3 90 degrees then servo 2 90 degrees. When I press the button again the Retract_Left subroutine executes, and servo 2 moves 90 degrees back to initial position and servo 3 the same. All is good!

I’m looking for the code in the “IF” loops to add another button that will act independently and do the same thing but executing subroutines Extend_Right and Retract_Right, with servos connected to P4 and P5.

Any ideas???


1  #Sets an initial state (open)
begin

    0 get_position
    1 less_than  #Check if button is pushed
    if
          
        dup
        if  

            Extend_Left #Run the Extend_Left subroutine
            drop  #Change the state to 0
            0

        else  

            Retract_Left #Run the Retract_Left subroutine               
            drop #Change the state to 1
            1

        endif

    endif

repeat
### Sequence subroutines: ###

# Extend_Left
sub Extend_Left
   3 get_position  #check the position of servo 1
 
  4000 equals if  #see if it is equal to 4000
    8000         #if it is, set new target of 8000
  else
    4000         #if it is not, set new target of 4000
  endif
 
  3 servo 900 delay        #send the target position to servo 1

2 get_position  #check the position of servo 1
 
  4000 equals if  #see if it is equal to 4000
    8000         #if it is, set new target of 8000
  else
    4000         #if it is not, set new target of 4000
  endif
 
  2 servo 900 delay        #send the target position to servo 1


return
# Retract_Left
sub Retract_Left


2 get_position  #check the position of servo 1
 
  4000 equals if  #see if it is equal to 4000
    8000         #if it is, set new target of 8000
  else
    4000         #if it is not, set new target of 4000
  endif
 
  2 servo 900 delay        #send the target position to servo 1

3 get_position  #check the position of servo 1
 
  4000 equals if  #see if it is equal to 4000
    8000         #if it is, set new target of 8000
  else
    4000         #if it is not, set new target of 4000
  endif
 
  3 servo 900 delay        #send the target position to servo 1

  
  return

Have you tried adding that functionality? The logic for adding another button controlling a second set of servos would be very similar to what you already have, just repeated for the second button.

There might be some problems with just copy/pasting the same set of commands. For example, it looks like you are using a state variable for the left side to keep track of whether it is retracted or extended. To add a second state variable, you would probably want to use the swap command to make sure you are referencing the right one when looking at the button for the right side. Also, it looks like you are checking the servo positions in your Extend_Left and Retract_Left subroutines, which kind of makes the state variable redundant. You could probably simplify it by checking the servo position inside your main IF statement to see which state it is in and call the appropriate subroutine to change it to the other position. Then, you could do the same for a second button, which eliminates the need for 2 state variables.

If you try adding your second button and servos and run into problems, can you post the latest script you tried and a description of the problem?

Brandon

Hi Brandon!

Thanks for your help!

I will fiddle around with adding another button but I don’t understand, 0 Get Position and 1 Less than as is shown for the first button??

What would I put for the second button??

Buttons on P0, P1, servos on 2,3 for left, 4,5 will be for servos on the right controlled by button on P1

I understand state variable but don’t understand the logic/syntax in Pololu. What is the DUP statement you see in the IF statements?

Hello.

It sounds like you are just starting out with the Maestro scripting language. If that is the case, I recommend looking over the “Command Reference” section of the Maestro’s User’s Guide for a list of the available commands and how each of them works.

Additionally, before writing your own code, you might find it helpful to go through some of the “Example Scripts” to get an understanding of how the commands are used. The “Step script” button in the “Script” tab of the Maestro Control Center can be helpful for going through the script one line at a time and seeing the effects. If you are having problems understanding the syntax, it might be helpful to look up some information about the Forth programming language, which is what the Maestro scripting language is based on.

If you look through those sections and still have some confusion about how particular commands work, let me know and I can try to clarify.

Brandon

Thank You Brandon! I will check out your suggestions.