This code runs motors B and C until they stall.
The code takes two rotation sensor readings, with a very short delay in
between them. The loop exits when the difference between the two readings
becomes less than a certain threshold. So if a motor slows down because
it encounters an obstacle, the code will stop running the motors. A short delay in
the beginning is necessary so that the motor can start moving before the rotation sensors start taking readings. Note that if the speed of the motor is decreased, the threshold for the difference needs to be lowered as well. This program was adapted from
Brian Davis' code.
This code creates a wait for loop that will wait for a random amount of time. The code uses the random number generator and a loop that waits for the timer to equal the generated value. The random number is set between 500 and 5000, which causes the loop to wait for 500 and 5000 milliseconds. Code by Brian Davis.
This code illustrates the Wait-for-Push command. When a touch sensor
on Port 1 is pressed, Motor A will stop.
This code uses two very important coding commands, Switches and Loops.
The touch sensor Switch causes Motor A to go forward if the touch sensor
is released and backward if it is pressed. The Loop causes the Switch
to loop infinitely.
When run, this code will cause a car to "snake" forward in wide arcs.
The length of the turns can be modified by changing either the time or motor power.
The code will run Motor A in a random direction (via a Random-Comparison Block combination), at a random
speed, for a random amount of time. The motor speeds will fall between
0-100 and the time will fall between 0-3 seconds.
If a car is programmed to travel 12?, how do we know is actually
traveled that distance? Often, the cars momentum will carry it past
the desired location even though the motors have stopped running.
The solution to this problem is a proportional control loop. By continually
monitoring the distance from the desired location, the motor speed
can be slowed as the car approaches. The governing formula is defined
as P*(desired-current) where P is an arbitrary constant.
In this program, a desired location is set at 50 degrees of the rotation
sensor. This value is continually subtracted by the current rotation traveled.
The difference is multiplied by two and this becomes the value of the "power" variable. The Compare Block determines the direction Motor C will travel: forward if "power" is positive, backward if "power" is negative. Thus, when the car
first starts off, its current position is zero and its desired position
is 50. Therefore, the power setting used is 100, which is full power forward. If the car overshoots its mark, the power
setting will become negative and the car will travel in reverse.
The goal of proportional control is to find a constant that will
get the system to equilibrium in the shortest time with the least
amount overshoot and the most accuracy.
The NXT can also be used to play music. By placing notes manually custom songs
can be created.
This program beeps, runs Lurch-1, then beeps again.
This code uses a Loop to turn Motor A on-and-off three times. When
downloaded to a single motor car, this program will accelerate the
vehicle forward in three short bursts. The number of loops can be
changed by modifying the Count of "3". Alternatively, if
you want an infinite program, you can set the Control of the Loop to Forever.