EasyManua.ls Logo

LEGO MINDSTORMS Robots

LEGO MINDSTORMS Robots
226 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
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.
Page 78
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) {

Table of Contents

Related product manuals