/**
   * Stops the robot when the ultrasonic sensor detects a certain distance.
   *
   * @param stopDistance as long as the current distance is less than this stop distance, the robot
   *     will keep moving. Once the current distance is larger than the stop distance, the function
   *     returns.
   * @return the current distance upon stopping.
   */
  private boolean stopBeforeEdge() {
    int currentDistance = 0;
    int lightLevelReading;
    int distanceToObject;
    boolean edgeIsSafe = false;

    do {
      lightLevelReading = lightSensorDown.readValue();

      Motor.A.rotate(centerToEdgeDistance, true);
      Motor.C.rotate(centerToEdgeDistance, true);
    } while ((lightLevelReading > BLACK_WHITE_THRESHOLD)
        && (Motor.A.isMoving() && Motor.C.isMoving()));

    pilot.stop();

    lightLevelReading = lightSensorDown.readValue();

    /*
    distanceToObject = ultrasonicSensor.getDistance();

    if(distanceToObject < 20)
    {
    	edgeIsSafe = true;
    	Motor.A.setSpeed(1000);
    	Motor.C.setSpeed(1000);
    	pilot.setRotateSpeed(1000);
    	pilot.travel(280);
    	Delay.msDelay(10000);
    	return(edgeIsSafe);
    }

    if(lightLevelReading < BLACK_WHITE_THRESHOLD)
    {
    	System.out.println("Unsafe edge");
    	edgeIsSafe = false;
    	return(edgeIsSafe);
    }
    else if (lightLevelReading > BLACK_WHITE_THRESHOLD)
    {
    	System.out.println("Safe edge");
    	edgeIsSafe = true;
    	safeEdgeCount++;
    	return(edgeIsSafe);
    }

    */

    return (edgeIsSafe);
  }
  /** Run. */
  public void run() {
    Motor.A.setSpeed(90);
    Motor.C.setSpeed(90);

    while (true) {
      moveForward();

      waitForReading();
      Motor.A.backward();
      Motor.C.backward();
      Delay.msDelay(1000);
      rotation(TURN_90_DEGREES, TURN_LEFT);
    }
  }
  /**
   * Main Entry Point. Creates the behaviors and starts the Arbitrator.
   *
   * @param args
   */
  public static void main(String[] args) {
    // Wall sensing behavior
    Behavior b1 = new WallBehavior(SensorPort.S3);
    // Move forward behavior
    Behavior b2 = new ForwardBehavior();
    // Left Light sensor sensing behavior
    Behavior b3 = new LeftLightBehavior(SensorPort.S2, SensorPort.S1);
    // Right Light sensor sensing behavior
    Behavior b4 = new RightLightBehavior(SensorPort.S2, SensorPort.S1);
    // Both Light sensors sensing behavior
    Behavior b5 = new BothLightsBehavior(SensorPort.S2, SensorPort.S1);
    // Win Behavior
    Behavior b6 = new WinBehavior(SensorPort.S2, SensorPort.S1);

    // Put the behaviors into the array based on priority
    Behavior[] behaviorArray = {b2, b1, b3, b4, b5, b6};

    // Create the arbitrator
    Arbitrator arby = new Arbitrator(behaviorArray);

    // Set the motor speeds
    Motor.A.setSpeed(800);
    Motor.C.setSpeed(800);

    // Start
    arby.start();
  }
  private int waitForReading() {
    int lightLevelReading;
    int distanceToObject;

    do {
      try {
        Thread.sleep(100);
      } catch (InterruptedException ex) {
      }

      lightLevelReading = lightSensorDown.readValue();
    } while (lightLevelReading > BLACK_WHITE_THRESHOLD);

    distanceToObject = ultrasonicSensor.getDistance();

    if (distanceToObject < 20) {
      Motor.A.setSpeed(1000);
      Motor.C.setSpeed(1000);
      pilot.travel(300);
      Delay.msDelay(10000);
    }

    return (lightLevelReading);
  }
 private void moveForward() {
   Motor.A.forward();
   Motor.C.forward();
 }