beautypg.com

Using a button or switch to control servos – Pololu Maestro User Manual

Page 60

background image

8000 # go to 8000 for values 600-1023

endif

endif

0 servo

drop # remove the original copy of the pot value

repeat

The example above works, but when the potentiometer is close to 300 or 600, noise on the analog-to-digital
conversion can cause the servo to jump randomly back and forth. A better way to do it is with hysteresis:

# Set the servo to 4000, 6000, or 8000 depending on an analog input, with hysteresis.

begin

4000 0 300 servo_range

6000 300 600 servo_range

8000 600 1023 servo_range

repeat

# usage: servo_range

# If the pot is in the range specified by low and high,

# keeps servo 0 at pos until the pot moves out of this

# range, with hysteresis.

sub servo_range

pot 2 pick less_than logical_not # >= low

pot 2 pick greater_than logical_not # <= high

logical_and

if

begin

pot 2 pick 10 minus less_than logical_not # >= low - 10

pot 2 pick 10 plus greater_than logical_not # <= high + 10

logical_and

while

2 pick 0 servo

repeat

endif

drop drop drop

return

sub pot

1 get_position

return

This example uses one range for deciding where to go when making a transition, then it waits for the servo to leave a
slightly larger range before making another transition. As long as the difference (10 in this example) is larger than the
amount of noise, this will prevent the random jumping.

Note that this example will only work if you connect your potentiometer to one of the analog input capable channels
(channels 0–11). The inputs on the other channels are digital.

Using a button or switch to control servos

It is possible to connect a button or switch to a Maestro and detect the state of the button in your script. The script
below moves a servo through a predefined sequence of movements, advancing to the next step each time the button
is pushed. It uses channel 0 for the button and channel 1 for the servo.

The button channel must be configured as an input and wired correctly. See

Section 7.b

for instructions on how to

wire a button to your Maestro using a pull-up resistor, so that the input is normally high, and when the button is
pressed it goes low.

goto main_loop # Run the main loop when the script starts (see below).

# This subroutine returns 1 if the button is pressed, 0 otherwise.

# To convert the input value (0-1023) to a digital value (0 or 1) representing

# the state of the button, we make a comparison to an arbitrary threshold (500).

# This subroutine puts a logical value of 1 or a 0 on the stack, depending

# on whether the button is pressed or not.

sub button

Pololu Maestro Servo Controller User's Guide

© 2001–2014 Pololu Corporation

6. The Maestro Scripting Language

Page 60 of 73