FSHS Robotics

Back to Outline

FSHS Robotics C Course

NXT: Simple Motor Movement

Set up your motors by going to Robot > Motors and Sensors Setup and adding the two motors as described here. Make sure you name it what you’ll put in the square brackets!

To start the motors:

motor[motorL] = 100; // Sets speed of motorL to max speed

To reverse the motors, set it to a negative value:

motor[motorL] = -100; // Sets speed of motorL to max backwards speed

Note that this sets the motor speed. It does not do anything else. The most common error is this:

if (x > 0) {
    motor[motorL] = 100;
}
else if (x < 0) {
    motor[motorL] = -100;
}
else {
    // No motor speed
}

When x is zero, the speed will remain the same as its previous state, and will not stop. To make it stop when x is 0, you must set the motor speed to 0:

else {
    motor[motorL] = 0;
}

Using wait

The wait command allows you to pause a program for a set number of milliseconds.

You can let the motor keep going at a speed for a certain period of time, and stop it after a period of time. For example, the following full program will set the motor speed to 100, wait for 3 seconds, then stop the motor.

#pragma config(Motor, motorA, motorL, tmotorNXT, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
task main()
{
    motor[motorL] = -100; // SETS (not pings) motor speed
    wait1Msec(3000); // Keeps running at -100 for 3 seconds
    motor[motorL] = 0; // SETS speed to 0
}

Exercises

  1. Make the motor turn max speed for 1 second, then reversed max speed for 1 second, and repeat.

  2. Make the motor turn at a speed proportional to the light value.

© Fort Street High School Robotics