// linebot_modified.nqc // line tracking tracking algorithm // sensor and motors #define EYE SENSOR_2 #define LEFT OUT_A #define RIGHT OUT_C // constants #define LINE_THRESHOLD 36 #define TURN_SPEED 7 #define INITIAL_TIME 4 int direction, time, eye; task main() { SetSensor(EYE, SENSOR_LIGHT); // initialize the variables direction = 1; time = INITIAL_TIME; // start driving OnFwd(LEFT+RIGHT); while(true) { // read the sensor value eye = EYE; if (eye > LINE_THRESHOLD) { ClearTimer(0); if (direction == 1) { SetPower(RIGHT+LEFT, TURN_SPEED); Rev(LEFT); } else { SetPower(RIGHT+LEFT, TURN_SPEED); Rev(RIGHT); } while(true) { // have we found the line? if (EYE < LINE_THRESHOLD) { time = INITIAL_TIME; break; } if (Timer(0) > time) { // try the other direction direction = -direction; time = 2*time; break; } } SetPower(RIGHT+LEFT, OUT_FULL); Fwd(RIGHT+LEFT); } } }