Potentiometer Input with Hysteresis


I am brand new to coding and was hoping someone could help me. I am using a Mini Maestro 12. I would like to use multiple potentiometers to control multiple pwm outputs using hysteresis. Is it even possible for the Mini Maestro 12 to operate four channels that way? I have successfully followed the servo controller user guide provided by Pololu to use one potentiometer to control one output. I understand how to wire my pots, how to select the correct mode in the channel settings, and where in the code to select the source and where to select the pwm output. The trouble I am running into is knowing where to do what when adding additional channels to the script. My errors are usually related to not closing begin/repeat loops, or the section of code that I add does not run at all. If someone could write the code as a reply that would be the most helpful to me. Below I have copied the script from the user guide and added some additional questions in bold. Thanks.

Do I need to add a new servo range for each new channel or can I use this for all additional channels?

  4000 0 300 servo_range
  6000 300 600 servo_range
  8000 600 1023 servo_range

Do I need a seperate servo_range sub routine, if so where does it go?

sub servo_range
  pot 2 pick less_than logical_not    # >= low
  pot 2 pick greater_than logical_not # <= high

I would like to control a switch as well, does this code fit inside the begin and repeat section just below this or does it go some where else?

2 get_position
dup 200 greater_than
8 servo

      pot 2 pick 10 minus less_than logical_not   # >= low - 10
      pot 2 pick 10 plus greater_than logical_not # <= high + 10
      2 pick 0 servo
  drop drop drop

Why is this sub routine outside the repeat command? Could it be placed at the top of the script?

sub pot
  1 get_position


Since you are new to coding, I suggest taking small steps to better understand how the Maestro scripting language works. A good tool for this is the “Step script” button in the “Script” tab of the Maestro Control Center. Using this button, you can progress the script a single step and investigate to see what changed. For example, you might consider going through the example scripts one at a time using the “Step script” button to get an understanding of how each command works. In particular, the “Compressing the sequence” example demonstrates how subroutines work and why they are useful.

Because of the way the “Using an analog input to control servos” example script with hysteresis works it would not be trivial to expand it to work with multiple inputs and servos. It checks to see if the input is within the specified range, and if it is, it enters a while loop to add the hysteresis and move the servo to the target specified for that range. It does not exit this while loop until the input is outside of the range with hysteresis taken into account. If you simply tried to copy/paste to make a second subroutine of servo_range for a different input, it would not reach it until it leaves the while loop.

It might be possible to write a script that will handle multiple analog inputs with hysteresis to control separate servos, but it would take a much more complicated script than what is used in the example. You would probably need to keep track of which range each input was in during the previous cycle through the code to know when to apply the hysteresis. There are a few ways you might be able to do this, such as a value on the stack that specifies which range each servo is in (e.g. a state variable) or using a separate Maestro channel as a dummy channel to represent a value. Alternatively, you might be able to use the get_position command to check the current target of the servo channel and apply hysteresis based on that; however, please note that if you are using a speed or acceleration limit on that servo channel, the position returned could be some intermediate position if it is being checked before the servo channel finishes reaching the final target position.