User Manual

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:
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Set the servo to 4000, 6000, or 8000 depending on an analog input.
begin
1 get_position # get the value of the pot, 0-1023
dup 300 less_than
if
4000 # go to 4000 for values 0-299
else
dup 600 less_than
if
6000 # go to 6000 for values 300-599
else
8000 # go to 8000 for values 600-1023
endif
endif
0 servo
drop # remove the original copy of the pot value
repeat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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: <pos> <low> <high> 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
?
?
Pololu Maestro Servo Controller User’s Guide © 2001–2017 Pololu Corporation
6. The Maestro Scripting Language Page 80 of 99