while (true) {
if (state == BOTH_ON)
OnFwd(OUT_A + OUT_C);
else if (state == LEFT_ON) {
Off(OUT_A);
OnFwd(OUT_C);
}
else if (state == RIGHT_ON) {
Off(OUT_C);
OnFwd(OUT_A);
}
}
A separate task, watcher, examines the light sensor values and sets the state variable. Here is the entire source code for the two sensor version of Trusty:
int state;
// "ON" refers to whether the light
//
sensor is on the line. If it is,
//
the light sensor is seeing black.
#define BOTH_ON 3
#define LEFT_ON 1
#define RIGHT_ON 2
#define BOTH_OFF 0
#define INDETERMINATE 255
// Thresholds for light and dark.
#define DARK2 35
#define LIGHT2 40
#define DARK3 40
#define LIGHT3 45
#define POWER 4
task main() {
initialize();
while (true) {
if (state == BOTH_ON)
OnFwd(OUT_A + OUT_C);
else if (state == LEFT_ON) {
Off(OUT_A);
OnFwd(OUT_C);
}
else if (state == RIGHT_ON) {
Off(OUT_C)
OnFwd(OUT_A);
}
}
}
sub initialize() {
SetSensor (SENSOR_2, SENSOR_LIGHT);
SetSensor (SENSOR_3, SENSOR_LIGHT);
Page 80
Need help?
Do you have a question about the MINDSTORMS Robots and is the answer not in the manual?
Questions and answers