RobotC Sample Programs (NXT)
Motors
Sample programs using basic motor and timing commands with no sensors.
Moving Forward |
NXT |
|
| Make the robot run forward for 4 seconds, using motor power commands and Wait commands. | ||
Forward
then Reverse |
NXT |
|
| Make the robot run forward for 4 seconds then backward for 4 seconds, using motor power commands and Wait commands. | ||
Modifiers |
NXT |
|
| Make the robot run at various speeds by assigning different power settings to the motors. | ||
Point Turns |
NXT |
|
| Set the robot's motors to run in opposite directions to make the robot turn in place. | ||
Float vs. Brake mode |
NXT |
|
| See the difference between Float and Brake mode on a graph using motor A as the wheel moves and alternates between the two modes. | ||
Float vs. Brake mode (graph) |
NXT |
|
| This program will graph the distance traveled from a stopped condition at a constant power/speed level. | ||
Loops
Programs showing the same repeating behavior, using different kinds of loops.
While loop |
NXT |
|
| Run four laps around a square, using a while loop to continue running "while less than 20 sides of the square have been traveled". | ||
For loop |
NXT |
|
| Run four laps around a square, using a while loop to continue running "for 20 sides". | ||
Do-While loop |
NXT |
|
| Run four laps around a square, using a while loop to continue running "while less than 20 sides of the square have been traveled". This version uses a do-while loop structure, which is similar to a while loop, except that it checks the loop count at the end of each cycle instead of the beginning. | ||
Touch Sensor
Programs using the Touch Sensor to inform and guide robot behavior.
Wait for Push |
NXT |
|
| Travel forward until a Touch Sensor on Sensor Port 1 is pushed (assuming it starts "released"). | ||
Wait for Release |
NXT |
|
| Travel forward until a Touch Sensor on Sensor Port 1 is released (assuming it starts "pushed"). | ||
Remote Control |
NXT |
|
| Uses two Touch Sensors. Turn left if one touch sensor is pressed. Turn right if the other is pressed. Run forward if both are pressed at the same time. | ||
BugBot |
NXT |
|
| Uses two Touch Sensors positioned so that they are "pushed" by antenna-like extensions when the antenna collides with an object. Start off moving forward. If either Touch Sensor is "pushed" by a collision-detector antenna, back up and turn away from the side that was pressed, then resume movement. | ||
Sound Sensor
Programs using the Sound Sensor to inform and guide robot behavior.
Clap Off |
NXT |
|
| Move forward until a loud noise (such as a clap) is detected by a Sound Sensor on Sensor Port 1. Make sure that the Sound Sensor is positioned sufficiently far from the robot's motors, or the motor noise may accidentally trigger the stopping behavior. | ||
Light Sensor
Programs using the Light Sensor to inform and guide robot behavior.
Wait for Dark
|
NXT |
|
| Move forward until the Light Sensor on Sensor Port 1 detects a dark region (assuming it starts off seeing a light region). This is typically a downward-looking light sensor finding a piece of black tape on a light-colored table. | ||
Wait for Light
|
NXT |
|
| Move forward until the Light Sensor on Sensor Port 1 detects a light region (assuming it starts off seeing a dark region). | ||
Line Tracker
|
NXT |
|
Requires a Light Sensor on Port 1, facing down toward a dark-colored line on a light-colored surface. The robot will follow the line, using the following reasoning:
|
||
Timed Line Tracker
|
NXT |
|
Requires a Light Sensor on Port 1, facing down toward a dark-colored line on a light-colored surface. The robot will follow the line, using the following reasoning:
|
||
Ultrasonic (Sonar) Sensor
Programs using the Ultrasonic Sensor to inform and guide robot behavior.
Obstacle Detection |
NXT |
|
| Move forward until the Ultrasonic Sensor on Sensor Port 1 detects an object closer than 20cm in front of it. | ||
Rotation Sensor
Programs using the Rotation Sensors (built into the NXT motors) to inform and guide robot behavior.
Run Straight |
NXT |
|
| Move forward until both of the motors have turned at least 1800 degrees. The actual distance covered will vary with the size of your robot's wheels. | ||
Encoder Point Turn |
NXT |
|
| Turn until both of the motors have turned at least 1800 degrees in their respective directions. The actual amount turned will vary with the size of your robot's wheels and chassis. | ||
Line Track for Distance |
NXT |
|
| Requires a Light Sensor on Port 1, set up for line tracking (see Light Sensor). Run the line tracking behavior until the left motor has turned at least 1800 degrees. | ||
NXT Display
Programs demonstrating the use of the NXT's built-in LCD display.
Sonar Display |
NXT |
|
| Display the value of the Ultrasonic sensor on Sensor Port 1, once a second, on the NXT's LCD viewscreen. | ||
NXT Advanced Features
Programs demonstrating some of the advanced functionality that can be achieved with the NXT and RobotC.
Motor Acceleration Demo |
NXT |
|
| Program uses both “brake” and “float” mode to illustrate how ‘brake’ mode provides a more linear relationship between power level and actual speed. Two graphs are drawn on the NXT LCD screen for comparison purposes. | ||
Distance vs. Time Graph |
NXT |
|
| Program graphs distance traveled vs. time to show the linear relationship. The graph is drawn on the NXT LCD screen. | ||
Float vs. Brake Stopping |
NXT |
|
| Program demonstrates the difference between ‘float’ and ‘brake’ mode for holding a motor in position. | ||
Battery Demo |
NXT |
|
| Program shows how to read the current and average battery levels and display on the NXT LCD screen. | ||
Bluetooth Demo |
NXT |
|
| Program demonstrates how to access some Bluetooth functions from a user application program. | ||
NXT Buttons |
NXT |
|
| This program illustrates how to use the LEFT, RIGHT, ENTER and EXIT buttons within a user program. | ||
NXT Custom Menu |
NXT |
|
| This is a more advanced program for using the NXT front panel buttons within a program. It illustrates the use of a user created menu for item selection using the NXT buttons. | ||
Basic File Commands |
NXT |
|
| This program illustrates opening and closing, and reading and writing files on the NXT from within a user program. It also illustrates use of the “file delete” function. | ||
NXT File Search |
NXT |
|
| This program illustrates using wildcards (“*”) to perform a search through NXT files. | ||
Move Block Simulator |
NXT |
|
| This program contains a function that emulates the capabilities of the MOVE block in the NXT-G graphical programming language. | ||
Random |
NXT |
|
| This program illustrates use of the “random()” function. | ||
RPG Reader |
NXT |
|
| This is the source code for the NXT's main menus. This file is for reference only. | ||
Stalled Motor Check |
NXT |
|
| Checks to see whether motors have been stationary when they shouldn't be, indicating that they are "stalled" or "stuck". This functionality is implemented in an independent task, allowing it to be run alongside any existing program, which can use a single function to check at any time to see whether the motors are stuck. | ||
Songs |
NXT |
|
| This file illustrates use of the NXT sound handling capabilities to play three different songs on the NXT speaker. It illustrates the ability to queue sounds (RobotC supports up to 10 queued sound items) and how to test whether there is space available in the sound queue. | ||
RobotC Unique Features
Programs demonstrating some of RobotC's special features.
Execution Speed Test |
NXT |
|
| This program is used to measure the real-timer performance of the RobotC interpreter on the NXT. It measures the time to execute 10 different “opcodes’ on the NXT. The time to repetitively loop executing a single opcode is measured and the overhead of an empty loops is subtracted. The resultant time is normalized to get the number of microseconds to execute each of the ten opcodes. Results range from 2 to 50 microseconds. | ||
PID Motor Control |
NXT |
|
| Program demonstrates the use of built-in PID (Program, Integral and Derivative) algorithms for precise control over motor speed. | ||
Motor Synchronization |
NXT |
|
| Program demonstrates synchronization of two motors on the NXT. This is a way of creating reliable movement by forcing one motor to link its speed to another. In this example, the “slave” motor is set to turn at 50% of the speed of the "master" motor. The motors then switch roles and repeat. | ||
Precise Position Control |
NXT |
|
| Program demonstrates precise motor movement on the NXT. The motor is set to move precise 6-degree "ticks" over and over, once a second (like the seconds hand on a clock). There is no overshoot in the motor movement. | ||
Drawing and Speed |
NXT |
|
| Program illustrates the use of various NXT LCD drawing functions. It also times the execution time for the various functions. | ||
Trigonometry Demo |
NXT |
|
| This program illustrates use of the NXT motor encoder as a sensor input, floating point variables, NXT LCD display functions and trig functions. It uses the input from a motor encoder as an angle and then graphical draws this angle on the NXT LCD. The numeric sine and cosine values are also displayed on the screen. | ||
Spiral |
NXT |
|
| This program just loops forever drawing a spiral and then erasing it on the NXT LCD screen. It illustrates use of the set and clear pixel functions for the NXT display and also the ‘sin’ and ‘cos’ trigonometric functions. | ||
Multitasking Demo |
NXT |
|
| Program illustrates multiple tasks on the NXT. 5 tasks are started, and simply count the number of times each has executed. Task priority is used to ensure that all tasks start together. | ||
State Machine Template |
NXT |
|
| State machines are one convenient method for architecting the control of a robot. This program provides a generic template for writing a state machine. | ||
Large Fonts Demo |
NXT |
|
| Displays text in double-size font on the NXT screen, along with regular-sized text for comparison. | ||
3rd Party Sensor Support
Programs designed to demonstrate or support the use of 3rd party sensors with the NXT. Note that these sensors are sold by independent manufacturers and vendors, and not officially supported. They must be purchased separately from their respective manufacturers.
Compass Sensor |
NXT |
|
| This program demonstrates the use of a 3rd party compass sensor for the NXT. The compass heading is continuously obtained and displayed on the NXT LCD screen. The compass sensor is a separate piece of hardware and is not included in the standard NXT set. This file automatically #includes the NXT_Compass_Sensor_Driver.c program below. | ||
Compass Sensor Driver |
NXT |
|
| This program contains a device driver for the Hitechnic Compass Sensor. With only a few small changes it can also be used for the Mindsensors compass. Both sensors are separate hardware and not included in the standard NXT set. This file contains only the driver code to support operation of the sensor. For a sample of the sensor in use, see the Compass Sensor (not driver) program. | ||
MindSensors MUX |
NXT |
|
| This program demonstrates RobotC code to support the MindSensors motor multiplexor, a 3rd party device that runs up to four RCX-type motors off a single NXT Sensor Port and an external power source. | ||
PCF8754 I2C Test |
NXT |
|
| A driver and simple test for an interface with a PCF8754 I/O expander. This attachment allows communication with digital I/O devices. | ||