Activity 4.1: A Simple Sensor-Activated System

Program “start.nxc”:

// start.nxc - a very simple program for the NXT
task main()
{
SetSensorTouch(IN_1);
OnFwd(OUT_A, 75);
while(true)
{
until(SENSOR_1 == 1);
OnFwd(OUT_A, -75);
until(SENSOR_1 == 0);
until(SENSOR_1 == 1);
OnFwd(OUT_A, 75);
Until(SENSOR_1 == 0);
}
}

Description (should do):

The program “start.nxc” should start the motor in the forward direction and run it at 75% power, while connected to output port A. When the touch sensor, connected to input port 1, is activated the motor will change direction and begin to rotate in reverse.The program will continue indefinitely.

Description (what happened):

At first, the program didn’t function properly do to a case sensitive command that was entered incorrectly. After changing the “Until” to “until” the program worked as hypothesized.

Questions:

What happens when you touch the switch?

When you touch the switch the motor changes its direction.

What do you think the statement until (SENSOR_1 == 1); does?

This statement means that when the system is closed the motor changes reverse direction.

What do you think the punctuation marks ; mean?

This symbol makes the logic sentence a statement.

What do the back-to-back until statements accomplish?

It means that you must release the button before the next sequence of code can be engaged.

What about the curly braces { } ?

These contain the body of the program.

Why is the seconduntil statement until(SENSOR_1 == 0); needed?

This command resets the sensor to change the direction.

Activity 4.3: Tribot1

Program “tribot1.nxc”:

// tribot1.nxc - drive straight ahead
#define LEFT OUT_A
#define RIGHT OUT_C
#DEFINE STRAIGHT_TIME 5000
task main()
{
OnFwd(LEFT, 75); // note power may need to be adjusted
OnFwd(RIGHT, 75); // so that the motors drive straight
Wait(STRAIGHT_TIME);
Off(OUT_AC);
}

Description (should do):

The program “tribot1.nxc” should start the both the left and right motors in the forward direction at 75% power, while connected to output ports A and C. the program should move straight for 5 seconds and then the motors will stop and the code will terminate.

Description (what happened):

At first, the program didn’t function properly and the robot moved in 360 degrees. This is because only the left motor is working. After changing the “#DEFINE” to “#define” and fixing the parenthesis on the RIGHT motor the program worked as hypothesized.

Questions:

What is the effect of changing the values of the power level?

When the values for the power level are changed, the speed of the robots’ travel is affected. Lowering the value causes the robot to move slower, while raising the value causes it to move faster.

What happens when the power level is small but not zero?

This causes the robot to move very slow but does not negate its’ motion.

Activity 4.4:Tribot2

Program “tribot2.nxc”:

// tribot2.nxc - drive and turn
// motors
#define LEFT OUT_A
#define RIGHT OUT_C
// how much time to spend turning or forward
#define TURN_TIME 2000
#define STRAIGHT_TIME 1000
task main()
{
// start with both motors on
OnFwd(LEFT, 75); // note power may need to be adjusted
OnFwd(RIGHT, 75); // so that the motors drive straight
Wait(STRAIGHT_TIME);
// repeat the following steps forever
while(true)
{
// turn right by reversing the right wheel
OnRev(RIGHT, 75);
Wait(TURN_TIME);
// resume going straight
OnFwd(RIGHT, 75);
Wait(STRAIGHT_TIME);
// turn left by reversing the left wheel
OnRev(LEFT, 75);
Wait(TURN_TIME);
// resume going straight
OnFwd(LEFT, 75);
Wait(STRAIGHT_TIME);
}
}

Description (should do):

The program “tribot2.nxc” should start both motors moving in the forward direction for the defined “STRAIGHT_TIME”. After the appropriate time has been waited, the right motor will be changed to the reverse direction causing the robot to perform a tight turn to the right for the defined “TURN_TIME”. The program will then change the direction of the right motor back to forward and the robot will drive straight until it has achieved the designated “STRAIGHT_TIME”. After the appropriate time has been waited, the left motor will be changed to the reverse direction causing the robot to perform a tight turn to the left for the defined “TURN_TIME”.The program will then change the direction of the leftmotor back to forward and the robot will drive straight until it has achieved the designated “STRAIGHT_TIME”. This program will run indefinitely.

Description (what happened):

The program functioned properly and the robot moved in to the pattern described in the hypothesis. Changing the “TURN_TIME” and “STRAIGHT_TIME” caused the robot to either spin for a longer or shorter amount of time and travel straight for a longer or shorter amount of time. Changing the power level simply changed the speed of the motor.

General Questions:

What is the role of the curly braces { }?

The curly braces are used to close the code. The code is incorporated within them.

What does the statement Wait(5000); do?

This statement tells the program to perform the previous command for 5 seconds before engaging the next command.

What does the statement while(true) {....} do?

This statement tells the program to repeat the previous command indefinitely while conditions are true.