© Fort Street High School Robotics
Now that we've learnt a tiny bit of C, let's quickly look at RobotC. Everything in the table from 1.2 is the same, except:
Code | RobotC Code | Comment |
---|---|---|
#include <stdio.h> |
- | Not needed! Instead, some automatically generated code replaces this. |
int main() { ... } |
task main() { ... } |
Instead of returning an int, we call them 'tasks'. |
return 0 |
- | Tasks do not require return values. |
Let's look at a program that runs Motor A for one second, then stops.
#pragma config(Motor, motorA, motorL, tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main() {
motor[motorL] = 100; // SETS the motor speed
wait1Msec(1000); // Keeps running at 100 speed for 1 second
motor[motorL] = 0; // sets speed to 0
}
Key things to note:
=
makes a change to a particular variable. Here, it's changing the motor speed variable.1000
in wait1Msec(1000)
is an argument to the wait function. We can change this number if we want it to be longer or shorter.motor[motorL] = 100
, it will run at 100 speed forever until it is stopped or the program terminates.Given the following starting code, copy the code into a new file, call it simplemotor.c
and save it in your Robotics folder.
#pragma config(Motor, motorA, motorL, tmotorNXT, PIDControl, encoder)
#pragma config(Motor, motorB, motorR, tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main() {
motor[motorL] = 30; // SETS the left motor speed
motor[motorR] = -30; // SETS the right motor speed to go backwards
wait1Msec(1000); // Keeps running for 1 second
motor[motorL] = 0; // sets speed to 0
motor[motorR] = 0; // sets speed to 0
}
Ask for the code to be uploaded to a demo robot and see if it works!