LEGO MINDSTORMS Robots Manual page 74

Unofficial guide
Table of Contents

Advertisement

while (true) {
if (SENSOR_2 < DARK2)
OnFwd(OUT_A + OUT_C);
}
The DARK2 and POWER constants are determined using #defines; this means it's easy to fiddle with their values, and our program is easy to read.
The second task takes care of things when the light sensor leaves the black line. Whenever the robot leaves the line, the toggle() subroutine is called. toggle() starts the robot turning. Then
we wait a little while; if the robot is still not on the black line, we call toggle() again to turn back the other way:
task lightWatcher() {
while (true) {
if (SENSOR_2 > LIGHT2) {
toggle();
Wait(TIMEOUT);
if (SENSOR_2 > LIGHT2) {
toggle();
Wait(TIMEOUT ∗ 2);
}
}
}
}
The toggle() subroutine performs two important functions. First, it makes Trusty turn, based on the value of the state variable. Second, it updates the value of state; if it was RIGHT, it
will be LEFT, and vice versa.
Here is the whole program:
int state;
#define LEFT 0
#define RIGHT 1
#define DARK2 35
#define LIGHT2 40
#define POWER 7
#define TIMEOUT 50
task main() {
state = LEFT;
SetSensor(SENSOR_2, SENSOR_LIGHT);
SetPower(OUT_A + OUT_C, POWER);
start lightWatcher;
while (true) {
if (SENSOR_2 < DARK2)
OnFwd(OUT_A + OUT_C);
}
}
task lightWatcher() {
while (true) {
Page 78

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the MINDSTORMS Robots and is the answer not in the manual?

Questions and answers

Subscribe to Our Youtube Channel

Table of Contents