Encoder Programming Guide (ROBOTC) Encoder: Tetrix Add-On Extensions
Encoder Programming Guide (ROBOTC) Encoder: Tetrix Add-On Extensions
Getting Started:
1. To start the program, type the Main Task function followed by an opening brace.
task main()
{
2. Declare six integer variables as seen in the code below. Set them to the values shown. After each variable there are two
slashes (//) followed by what that variable is representing. These mark a comment. Notice that comments appear in green
when code is typed into ROBOTC.
Note: A comment is not part of the code that the NXT Brick will execute, but it helps the programmer by allowing the
addition of notes to describe what has been done.
int object1Dist;
int object2Dist;
4. Add a While Loop that executes while the Ultrasonic Sensor reading is greater than the stopDist variable (set to 30 cm). Set
both DC motors power to the fwdPower value while this condition remains True. This will make the robot move forward in a
straight line towards the first object.
while(SensorValue(SonarSensor) > stopDist)
{
motor[motorD]= fwdPower;
motor[motorE]= fwdPower;
}
135
Encoder
// Stop motors and make the robot wait for half a second
motor[motorE] = 0;
wait1Msec(500);
6. The code below reads the current output value of the encoder sensor. This value is the number of encoder counts that
make up the distance from the robots starting point to the point 30 cm from the first object. The program needs to remember this value for later on in the program, so it writes it to the variable object1Dist.
object1Dist = nMotorEncoder[motorD];
7. The motor encoder needs to be set to zero again to read it correctly in the next step.
nMotorEncoder[motorD] = 0;
8. Add a While Loop that executes while the current encoder value is below the variable turnDist. This variable is an encoder
count that is equal to the distance that a wheel rotates during a 90-degree turn. Set the DC motors power to the turnPower
variable while this condition remains True; however, the right motor will be set to the negative value of turnPower. This will
make the robot turn on the spot.
Note: You must adjust the turnDist value so the robot turns exactly 90 degrees. The preset value of 1380 shown in Step 2 is
a good starting value; however, the robot turns different amounts on different ground surfaces. The value will probably
have to be adjusted for a specific surface.
while (nMotorEncoder[motorD] < turnDist)
{
motor[motorD] = turnPower;
motor[motorE] = -turnPower;
}
9. As was done after the first While Loop, turn off the motors and make the robot wait for half a second. These three lines of
code will be repeated after every While Loop.
motor[motorD] = 0;
motor[motorE] = 0;
wait1Msec(500);
10. Now the robot is ready to search for the second object. First, the motor encoder needs to be reset to zero.
nMotorEncoder[motorD] = 0;
136
Encoder
{
motor[motorD] = fwdPower;
motor[motorE] = fwdPower;
}
motor[motorD] = 0;
motor[motorE] = 0;
wait1Msec(500);
12. Similar to Step 6, the distance from the first object to the second object must be remembered and written to the variable
object2Dist. After storing the encoder value, reset the encoder to zero.
object2Dist = nMotorEncoder[motorD];
nMotorEncoder[motorD] = 0;
13. Make the robot turn again using the same turning loop as in Step 8. Then, turn off the motors and make the robot wait for
half a second.
while (nMotorEncoder[motorD] < turnDist)
{
motor[motorD] = turnPower;
motor[motorE] = -turnPower;
}
motor[motorD] = 0;
motor[motorE] = 0;
wait1Msec(500);
14. The robot will now begin traveling back to its starting point. First, reset the motor encoder to zero.
// Begins to travel back to the start
nMotorEncoder[motorD] = 0;
15. Add a While Loop that drives the robot straight for the distance of the encoder count value stored in the variable
object1Dist. This means the robot will travel the same distance as it did from its starting point to the first object. After the
loop finishes, turn off the motors and make the robot wait for half a second.
while (nMotorEncoder[motorD] < object1Dist)
{
137
Encoder
138
Encoder
Completed Code:
task main()
{
int object1Dist;
int object2Dist;
nMotorEncoder[motorD] = 0;
while (SensorValue[SonarSensor] > stopDist) // Moves forward until within 30 cm (stopDist) of first
object
{
motor[motorD] = fwdPower;
motor[motorE] = fwdPower;
}
motor[motorD] = 0;
// Stop motors and make the robot wait for half a
second
motor[motorE] = 0;
wait1Msec(500);
object1Dist = nMotorEncoder[motorD];
nMotorEncoder[motorD] = 0;
while (nMotorEncoder[motorD] < turnDist)
{
motor[motorD] = turnPower;
motor[motorE] = -turnPower;
}
motor[motorD] = 0;
motor[motorE] = 0;
wait1Msec(500);
139
Encoder
nMotorEncoder[motorD] = 0;
while (SensorValue[SonarSensor] > stopDist) // Moves forward until within 30 cm (stopDist) of
second object
{
motor[motorD] = fwdPower;
motor[motorE] = fwdPower;
}
motor[motorD] = 0;
motor[motorE] = 0;
wait1Msec(500);
object2Dist = nMotorEncoder[motorD];
nMotorEncoder[motorD] = 0;
while (nMotorEncoder[motorD] < turnDist)
{
motor[motorD] = turnPower;
motor[motorE] = -turnPower;
}
motor[motorD] = 0;
motor[motorE] = 0;
wait1Msec(500);
nMotorEncoder[motorD] = 0;
while (nMotorEncoder[motorD] < object1Dist)
{
motor[motorD] = fwdPower;
motor[motorE] = fwdPower;
}
motor[motorD] = 0;
motor[motorE] = 0;
wait1Msec(500);
140
Encoder
nMotorEncoder[motorD] = 0;
while (nMotorEncoder[motorD] < object2Dist)
{
motor[motorD] = fwdPower;
motor[motorE] = fwdPower;
}
motor[motorD] = 0;
motor[motorE] = 0;
}
141