EasyManuals Logo

LEGO MINDSTORMS Robots User Manual

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
Page #74 background imageLoading...
Page #74 background image
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

Questions and Answers:

Question and Answer IconNeed help?

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

LEGO MINDSTORMS Robots Specifications

General IconGeneral
Product LineLEGO MINDSTORMS
CategoryToy
Age Range10+
Batteries RequiredYes
Memory64 MB RAM, 16 MB Flash
ConnectivityBluetooth, USB
SensorsTouch, Color, Gyro
Battery TypeAA
Interactive FeaturesProgrammable, Remote Control
App CompatibilityiOS, Android
ProcessorARM9
Pieces Count601
Battery Count6
App NameLEGO MINDSTORMS EV3 Programmer App

Related product manuals