From EasyC to RobotC

This article will show you how to get used to using RobotC from being used to using EasyC. This article assumes that you have knowledge on how to program with EasyC.

What are the differences between the two?

Well, the use of RobotC marks your graduation from "little kid" to "big kid". Jokes aside, RobotC is significantly different from EasyC. The biggest difference is that in RobotC, you actually type everything. No more dragging and dropping. This may make RobotC seem to be quite a bit more difficult to use than EasyC, but after you get the hang of it, you may find it much faster to use RobotC since you don't need to drag an "if" when you can type "if" instead.

Syntax Differences

Here are the syntax differences between EasyC and RobotC:

Variable Typing

Whenever you declare a variable, you must first give it a type.

int - This represents an integer. For those who are familiar with this term, an integer is a regular non-decimal number. Ex: 34 or -19.
float - This represents a decimal number. Ex: 1.35 or 9.33.
bool - A boolean represents a value of either true or false. That's all there is to it.

These should be most of the types you will use when programming with RobotC.

To type a variable, you put it before the name of the variable.

Example:

int bobette = 2; // This is an integer

float bobman = 4.8; // This is a floating-point decimal

bool isBob = false; // This is a boolean

Semicolons

A statement is a piece of code that makes the robot do something. In RobotC, after every statement, you place a semicolon. DON'T FORGET THIS!

Example:

int jello = 5; // This is a statement, notice the semicolon

motor[port5] = vexRT[Ch3]; // Another statement

Control Statements

The following example will illustrate how control statements (if, while, etc) are used in RobotC:

int bob = 2;

if (bob == 3) // <- Condition to check for is inside brackets
{ // <- Curly brace to open if statement

   bob = 2; // <- This is a statement inside if statement. Notice the semicolon

   int china = 23;

} // <- Curly brace to close if statement. You DO NOT use a semicolon to close control statements.

Basic things to know

Here are the most basic and important things to know in order to program in RobotC:

Motor Control

To control the motors, all you need to know is the following statement:
motor[port#]= power;
Where port# is a port from 1-10 (Ex. port3) and power is a number from -127 - +127.

Here is an example of motor control in action:

while (true)
{
   motor[port2] = 110; // Spins motor at port 2 at a power of 110 clockwise
   motor[port3] = -127; // Spins motor at port 3 at a power of 127 anticlockwise
}

Joystick Detection

This is how you can detect events made by the joystick:
vexRT[Ch# OR Btn#Direction]
If you want to use the two analog sticks, then you use a channel number inside vexRT (Ex. vexRT[Ch3]). vexRT when used with the analog sticks will return a value from -127 to +127.

If you want to use the buttons, then use the button number, then direction, D for bottom button, U for upper botton. (Ex. vexRT[Btn5U]). vexRT used with buttons will return a value of 0 or 1. (0 for not pressed, 1 for pressed).

Here is an example of some code with joystick action:

while (true)
{
   motor[port2] = vexRT[Ch3]; // Sets the power of the motor in port 2 depending on how far the left analog stick is tilted vertically

   if (vexRT[Btn6D] == 1) // If bottom button on right side is pressed...
   {
      motor[port10] = 20; // ...then set the power of the motor at port 10 to 20
   }
}

Sensing

To use sensors, all you need this simple command:
SensorValue[in# OR dgtl#]
Use in# with analog sensors and dgtl# with digital sensors.

NOTE: Sensor values that will be returned will be the opposite to the values that EasyC returns. (i.e. 0 for not-pressed and 1 for pressed)

Example code time:

while (true)
{
   if (SensorValue[dgtl3] == 1) // If the digital sensor in port 3 is pressed...
   {
      motor[port3] = 50; // Set the power of the motor in port 3 to 50
   }
   if (SensorValue[in2] > 52) // If the analog sensor in port 2 returns a value greater than 52...
   {
      motor[port2] = 0; // Motor in port 2 is stopped.
   }
}

Code Snippets

Below are code snippets in RobotC for certain functions in EasyC. Place the code in the while loop in the user control task.

Tank Control

The following 2 lines of code will replicate the tank drive found in EasyC:

motor[portL] = vexRT[Ch3];
motor[portR] = vexRT[Ch2];

Make sure you replace L and R with the respective motor port numbers. Also, make sure one of the ports is reversed in the Motors and Sensors Setup.

More Information:

For more information check out this awesome RobotC Video Trainer.

Last edited Jan 30, 2011 at 6:48 AM by fusionlord, version 13

Comments

No comments yet.