BUTTON PROGRAMMING
Programming buttons in RobotC is a little different then program a joystick in RobotC. To retrieve a value from a button in RobotC, you would write “vexRT[Btn#];” # would be replaced with the number of the button. This statement returns either 0 or 1: 0 if the button is up and 1 if the button is down. Since 0 stands for false in C and 1 stands for true in C, one can treat this statement like a boolean. Therefore, one would usually use an if – else statement when using a button. Hence, an example code would be:
task main()
{
while (true)
{
if (vexRT[Btn6]) // button6 pressed on the main controller
{
motor[port1] = 127; // power the motor
}
else // otherwise…
{
motor[port1] = 0; // turn the motor off
}
}
RUNNING MOTORS
}
When button 6 is pressed, the motor plugged into port one will run. However, if the button is not pressed, the motor will be stopped.
Similar to programming partner controller joystick’s in Vex, one must append “Xmtr2” after the channel number to talk to the partner controller. Thus, instead of typing “vexRT[Btn#];”, you would write “vexRT[Btn#Xmtr2];” Using this knowledge, to write the same code as above but using the partner controller as well as port 2, one would write:
task main()
{
while (true)
{
if (vexRT[Btn6Xmtr2]) // button6 pressed on the partner controller
{
motor[port2] = 127; // power the motor
}
else // otherwise…
{
motor[port2] = 0; // turn the motor off
}
}
}
And when the first and second codes are combined, it would look like:
task main()
{
while (true)
{
if (vexRT[Btn6]) // button 6 pressed on the main controller
{
motor[port1] = 127; // power the motor
}
else // otherwise…
{
motor[port1] = 0; // turn the motor off
}
if (vexRT[Btn6Xmtr2]) // button 6 pressed on the partner controller
{
motor[port2] = 127; // power the motor
}
else // otherwise…
{
motor[port2] = 0; // turn the motor off
}
}
}
When you press the button labeled 6 on the main controller, motor 1 will run. Similarly, when you press button 6 on the partner controller, motor 2 will run. Now let’s download the code to the robot and test it! Remember to have you compiler mode set to VEXnet or USB when using remotes.
Cheers!
task main()
{
while (true)
{
if (vexRT[Btn6]) // button6 pressed on the main controller
{
motor[port1] = 127; // power the motor
}
else // otherwise…
{
motor[port1] = 0; // turn the motor off
}
}
RUNNING MOTORS
}
When button 6 is pressed, the motor plugged into port one will run. However, if the button is not pressed, the motor will be stopped.
Similar to programming partner controller joystick’s in Vex, one must append “Xmtr2” after the channel number to talk to the partner controller. Thus, instead of typing “vexRT[Btn#];”, you would write “vexRT[Btn#Xmtr2];” Using this knowledge, to write the same code as above but using the partner controller as well as port 2, one would write:
task main()
{
while (true)
{
if (vexRT[Btn6Xmtr2]) // button6 pressed on the partner controller
{
motor[port2] = 127; // power the motor
}
else // otherwise…
{
motor[port2] = 0; // turn the motor off
}
}
}
And when the first and second codes are combined, it would look like:
task main()
{
while (true)
{
if (vexRT[Btn6]) // button 6 pressed on the main controller
{
motor[port1] = 127; // power the motor
}
else // otherwise…
{
motor[port1] = 0; // turn the motor off
}
if (vexRT[Btn6Xmtr2]) // button 6 pressed on the partner controller
{
motor[port2] = 127; // power the motor
}
else // otherwise…
{
motor[port2] = 0; // turn the motor off
}
}
}
When you press the button labeled 6 on the main controller, motor 1 will run. Similarly, when you press button 6 on the partner controller, motor 2 will run. Now let’s download the code to the robot and test it! Remember to have you compiler mode set to VEXnet or USB when using remotes.
Cheers!