Potentiometer to control servo trvl w/ switch to start scrip

Hello

I have a micro maestro servo controler been using it with a switch to control 6 servos works great with your help thanks. Have a new project want to control 1 servo with a switch but use a pot to set the position of the servo make the servo position adjustable how do list the code properly.I think i understand this has an input from a switch on channel 0 and moves a servo connected to channel 2 from 4000 to 8000 each time the button is pressed.

goto main_loop 
sub button 
  0 get_position 500 less_than
  return
  
# This subroutine uses the BUTTON subroutine above to wait for a button press, 
# including a small delay to eliminate noise or bounces on the input. 
sub wait_for_button_press 
  wait_for_button_open_10ms 
  wait_for_button_closed_10ms 
  return
  
# Wait for the button to be NOT pressed for at least 10 ms. 
sub wait_for_button_open_10ms 
  get_ms # put the current time on the stack 
  begin
    # reset the time on the stack if it is pressed 
    button 
    if
      drop get_ms
    else
      get_ms over minus 10 greater_than
      if drop return endif
    endif
  repeat
  
# Wait for the button to be pressed for at least 10 ms. 
sub wait_for_button_closed_10ms 
  get_ms
  begin
    # reset the time on the stack if it is not pressed 
    button 
    if
      get_ms over minus 10 greater_than
      if drop return endif
    else
      drop get_ms
    endif
  repeat
  
# An example of how to use wait_for_button_press is shown below: 
  
# Uses WAIT_FOR_BUTTON_PRESS to allow a user to step through 
# a sequence of positions on servo 2. 
main_loop:
begin
  4000 frame 
  5000 frame 
  6000 frame 
  7000 frame 
  8000 frame 
repeat
  
sub frame 
  wait_for_button_press 
  2 servo
  return

Below From the user manual is this the script to to use a pot to set the servo position? I would like to be able to adjust servo position from one end of the travel range to the other.

# Sets servo 2 to a position based on an analog input. 
begin
  1 get_position    # get the value of the pot, 0-1023 
  4 times 4000 plus # scale it to 4000-8092, approximately 1-2 ms 
  2 servo           # set servo 2 based to the value 
repeat
goto main_loop 
sub button 
  0 get_position 500 less_than
  return

below is how i think it would go together with the botton press to move the servo to the location set by the pot. is this correct

# This subroutine uses the BUTTON subroutine above to wait for a button press, 
# including a small delay to eliminate noise or bounces on the input. 
sub wait_for_button_press 
  wait_for_button_open_10ms 
  wait_for_button_closed_10ms 
  return
  
# Wait for the button to be NOT pressed for at least 10 ms. 
sub wait_for_button_open_10ms 
  get_ms # put the current time on the stack 
  begin
    # reset the time on the stack if it is pressed 
    button 
    if
      drop get_ms
    else
      get_ms over minus 10 greater_than
      if drop return endif
    endif
  repeat
  
# Wait for the button to be pressed for at least 10 ms. 
sub wait_for_button_closed_10ms 
  get_ms
  begin
    # reset the time on the stack if it is not pressed 
    button 
    if
      get_ms over minus 10 greater_than
      if drop return endif
    else
      drop get_ms
    endif
  repeat
  
# An example of how to use wait_for_button_press is shown below: 
  
# Uses WAIT_FOR_BUTTON_PRESS to allow a user to step through 
# a sequence of positions on servo 2. 
main_loop:
# Sets servo 2 to a position based on an analog input. 
begin
  1 get_position    # get the value of the pot, 0-1023 
  4 times 4000 plus # scale it to 4000-8092, approximately 1-2 ms 
  2 servo           # set servo 2 based to the value 
repeat

goto main_loop 
sub button 
  0 get_position 500 less_than
  return

  
sub frame 
  wait_for_button_press 
  2 servo
  return

Thanks for all your help
Bobby

Hello, Bobby.

I’m glad you have had success using the Maestro.

Did you write the sentence “below is how i think it would go together with the botton press to move the servo to the location set by the pot. is this correct” at the wrong position? Since it is in the middle of your script, you will need to put a # sign at the beginnings of those lines to make your script compile.

Your script (the script at the bottom of your post) contains two definitions of the subroutine “button”. You should pick one and delete the other (but don’t delete the “goto main_loop” at the very top of the script).

In order for a subroutine to have an effect, it has to be called at some point in your program. Your main loop doesn’t actually call any of the subroutines you defined, so your entire script could be simplified down to just the following (which is not what you want):

  begin
    1 get_position
    4 times 4000 plus 
    2 servo
  repeat

I’m not sure what this sentence means:

It sounds like you want to have two input devices (switch and potentiometer) that affect the position of the servo. If the servo is controlled with a switch, then how exactly does the pot set the position of the servo?

Here is my guess and what you meant: Define two servo positions, Position A and Position B. While the switch is pressed, the servo always goes to Position B and while the switch is not pressed it always goes to Position A. The potentiometer would control the value of Position B. This is definitely doable with the Maestro, but is this what you want? Do you need control over Position A too?

–David

David

Thanks for your response. Your last phrase is exactly what I am trying to do. The servo is at position A the button is pushed , the servo goes to position B, position B is determined by the pot, and then the switch is released and servo returns to position A. Can you help with order of the script. I understand the switch and the positions of A & B in the script. I dont understand how to add the pot to create position B as variable. I am trying to use a servo to create a high idle condition on a gasoline engine when it goes into gear(my switch) and the pot will allow me to adjust the level of me high idle

