The Label and Jump commands are used together to create loops and make decisions.
Subroutines can be created using the Subroutine, Jump and Return commands together.
You can use the Label command to give a line its own unique name — in this example the line’s name is Start:
Label Start
You can then refer to this label by name in a Jump command, anywhere else in a sequence:
Jump To Start
This simple loop will play forever, flashing the LED named Status on and off every second. When the Jump command is played, MECControl jumps to the line labelled Start and continues playing the sequence from there:
Label Start
Light Status
Wait 1 Second
Light Status Black
Wait 1 Second
Jump To Start
You can add a condition to a Jump command, so that the jump only occurs if the condition is met.
In this loop, the Status LED will flash until the Pressed action of a Button named StopButton occurs:
Label Start
Light Status
Wait 1 Second
Light Status Black
Wait 1 Second
Jump To Start Until StopButton Pressed
You can create loops that play a set number of times by testing the number stored inside a variable.
In this loop, the Status LED will flash five times:
Let Counter = 0
Label Start
Light Status
Wait 1 Second
Light Status Black
Wait 1 Second
Let Counter = Counter + 1
Jump To Start Until Counter = 5
Using symbols other than Equal ( = ) you can perform other tests on the number stored inside a variable:
In this example, a Motor named Jib will run Clockwise if the Pressed action of a Button named RaiseJibButton occurs, or Counterclockwise if the Pressed action of a Button named LowerJibButton occurs:
Label Start
Jump To RaiseJib If RaiseJibButton Pressed
Jump To LowerJib If LowerJibButton Pressed
Jump To Start
Label RaiseJib
Run Jib Clockwise
Jump To Start
Label LowerJib
Run Jib Counterclockwise
Jump To Start
If you use a Wait command to wait for a Keypress, you can a make decisions based on the Key that was Pressed, as in this re-working of the previous example::
Label Start
Wait For Keypress
Jump To RaiseJib If Up Key Pressed
Jump To LowerJib If Down Key Pressed
Jump To Start
Label RaiseJib
Run Jib Clockwise
Jump To Start
Label LowerJib
Run Jib Counterclockwise
Jump To Start
Now the Jib motor will run Clockwise if you press the Up cursor key, and Counterclockwise if you press the Down cursor key. You can test to see if any of the following keys have been pressed:
You can also test to see if any key has been pressed, or no key has been pressed:
Jump To RaiseJib If Any Key Pressed
Jump To LowerJib If No Key Pressed
You can also access a property of a device and use it to make a decision, as in this example:
Jump To Start If Arm Position > 45 Degrees
Here the jump to the Start label will occur only if the Position property of a Meccanoid Servo Motor named Arm is greater than 45 degrees clockwise.
If you find yourself writing the same list of commands over-and-over, you could instead create a subroutine containing those commands. Here is the flashing LED loop example, re-written as a subroutine:
Subroutine FlashStatusLED
Let Counter = 0
Label Start
Light Status
Wait 1 Second
Light Status Black
Wait 1 Second
Let Counter = Counter + 1
Jump To Start Until Counter = 5
Return From Subroutine
Notice the addition of the FlashStatusLED Subroutine label, which gives the subroutine a name, and the Return command. These allow you to use to access the subroutine from anywhere else in the sequence, as in this example:
Jump To FlashStatusLED
Wait 10 Seconds
Jump To FlashStatusLED
Wait 10 Seconds
Jump To FlashStatusLED
Wait 10 Seconds
Here the subroutine runs three times, separated by 10 second pauses.
Whenever the Return command inside the subroutine is played, MECControl returns to the line after the last Jump command and continues playing the sequence from there.