Example below specifies a fixed distance to travel based on the diameter of the wheel. This example uses the .move() method to move for a specified distance. The amount of distance is the first parameter and the units of distance are the second parameter. The units may be centimeters (cm), inches (in), degrees of rotation (deg).
Moving Forward: Example 2
Example below specifies a fixed distance to travel based on the degrees of rotation using a while loop with a condition specifying the number of degrees. In the while condition the robot is to print the degrees observed to the console window. The .stop() method is required following the while loop. Simply exiting the while loop does not stop the robot by itself. You need to issue the .stop() method to stop the robot.
Moving Forward: Example 3
Example below specifies a fixed distance to travel based on the degrees of rotation using a while loop with a condition specifying the number of degrees. In the while condition the robot is to print the degrees observed to the console window. The .stop() method is required following the while loop. Simply exiting the while loop does not stop the robot by itself. You need to issue the .stop() method to stop the robot. This example also shows the distance that was traveled by the robot.
Line Following Examples
Example 1: One Sensor to control steering
Example below shows a line following program that controls the steering parameter of the move function. Input to the steering parameter comes from the light sensor readings.
Example 2: One Sensor to Control Steering
Example below shows a line following program that controls the steering parameter of the .start_at_power() method. Input to the steering parameter comes from the light sensor readings.
A function is "defined" in the program as having a specific "type" (or return value). Most functions are created as the type: "void". A function declared as the type: "void" starts with the word "void" before the name of the function as in the following:
// I am declaring a function of type void below
void driveForward() {
}
Notice how the function definition includes function type (void), name, parentheses, and braces "{}" (both an open and a close brace). The commands that you write between the two braces are what will be executed when the function is "called".
Declaring a Function with Arguments
Arguments (or "Parameters" as they are sometimes called) can be passed to the function by declaring them between the parentheses in the function's definition. For example:
In this example, the driveForward() function has an added parameter called: "distance" that was defined as a type: "int". In the example above, my "driveForward(distance)" function is "calling" an internal RobotC function called: "forward()" which has three parameters: amount, type, and power.
In my program, I could have just used the internal RobotC function called forward(). However, that function requires 3 parameters which means that I have to always include the amount, type, and power level parameters whenever I want to drive forward. My user defined function is a "short cut" allowing me to call my function with one parameter which will then automatically fill in the other two parameters when calling the internal RobotC function.
Defined BEFORE it can be Used
One note of caution. You have to define the function before you can use it. This seems to make sense. But what this means is that the tops of your programs may contain many function definitions before you get to the actual sequence of your program that makes the robot do its mission.
Note: Functions must be defined before the "main task".
The "task main() { }"
One very important function block in all RobotC programs is the "task main" (see below).
// This is the "main" task
task main() {
}
Program execution starts at the "main" task. The "main()" function is defined as a type "task". The "task" type is a special keyword and should be used only for the main() function. In order for your program to work, it needs to have one line (towards the bottom) that says: "task main() {"
It is possible to have more than one function defined with the type "task". Doing so would create a multi-threaded program which has some unique capabilities in responsiveness of the robot.
Exercise
Exercise: Write a small program that creates a function (before the main task). Call that function from within the main task.