Thanks
Bobby

The Maestro has limited support for storing variables, so instead of storing the position as a variable we can just make a subroutine (named “position_b”) that computes the position and puts it on the stack. Here is a script that should work for you (though I have not tested it myself):

begin
  button                               # Get button state and store it on the stack.
  if position_b else position_a endif  # Compute desired servo position.
  2 servo                              # Set servo's target position
repeat

sub button
  0 get_position 500 less_than
  return

sub position_a
  4000
  return

sub position_b
  1 get_position    # get the value of the pot, 0-1023 
  4 times 4000 plus # scale it to 4000-8092, approximately 1-2 ms 
  return

I recommend that you read the script carefully and, with the aid of the Maestro User’s Guide, try to understand every part of it.

The script above will allow you to turn the pot while the button is pressed, and you will see the servo change position immediately. One potential drawback: if there is noise on the potentiometer input line, then your servo might wiggle a little bit while the potentiometer is pressed. If you find that this is a problem for you, let me know and I can help you fix it.

–David

David

#1 with your script the pot adjusts the servo position without the button pressed and no change in servo position when the when the button is pressed and pot adjusted. I need to adjust the servo with pot when the button is pressed.

Channel 0 switch
Channel 1 100k pot
Channel 2 servo

#2 how come when I am in the status tab of the maestro control center channel 0 slider moves when I change the resistance of the pot on channel 1

#3 the servo does have a lot of bounce when the switch is not pressed

Thanks
Bobby

David

I switched the (a) & (b) in the sub position lines of your script and can now adjust the position of the servo with the pot when the switch is pressed(closed) but am still confused why the pot changes the the slider of the switch in the status tab of the control center. Also the pot has to be in a small range for the switch to work, what i am trying to say the switch will not move the servo unless the pot is set in a small range but when the switch is pressed(closed) i can move the servo through most of its range with the pot. both the pot and switch are set as inputs. You asked earlier if I wanted the servo to return to position A when the button is released(open) I do. Would like it to start at one end of the servos limit and go to the other end of the servos limit when swich is pressed(closed) and be able to tune it back with the pot to set my high idle RPM. when the switch is open it will return to the low idle RPM.

Thanks
Bobby

It sounds like the program was misunderstanding when you have your switch pressed and when you don’t have it pressed. You must have your switch hooked up in the opposite way from what I was expecting, so that the input line actually goes high (5 V) when the switch is pressed and stays low otherwise. Instead of changing the definitions of position A and position B (which will probably confuse us a lot because you go on to talk about position A later in your post), I recommend that you just fix the “button” function:

# returns 1 if the button is pressed, 0 if the button is not pressed
sub button
  0 get_position 500 greater_than
  return

Do you understand what this change does and why it would solve this problem?

By how much does the Position of Channel 0 change when you turn the pot? Maybe the channel 0 input is floating; how did you hook up the switch to it? Did you use an external pull-up or pull-down resistor?

I’m not sure what you mean by “bounce”. What is the servo bouncing off of? I was expecting that the servo might move back and forth by tiny amounts on its own without you touching it. Is that what is happening?

Yes, right now the scaling code in the “position_b” function allows the servo to move through most of its range (1000-2000 us) while you turn the pot. You can change the scaling code to restrict the servo to a smaller range if you want.

–David

David

I believe changing the stack to 1 when the switch is pressed and 0 when it is not pressed? and the value is graeter than 500 when the switch is pressed? This did work with your script like my modifications did. using your script and your modifications

My switch is the switch assembly i have been using for my projects it is assembled as the user guide shows a pull up resistor. my switch is hooked to a 6 volt battery with a resistor between. the battery also powers the maestro and my servos.

The slider for the switch follows the pot they both start at 0 and if I get a higher value than 160-165 on the target for the pot the servo starts to move with the switch not pressed. With a pot value of 160-165 the switch value is 118. With the pot maxed on the slider(not on the pot itself) the switch value is 210 and if I turn the pot max the value on the switch goes to 255

The bounce I see comes when I get the pot value on the slider to 165 and higher than that it just moves the servo more. The pot value ofover 165 sets the servo position with out pressing the switch. When the pot value gets to 165 the servo slider moves exactly as the pot slider matches its position.

the scaling for the pot to the servo movement is good with button pressed right now maybe we can get into that later. I think my biggest problem now is why the switch and pot have a relation I thought they were 2 separate inputs. And why does the pot move the servo without the switch closed.

Thanks
Bobby

They are two separate inputs, but the Maestro’s microcontroller only has one ADC (Analog-to-Digital converter) which is used to measure both inputs, so that’s why the voltage on one input can affect the voltage on another input. However, this should not be a problem if your button/switch is wired correctly.

This diagram from the “Attaching Servos and Peripherals” section of the Maestro User’s Guide shows the correct way to hook up a button:

If you connected the pull-up resistor correctly then the voltage on channel 0 should be 5 V or more when the button is unpressed (open). This is not what you are seeing in the Maestro control center, so it makes me think there is something wrong with your wiring. Maybe you could explain in more detail all of your connection and/or post some pictures here. Do you have a multimeter so you can measure the voltage on channel 0 and verify that it is what you would expect?

–David