RobotC Sample Programs (NXT)


Motors
Sample programs using basic motor and timing commands with no sensors.

Moving Forward
Sample Programs/NXT/Motor/nxt_moving_forward.c
NXT
Make the robot run forward for 4 seconds, using motor power commands and Wait commands.

Forward then Reverse
Sample Programs/NXT/Motor/nxt_moving_forward_then_reverse.c
NXT
Make the robot run forward for 4 seconds then backward for 4 seconds, using motor power commands and Wait commands.

Modifiers
Sample Programs/NXT/Motor/nxt_modifiers.c
NXT
Make the robot run at various speeds by assigning different power settings to the motors.

Point Turns
Sample Programs/NXT/Motor/nxt_point_turns.c
NXT
Set the robot's motors to run in opposite directions to make the robot turn in place.

Float vs. Brake mode
Sample Programs/NXT/Motor Acceleration.c
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)
Sample Programs/NXT/Motor Distance vs Time.c
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
Sample Programs/NXT/Motor/nxt_loop_1_while.c
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
Sample Programs/NXT/Motor/nxt_loop_2_for.c
NXT
Run four laps around a square, using a while loop to continue running "for 20 sides".

Do-While loop
Sample Programs/NXT/Motor/nxt_loop_3_do_while.c
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
Sample Programs/NXT/Touch/nxt_wait_for_push.c
NXT
Travel forward until a Touch Sensor on Sensor Port 1 is pushed (assuming it starts "released").

Wait for Release
Sample Programs/NXT/Touch/nxt_wait_for_release.c
NXT
Travel forward until a Touch Sensor on Sensor Port 1 is released (assuming it starts "pushed").

Remote Control
Sample Programs/NXT/Touch/nxt_remote_control.c
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
Sample Programs/NXT/Touch/nxt_bug_bot.c
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
Sample Programs/NXT/Sound/nxt_clap_off.c
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
Sample Programs/NXT/Light/nxt_wait_for_dark.c
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
Sample Programs/NXT/Light/nxt_wait_for_light.c
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
Sample Programs/NXT/Light/nxt_line_track.c
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:

  • Aim for the left edge of the line
  • If the sensor sees dark, it means it's somewhere over the middle of the line, and should turn toward the left to get back to the left edge
  • If the sensor sees light, it means it's somewhere off the line to the left, so it should turn back to the right to get to the edge

Timed Line Tracker
Sample Programs/NXT/Light/nxt_line_track_timer.c
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:

  • Aim for the left edge of the line
  • If the sensor sees dark, it means it's somewhere over the middle of the line, and should turn toward the left to get back to the left edge
  • If the sensor sees light, it means it's somewhere off the line to the left, so it should turn back to the right to get to the edge
  • After each round of checking the sensor, check whether it's time to stop yet

Ultrasonic (Sonar) Sensor
Programs using the Ultrasonic Sensor to inform and guide robot behavior.

Obstacle Detection
Sample Programs/NXT/Sonar/nxt_obstacle_detect.c
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
Sample Programs/NXT/Rotation/nxt_straight.c
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
Sample Programs/NXT/Rotation/nxt_point_turns_enc.c
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
Sample Programs/NXT/Rotation/nxt_line_track_rotations.c
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
Sample Programs/NXT/Sonar/nxt_sonar_display.c
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
Sample Programs/NXT/Motor Acceleration.c
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
Sample Programs/NXT/Motor Distance vs Time.c
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
Sample Programs/NXT/Motor Float Mode Test.c
NXT
Program demonstrates the difference between ‘float’ and ‘brake’ mode for holding a motor in position.

Battery Demo
Sample Programs/NXT/NXT Battery Level.c
NXT
Program shows how to read the current and average battery levels and display on the NXT LCD screen. 

Bluetooth Demo
Sample Programs/NXT/NXT Bluetooth Test.c
NXT
Program demonstrates how to access some Bluetooth functions from a user application program.

NXT Buttons
Sample Programs/NXT/NXT Button.c
NXT
This program illustrates how to use the LEFT, RIGHT, ENTER and EXIT buttons within a user program.

NXT Custom Menu
Sample Programs/NXT/NXT Button Advanced.c
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
Sample Programs/NXT/NXT Test File IO.c
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
Sample Programs/NXT/NXT File Searching.c
NXT
This program illustrates using wildcards (“*”) to perform a search through NXT files.

Move Block Simulator
Sample Programs/NXT/NXT-G Move Block Basic.c
NXT
This program contains a function that emulates the capabilities of the MOVE block in the NXT-G graphical programming language.

Random
Sample Programs/NXT/RandomTest.c
NXT
This program illustrates use of the “random()” function.

RPG Reader
Sample Programs/NXT/RPGReader.c
NXT
This is the source code for the NXT's main menus. This file is for reference only.

Stalled Motor Check
Sample Programs/NXT/Stalled Motor Check.c
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
Sample Programs/NXT/NXT Songs.c
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
Sample Programs/NXT/Execution Speed Test.c
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
Sample Programs/NXT/Motor Speed PID.c
NXT
Program demonstrates the use of built-in PID (Program, Integral and Derivative) algorithms for precise control over motor speed.

Motor Synchronization
Sample Programs/NXT/NXT Motor Synchronization.c
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
Sample Programs/NXT/NXT Position Control.c
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
Sample Programs/NXT/NXTDisplaySpeedTest.c
NXT
Program illustrates the use of various NXT LCD drawing functions. It also times the execution time for the various functions.

Trigonometry Demo
Sample Programs/NXT/NXT Trig Demo.c
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
Sample Programs/NXT/NXT Draw Spiral.c
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
Sample Programs/NXT/NXT Test Task.c
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
Sample Programs/NXT/State Machine Template.c
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
Sample Programs/NXT/NXT Large Font.c
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
Sample Programs/NXT/NXT Compass Sensor.c
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
Sample Programs/NXT/NXT Compass Sensor Driver.c
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
Sample Programs/NXT/NXT Mindsensors Motor MUX.c
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
Sample Programs/NXT/PCF8754.c
NXT
A driver and simple test for an interface with a PCF8754 I/O expander. This attachment allows communication with digital I/O devices.