StopInGoal.java
import lejos.nxt.*;
/**
 * A simple line follower for the LEGO 9797 car with
 * a light sensor. Before the car is started on a line
 * a BlackWhiteSensor is calibrated to adapt to different
 * light conditions and colors.
 * 
 * The light sensor should be connected to port 3. The
 * left motor should be connected to port C and the right 
 * motor to port B.
 * 
 * @author  Ole Caprani
 * @version 23.08.07
 */
public class StopInGoal
{
  public static void main (String[] aArg) throws Exception {
     final int power = 80;
     final int consMeas = 20;
 
     ColorSensor sensor = new ColorSensor(SensorPort.S1);
 
     sensor.calibrate();
     int count = 0;
 
     LCD.clear();
     LCD.drawString("Light: ", 0, 2); 
 
     Thread.sleep(500);
 
     while (! Button.ENTER.isPressed());
 
     while (! Button.ESCAPE.isPressed()) {
 
         LCD.drawInt(sensor.light(),4,10,2);
         LCD.refresh();
 
         if ( sensor.black() ) {
             Car.forward(power, 0);
             count = 0;
         } else if ( sensor.white() ) {
             Car.forward(0, power);
             count = 0;
         } else if ( sensor.blue() ) {
             count++;
             if (count > consMeas) {
                 count = 0;
                 break;
             }
         }
         Thread.sleep(10);
     }
 
     Car.stop();
     LCD.clear();
     LCD.drawString("Program stopped", 0, 0);
     LCD.refresh();
   }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License