User Manual
Note that BEGIN…WHILE…REPEAT loops are similar to the while loops of many other languages.
But, just like everything else in the Maestro script language, the WHILE comes after its argument.
For conditional actions, an IF…ELSE…ENDIF structure is useful. Suppose we are building a system
with a sensor on channel 3, and we want to set servo 5 to 6000 (1.5 ms) if the input value is less
than 512 (about 2.5 V). Otherwise, we will set the servo to 7000 (1.75 ms). The following code
accomplishes this relatively complicated task:
As in most languages, the ELSE section is optional. Note again that this seems at first to be backwards
relative to other languages, since the IF comes after the test.
The WHILE and IF structures are enough to create just about any kind of script. However, there are
times when it is just not convenient to think about what you are trying to do in terms of a loop or a
branch. If you just want to jump directly from one part of code to another, you may use a GOTO. It
works like this:
In this example, only servo 2 will get set to 4000, while servo 1 will be unchanged.
Subroutines
It can be useful to use the same sequence of commands many times throughout your program.
Subroutines are used to make this easier and more space-efficient. For example, suppose you often
need to set servo 1 and servo 2 back to their neutral positions of 6000 (1.5 ms) using the sequence
“6000 1 servo 6000 2 servo”. You can encapsulate this in a subroutine as follows:
1
2
3
4
5
6
7
8
9
10
10 # start with a 10 on the stack
begin
dup # copy the number on the stack - the copy will be consumed by WHILE
while # jump to the end if the count reaches 0
8000 1 servo
500 delay
4000 1 servo
500 delay
1 minus # subtract 1 from the number of times remaining
repeat
1
2
3
4
5
6
7
3 get_position # get the value of input 3 as a number from 0 to 1023
512 less_than # test whether it is less than 512 -> 1 if true, 0 if false
if
6000 5 servo # this part is run when input3 < 512
else
7000 5 servo # this part is run when input3 >= 512
endif
1
2
3
4
5
goto mylabel
# ...any code here is skipped...
4000 1 servo
mylabel: # the program continues here
4000 2 servo
?
?
?
Pololu Maestro Servo Controller User’s Guide © 2001–2019 Pololu Corporation
6. The Maestro Scripting Language Page 71 of 102










