Programming the Robot
The robot will be programmed with the use of the program ROBOTC which can be downloaded from here:
http://www.vexrobotics.com/vexiq/software. You have to log in to your account. We will use their block programming
interface which is a graphical way of programming.
Before we begin the programming itself, we will go through how to update the firmware of the robot.
Next up we have set up ROBOTC so it knows which ports you have plugged in the different devices.
Hereafter we will examine the functions that we are going to use in order to make the robot act and sense. The best way to
understand how something works is by seeing it in action which is why we not only explain what the function does, but also
make see the robot’s response to it, so that you can experience it for yourself.
Updating the firmware
In order to update the firmware you have to install a program called “VEXos Utility”. It can be downloaded from here
http://link.vex.com/vexiq/downloads/vexos-utility-setup. This program is used to update the firmware of all the devices of
the robot, that is, sensors, actuators and the robot brain.
1.
2.
3.
4.
5.
Connect your PC to the robot brain using the usb cable.
Connect the sensors and the actuators to the robot brain.
Start the program.
Turn on the robot brain (use the button with the checkmark).
Now click on the “install button” in the program and wait until it finishes.
If you followed the steps, your robot should be ready to be programmed.
Configuring the robot sensors and actuators
In order for ROBOTC to program the robot it needs to know which devices are plugged into which ports on the robot brain.
You have to specify this and it is done by following these steps:
3.
1.
Open up ROBOTC (the graphical)
2.
a.
Press “New File”
Press “Motor and Sensor Setup”
4.
press:
a.
if you followed the assembly guide and plugged the devices into the same ports on the robot brain then you can
standard models
b.
choose “Clawbot IQ With Senosors”
5.
a.
i.
if you did not follow plug the devices into the same ports you have to do it manually
press motors
This lets you decide your own names (which is used in the programming activity) for the motors and the ports
they are connected to.
b.
press devices
i.
This lets you once again decide the names and the ports.
6.
When you are done press “Apply” in the bottom right corner.
Uploading a program to the robot
before we examine the functions you have to be able to make the robot use the functions. This is done by uploading a
program to the robot. First we create a small example program:
1.
2.
3.
Press “New File”
On the left side scroll down and locate a block where it says: “moveMotor”.
Click and hold the block and drag it to the left
4.
Now click where it says “motor10”, and select “clawMotor”
5.
replace the 1 with 50 and change the rotations to degrees.
6.
Click the “Compile Program” button. In case it asks you to save you should do so.
7.
8.
Now connect the robot brain and the PC using the USB cable.
Click the “Download to Robot” button
9.
Make sure the claw is open all the way
10.
Now press the start button
11.
Now the claw should close
Congratulations! You have now compiled and uploaded your first program.
Order Of Code Execution
When programming it is important to know in what order the lines of code are executed. Luckily the order is quite intuitive
for most people as the order of execution is from top to bottom. There are a few exceptions which will be explained when
appropriate.
Predefined Functions
Functions in regards to programming the Vex are used to either control an actuator or to acquire sensor information. There
will only be explanations of the functions used for the purpose of this project. Information about the rest of the functions
can be found by pressing “F1” within the ROBOTC program.
Before going into it, here is some advise: The best way of learning how to use these functions is to change the contents of
the fields, so you can get a feel of how it is functioning.
Display functions
The display on the robot can be used to show information about the sensors of the robot and also about variables (variables
will be discussed in a subsequent section). Using the display can be useful if the robot is acting in way you did not expect.
Example cases will be examined when discussing appropriate functions.
displayText
The purpose of this function is to display any text the programmer desires on the display of the robot. This is shown in the
figure below:
The display has five lines total you can output text on. The line is chosen in the first field, and the text to output is input in
the second field. This can be very useful when trying to determine where in the program the robot is currently executing.
To do this you should make the robot display text, when something of importance is happening. This will become more
clear as you learn more about programming.
displayMotorValues
This function will output the degrees of movement of the selected motor since the start of the program. The first field
determines the line on the display to output to, and the second field determines which motor to display values from.
The degrees will not be output continuously, they will only be output once per function call. So if you want it to show the
degrees continuously you have to put it in a loop. (Loops will be discussed in a subsequent section).
Write this program and upload it to the robot. Now run the program. Now move the wheels on the left side and watch the
degrees change on the display. This is useful for determining how many degrees the motors should turn in order to move
for example 20 cm.
displaySensorValues
This function does the same as the displayMotorValues function, however this shows values from the other sensors like the
color sensor. The second field determines which sensor to output values from.
displayVariableValues
This function also does the same, but will output variable values instead. Variables will be discussed in a subsequent
section.
Motor functions
moveMotor
The purpose of this function is to make a certain motor move. The movement can be set and the motor can be moved
based on one of five parameters:
Rotations:
o you can determine how many rotations the motor should move.
Degrees:
o you determine the amount of degrees the motor should move.
Milliseconds, seconds and minutes:
o determines the amount of time the robot should move.
Here is how to change the settings of the functions:
The first field (where it says “clawMotor”) is where you choose which motor you want to control.
the third field is where you decide if you want to make the motor move based on rotations, degrees or time.
the second field determines the amount of rotations, degrees or time.
the fourth field determines the speed of the movement.
setMotor
The purpose of this function is to make a certain motor move with a certain speed. The motor will move at that speed until
its speed is set to 0 in the program or the program finishes. So what is meant by the program finishes? The program finishes
when there is no more code to execute. It should become clear if you follow this example:
1.
2.
Write a program that only contains the setMotor function and where the speed is not set to zero.
upload and execute this program on the robot.
What happened? The robot did not do anything. This is because the program finishes almost immediately because the only
code there is to execute is the setMotor function. So how do you make so it does not stop immediately? Try inserting a wait
command.
1.
2.
Write the program to contain these two elements and nothing must be zero.
Upload and execute the program on the robot.
Now the robot should move at the given speed for the specified time.
The setMotor function is in some cases desired over the moveMotor function. This is because the moveMotor function in
some cases will make the robot move in jerks. An example of a case would be when you want the robot to move until
something happens, like the robot sensing that it has collided with something. If you want to do this with the moveMotor
function you have to call the function over and over again until the collision happens, and at every call the motor will stop
and start; which is called a jerk. But if you do this with the setMotor, you only call the function once to start the movement,
and when the collision happens you call the function again to stop the movement.
setMultipleMotors
This function is basically the same as the setMotor function, however this function allows you to set multiple motors at
once. Note that the motors will be set to same speed. This is useful if you want to make the robot go forward. In this regard
you may have to set one of the motors to reverse in the “motor and sensor setup”.
This will be the case if the motors are installed like so:
This is because the motors are installed opposite of each other, meaning that positive speed on one will turn the wheels in
the opposite direction of the other when its speed is positive. But setting one of them to reverse resolves the issue.
stopMotor
This function is equivalent to using the setMotor function where the speed is set to zero. The motor selected in the field is
the motor that will be stopped.
stopAllMotors
This function will stop all motors connected to the robot. This function is useful if you have used a lot of setMotor functions
and want them all to stop at once. There is a good example in the gyroSensor section. It uses two setMotor functions to
turn the robot, and then it uses the stopAllMotors, when the robot has turned 90 degrees counter clock wise.
forward
If everything is set up correctly in the “motor and sensor setup” this function will move the robot forward. The will move
forward based on the same parameters as was the case with the moveMotor function: rotations, degrees and time. And
the speed is set in the last field as well.
To make this function properly, motors that are responsible for moving the robot has to be set in the “motor and sensor
setup”. The motor on the left side of the robot has to be set as the left Drive Motor Side and the motor on the right side of
the robot has to be set as the right.
backward
This function is basically the same as the forward function, with the one difference that it makes the robot move backward
instead of forward.
Touch LED functions
setTouchLEDColor - Actuator
The purpose of this function is to set the color of the LED. In the first field you determine which device you want to use, and
it has to be a Touch LED sensor. It is useful because your robot could have more than one Touch LED. The second field is
drop-down menu, from which you can choose between thirteen different colors including no color.
SetTouchLEDRGB - Actuator
The purpose of this function is also to set the color of a Touch LED, however this function allows the programmer to set
each of the three channels individually (Red, Green and Blue). This allows for a much wider range of different colors than
the setTouchLEDColor function. The first field is the device, the second to the fourth field are fields for how much light each
channel should emit. The fields can understand number between 0 and 255. Where 0 is no light emitted and 255 is
maximum light. The second field is the red channel, the third is the green channel and the fourth is the blue channel. You
can use this tool: http://www.colorpicker.com/, to find colors represented in RGB format.
A Small Activity
All the functions that is associated with getting sensor data, are functions that “return” values. In essence this means that
the function call can be seen as a value, that is the value of the sensor data. In order to make use of the functions
associated with the sensors, you will have to use the sensor data for something. By using it for something is meant that the
function can only be called within these scenarios:
In a while loop
In a repeatUntil loop
In if statements
In if/else statements
In waitUntil statements
To assign the value of the sensor to a variable
Because of this it would be great if you write the following program, as a template you can use for testing the sensor
functions. It would have been good to know about loops because that is what the following template will
contain.Unfortunately it is not possible to do everything at once.
So because you have not yet read the part about while loops, it will be explained shortly here. The while loop will execute
all the code inside the loop while the statement to the right of where it says “while (“ and left of “) {”. The code that is
inside the loop is all the lines of code that are surrounded by orange. In the above example the while statement can be read
as follows: while the getTouchLED is not touch (that is touch is false). So as long as that statement is true the color of the
LED is red. So what happens when the touchLEDSensor is touched? Then the program breaks out of the loop (executes the
codes beneath the orange), and in the above example makes the LED light up lime green. Please note that when the sensor
is touched the program finishes.
GetTouchLEDValue - Sensor
So now that you have the template ready, it will be easy for you to see what happens. The purpose this function is to
determine whether or not the button has been touched. The function will return false if the sensor is not currently touched,
and will return true if currently touched.
Distance sensor functions
getDistanceValue
The purpose of this function is to return the value of the distance in millimeters to the object directly in front of the
distance sensor.
Now try to change the template you made in the “a small activity” section, so it changes the color of the LED when
something is closer than 100 millimeters away. It should look something like this:
You can see if this works by putting your hand in front of the distance sensor, and see if the color of the LED changes.
Color sensor functions
Regarding the use of this sensor it is very important that the environment is sufficiently lit, otherwise the sensor will return
very low values which is undesirable.
getColorRedChannel
The purpose of this function is to return the value of red color the sensor senses directly in front of it. The functions can
return value ranging from 0 to 255, where 0 corresponds to no red and 255 means a lot of red.
Now try to change the template you made in the “a small activity” section, so that the LED changes color when the sensor
returns more than some value of red color. (remember to have a well lit environment). It should look something like this:
If the LED does not change color try to either shine light on the red surface, or lower the value it should go above.
getColorGreenChannel
The purpose of this function is to return the amount of green color the sensor senses directly in front of it. This functions
works in the exact same way as the getColorRedChannel.
getColorBlueChannel
The purpose of this function is to return the amount of blue color the sensor senses directly in front of it. This functions
works in the exact same way as the getColorRedChannel.
Gyro sensor functions
resetGyro
The gyro sensor keeps track of how many degrees it has turned since the program started, or since the last time resetGyro
was called. So the purpose of this function is to reset the gyro sensor, that is set its internal degrees back to 0. This is useful
if you do not care about how much the robot has turned since the beginning of the program, but now want to turn for
example 90 degrees. This example will be shown when examining the getGyroDegrees function.
getGyroDegrees
The purpose of this function is to return the amount of the degrees the robot has turned since the beginning of the
program or since the last time the resetGyro was called.
Now you should try and turn the robot 90 degrees using the template created in the “a small activity” section. It should look
something like this:
Please note that if you want to turn 90 degrees to the right you need to change the degrees to - 90 and change the less than
sign to a greater than sign. And also change the signs of the setMotor functions so the wheels turn the other way.
Bumper switch functions
getBumperValue
The purpose of this function is to determine if the bumper is pressed or not. it returns false if not pressed and true if
pressed.
Now change the template made in the “a small activity” section, so that the color of the LED changes when the bumper is
pressed. It should look something like this:
Variables
Variables are used to store information, you can think of it as a data container. In the context of the Vex robot the
information to be stored is most likely sensor data or to use the variables to count how many times something has
happened.
There are three pre-defined functions associated with variables:
These will be examined shortly, but first we take a look at creating variables
Creating a variable
The first thing you need to is to take any one of the three predefined functions of the variables and move it to the left. Now
press where it says “Select Variable” and select the “Add Variable”:
Now the following box should show up:
In the text box, you write the name you want to give your variable. You can name it anything you like, however there are
some characters it does not allow for example the space character. Even though you can name the variable anything, it is
recommended to name it something that makes sense for the data it is storing. For example if you are using the variable to
count, you could name it counter. Or if you are using it to store the value of the distance sensor, you could call it distance or
something similar. When you have given the variable a name, press the “OK” button. Now your variable is ready to be used.
When using variables it is recommended that the variables are initialised as the first thing in your program. The reason for
this is that this makes you certain as to what starting value they have. This will become apparent in some of the examples in
the subsequent sections.
It is very important to mention that all the function fields that take written numbers can use the variable values as well.
Here is an example:
All the fields that contain numbers in the example could be by variable names, and the program will it self input the value of
the variable in its place.
Control structures
Waits
wait
The purpose of the wait is to pause the program for a specific time. The time can be specified in milliseconds, seconds and
minutes. The second field determines the unit of time, and the first field specifies the amount of specified units.
To see the use of this in action, try running this program on the robot:
waitUntil
The purpose of the waitUntil is to wait for something to happen. This can be useful if you want decide when a sensor value
is saved. Try this example:
The program will wait for you to touch the touch sensor and then display it on the LED.
The wait until is very useful when using the touch sensor. Let's say you want to count the amount of times the touch sensor
has been touched, you do not wait for the touch sensor to be “untouched” it will keep counting for as long as the sensor is
touched.
Even though you probably have not yet read the repeat and if sections, you should try these two examples that display why
it can be useful:
If you keep an eye on the display you will see that the number of presses is way too high. This is fixed by inserting the
waitUntil, so it “pauses” the program until you stop touching the sensor:
Loops
Repeat
The purpose of the repeat is to execute some code X number of times. The field determines the amount of times the code
inside the repeat should be executed.
Try this example to see it in action:
This should make the color of the LED change 4 times and waiting five seconds between each change.
Or try this example to see an example of the use of a variable as a counter:
This should make the LED shine more and more red for each run through the loop.
Repeat (forever)
The purpose of this structure is to make the code inside it repeat forever. This can be useful if you want the robot to do
something every time something is detected on one or more sensors.
Try this example:
This should continuously show the color detected by the color sensor on the LED. Please note that the color shown on the
LED is not very accurate. And also note once again that the environment should be well lit.
While
The purpose of this structure is to repeat the contained code while the statement is true. In the example shown, the code
inside will be repeated while the touch sensor is not touched.
The following example is similar to the example in the repeat forever, however the repeat forever has been changed to a
while. Try the example:
The color of the LED should be showing the color detected by the color sensor while the touch sensor is not touched. When
the touch sensor is touched the program will come out the while loop and execute the code after it. In the example the LED
should turn to a blue-green color when the touch sensor is touched.
The while structure can also be used with a counter, meaning that the program should go out of the while loop when
something has happened X number of times. Try this example:
This program will show the current color detected by the color sensor on the LED when the touch sensor is touched. Please
note that the program also waits until you “untouch” the touch sensor. The program uses the counter variable to count the
amount of times the touch sensor is touched. If you take a look at the while you will see that the program will show the
current color while the number of presses is less than 5. This means that the program will go out of the loop when the
touch sensor is touched five times, and show the blue-green color on the LED.
If
The purpose of the if is to execute some code when the statement is true. In the example above the code inside will be
executed if the touch sensor is touched.
Try this example:
This program will make the LED light red when the sensor is touched.
The if is also very useful when used with a counter. Try it out:
In this example the light of the LED changes for every touch on the touch sensor, and when the counter reaches three, it
start over with 0.
Here is one last example. Here the counter resets when an object is close to the distance sensor:
The previous example could have been programmed using waitUntil structures because there was only one input. However
this example has to check the value of the touch sensor and the distanse sensor, meaning that the program cannot pause
on one sensor.
If/else
This structure is similar to the if, however this also specifies what happens when the if statement is false. This is useful
when you want your program to do one of two things.
Try this example
If the touch sensor is not touched the LED will light red otherwise it will light green.
Two ways of programming the Vex IQ
This document has only examined one way of programming the Vex IQ; the graphical way. It is also possible to program
using text. There will be no examination of this in this document. With that being said, there is a way to turn your graphical
code into a text code. In the RobotC program select the “view” and then select the “Convert Graphical File to Text”.
© Copyright 2026 Paperzz