Creating your first program

ROBOTC provides many sample programs that make for a great way to get started. We will be starting by using Dual Joystick Control.c

Opening the sample program

  1. Click on File in the menu bar.
  2. Click on Open Sample Program
  3. Make sure you are in the folder for VEX2 and then open the folder Remote Control
  4. Select Dual Joystick Control.c and open it up
  5. Go to File, Save as and save it in a safe location with a relevant name

Getting familiar

By now you should be looking at the sample program for single joystick control.

At the top you will notice the motor configuration, it should look like this:

1
2
3
#pragma config(Motor,  port2,           rightMotor,    tmotorNormal, openLoop, reversed)
#pragma config(Motor,  port3,           leftMotor,     tmotorNormal, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard

Here we have two motors configured, the right motor (named rightMotor) in port 2, and the left motor (named leftMotor) in port 3. If we click on Motor and Sensor Setup in the top bar and then go to the Motors tab, we can see the listing for all the motors and their ports. This setup window is the best way to configure your motors. Make sure to always give them a recognizable name, set the motor type to the correct one listed on the back of the motor, and enable or disable Reversed depending on your needs.

If we look further down in the file, we see the code for controlling the motors with the joysticks:

23
24
25
26
27
28
29
30
31
32
task main ()
{

  while(1 == 1)
  {
    motor[leftMotor]  = vexRT[Ch3];   // Left Joystick Y value
    motor[rightMotor] = vexRT[Ch2];   // Right Joystick Y value
  }

}

Here we can see one of the most basic programs you can write in ROBOTC. The program begins with task main () which is the way of telling the computer, this is where our code begins.

Next we see while(1 == 1), this is an infinite loop that will run whatever code is inside the curly braces as long as 1 is equal to 1, or forever.

Going down further we see motor[leftMotor] this is the way we access one of our named motors. Then we see it is equal to vexRT[Ch3], which if you look on your controller, corresponds with the label for vertical axis of the left joystick. By saying motor[leftMotor] = vexRT[Ch3], we are telling the robot to set the motor power for leftMotor to the value of the Ch3 joystick, which is 0 when its in the middle, -127 when it is all the way down, and 127 when it is all the way up. We see the same idea with rightMotor. Taking this basic idea, you can change which channels motors use and add more motors on the same or different channels.

Adding your own controls

While it is nice to be able to control motors by different joystick channels, the controller has tons more buttons that can also be utilized. Here are some examples on how to add more functionality to your bot.

Adding an arm

In this example, we will be using a motor named arm that is controlled by Btn5U and Btn5D on the controller.

if(vexRT[Btn5U])
{
    motor[arm] = 127;
}
else if(vexRT[Btn5D])
{
    motor[arm] = -127;
}
else
{
    motor[arm] = 0;
}

This here is a basic, if else-if else. The program will check the first condition (vexRT[Btn5U]) to see if it is true, and if it is it will run the code inside the first set of curly braces. If the first condition is not true, it will then look at the next one (vexRT[Btn5D]), if that is true it will run the code inside the second set of curly braces. If none of those are true, it will run the code in the last set of curly braces.

You may be thinking why do “I need else if”? The reason we use else if is because we want to make this one whole statement where if the first one is not true it will then check the second one and if that is not true, finally default on the last one. A common mistake is the make two separate if else statements like this:

//Do not do it like this

if(vexRT[Btn5U])
{
    motor[arm] = 127;
}
else
{
    motor[arm] = 0;
}

if(vexRT[Btn5D])
{
    motor[arm] = -127;
}
else
{
    motor[arm] = 0;
}

The problem with this is that if you press Btn5U it will trigger the arm to go at 127 power as expected, however when the program reaches the next if statement for Btn5D, it will be false and it will set the arm to 0. This will cause the bot to spasm between to contradicting statements. By simply combining it into an if else-if else we avoid this by making sure the arm will stop only if both are buttons are not pressed.