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.
The two-button remote control is a very versatile program for controlling
Lego robots. It uses a series of touch sensor Switches to determine which
behavior you want to trigger. Since the Switches are between a set of Loops, the program loops infinitely, and the behavior of the robot
will change every time a different touch sensor is hit.
In this program, the two-button remote is setup to control a car
with motors connected to Ports A and C. When only touch sensor 1 is pressed, Motor A spins forward and Motor C spins backward. This will turn
the car left or right. When only touch sensor 2 is pressed, the
motors are reversed and the car will spin the opposite direction.
If both touch sensors are held, the motors go forward. When no touch
sensors are held, the car does not move. Remember, each branch of
the Switch can control something different. It does not only work
for a car. The touch sensors can trigger music, events, even mail
to other robots. The possibilities are endless.
In this code, sound frequencies are played corresponding to changing
light values. Light sensor readings, which fall between 0-100, are
placed in a variable and multiplied by the constant 8. The value
of that variable, which now falls between 0-800, becomes the frequency
in Hz of the sound played. The frequency is also displayed on the NXT.
This is another version of the two-button remote.
The advanced proportional control program works the same way as
the basic version; however, this code allows the user to vary the
proportionality constant. When run, the NXT first displays the current
proportionality constant that the later "power" variable will be divided
by. Thus, if 4 is shown on the NXT, the proportionality constant will
be ? or .25. The constant can be changed by turning the rotation
sensor. Only positive values will work. After the constant is set,
a press of the touch sensor will "Switch" into the proportional control loop from the basic program.
Experiment with different constants. Note the difference between very
small constants and very large constants.
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.
Like its smaller cousin, the three-button remote control is a
great way to define unique behaviors for your robot. The advantage
to the three-button control is that there are now 8 possible behavior
combinations, compared to just 4 on the 2-button remote. In addition
to forward, left and right motion on our two motor car, it is now
possible to move in reverse, and even control a third motor. With
so many combinations, it is possible that you don?t need them
all. In that case just leave one branch of the Switch empty.
This program uses two touch sensors combined with two Switches--all in an infinite loop--to display a number on the screen. Port 1 adds 1 and Port 2 adds 100. The "adder" variable is set to zero at the beginning of this program.
The NXT can also be used to play music. By placing notes manually custom songs
can be created.