Need Help setting up a variable

I am using the maestro controller and cannot seem to create a variable successfully. I am trying to make a button that cycles a servo though two positions. here’s my code.

 begin
 
  if 7 get_position less_than 50 press_a
   endif
 repeat 

 sub reset
 button_presses = 0
  return

 sub press_a
  2000 0 servo
if button_presses = 3 reset
   endif
button_press + 1 = button_press
  return


Here’s the error “error applying parameters. script:8:2: Did no understand BUTTON_PRESSES” Thank You!

Hello.

The Maestro scripting language does not support the use of variables like that. It also does not support equations like you are showing in your script.

Instead of using a named variable, you can either store a value on the stack and refer to it later, or use a spare servo channel as a dummy channel to hold the variable and use get_position to read it back later.

For example, instead of button_presses = 0, you can initialize the variable by putting 0 on the stack before your main being/repeat loop. Then, your press_a subroutine would look something like this:

sub press_a
  2000 0 servo
  1 plus #add one to the value stored on top of the stack
  3 equals if  #if the value on the stack is equal to 3
    reset  #call reset subroutine
  endif   
return 

To “reset” the value back to 0, you can use the drop command to remove the value from the stack, then replace it with a 0, like this:

sub reset
  drop #drop top value on stack
  0 #replace value we dropped with 0
return

If you have a lot of other stuff going on in your script, it can get a bit more complicated because the value stored on the stack might not be on the top when you need to reference it. There are a few ways around this, but if you have a spare servo channel, using that as a dummy channel to hold the value of your variable might be easier; however, there are limitations to that too, since the target position of a servo channel must be within the “min” and “max” range configured for that channel in the “Channel Settings” tab. So, for example, if you wanted to store a value of 1, you could set the target position of a spare channel to 6001, then when you read the position back later, you can subtract 6000 from it to get your variable.

If you try making either of these changes and run into problems, you can post your updated script and I would be happy to take a look.

Brandon