When the sing task is done playing music, it stops the main task with the stop command. Then it turns the motors off. The order is critical. If we turned off the motors and then stopped the
main task, it's possible that main would turn on the motors again before it was stopped. Multithreaded programming is powerful but tricky.
Each RCX program can have up to ten tasks.
Subroutines
A subroutine is a group of commands that you will execute frequently. Subroutines offer a way to clean up your source code and reduce the size of compiled programs. Subroutines in NQC are
defined in much the same way as tasks. The following program has one subroutine, called wiggle(). The main task shows how this subroutine is called:
task main() {
wiggle();
Wait(200);
wiggle();
}
sub wiggle() {
OnFwd(OUT_A);
OnRev(OUT_C);
Wait(20);
OnFwd(OUT_C);
OnRev(OUT_A);
Wait(20);
Off(OUT_A + OUT_C);
}
Subroutines execute as part of the task from which they are called. It works just as if the call to wiggle() was replaced with the commands it contains. The nice thing about subroutines is that
their code is defined once, but you can call it as many times as you like from other places in your program. You could accomplish the same sorts of things with subroutines and macros, but
Need help?
Do you have a question about the MINDSTORMS Robots and is the answer not in the manual?
Questions and answers