public DistanceMeasure() {
    ultrasonicSensor = new UltrasonicSensor(SensorPort.S1);

    while (true) {
      System.out.println("Distance: " + ultrasonicSensor.getDistance());
    }
  }
  /**
   * Reads data from the ultrasonic sensor and filters it to prevent false 255 values
   *
   * @return The filtered data
   */
  private int getFilteredData() {
    int distance;

    // do a ping
    us.ping();

    // wait for the ping to complete
    try {
      Thread.sleep(50);
    } catch (InterruptedException e) {
    }

    // there will be a delay here
    distance = us.getDistance();

    if (minDistance > distance) {
      minDistance = distance;
    }

    if (distance > 55) {
      distance = 55;
    }

    if (distance > 255) {
      distance = 255;
    }

    return distance;
  }
 /** The robot moves along the Wall and turns to the right, if the wall is too far away. */
 @Override
 public void action() {
   suppressed = false;
   Settings.motorAAngle = Settings.SENSOR_RIGHT;
   while (!suppressed
       && !((Settings.TOUCH_L.isPressed() || Settings.TOUCH_R.isPressed()))
       && Settings.LIGHT.getLightValue() < 80) {
     if (sonic.getDistance() > (distanceToWall + 10)) {
       pilot.arc(-15, -90, true);
     } else if (sonic.getDistance() <= distanceToWall) {
       pilot.arc(40, 20, true);
     } else if (sonic.getDistance() > distanceToWall) {
       pilot.arc(-60, -20, true);
     }
   }
   if (Settings.LIGHT.getLightValue() >= 80) {
     pilot.stop();
     System.out.println("LINE Detected");
     Settings.afterSlider = true;
     Settings.motorAAngle = Settings.SENSOR_FRONT;
     Settings.PILOT.setTravelSpeed(Settings.PILOT.getMaxTravelSpeed() * 0.15);
   }
 }
Exemple #4
0
  public static void main(String[] args) {
    LightSensor light = new LightSensor(SensorPort.S1);
    SoundSensor sound = new SoundSensor(SensorPort.S2);
    TouchSensor touch = new TouchSensor(SensorPort.S3);
    UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S4);

    while (sound.readValue() < 90) {
      System.out.println("light = " + light.readValue());
      System.out.println("sound = " + sound.readValue());
      System.out.println("touch = " + touch.isPressed());
      System.out.println("distance = " + sonic.getDistance());
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ie) {
      }
    }
  }
Exemple #5
0
  public static void Sonarflag() {
    // execute all actions of this state
    LCD.drawString("Sonarflag", 0, 3);
    Sound.beep();
    btThread.write(sonar.getDistance()); // sends the data from the sonar

    // leg de huidige tijd vast voor alle transitions met een timeoutcondition
    long starttime = System.currentTimeMillis();

    // when done, wait for a trigger for a transition
    boolean transitionTaken = false;
    while (!transitionTaken) {
      if ((true)) {
        current = State.WATCH;
        transitionTaken = true;
      }
    }
  }
Exemple #6
0
  public static void Watch() {
    // execute all actions of this state
    LCD.drawString("Watch", 0, 3);

    // leg de huidige tijd vast voor alle transitions met een timeoutcondition
    long starttime = System.currentTimeMillis();

    // when done, wait for a trigger for a transition
    boolean transitionTaken = false;
    while (!transitionTaken) {
      if ((sonar.getDistance() <= 10)) {
        current = State.SONARFLAG;
        transitionTaken = true;
      } else if ((lejos.robotics.Color.RED == colorsens.getColorID())
          || (lejos.robotics.Color.BLUE == colorsens.getColorID())
          || (lejos.robotics.Color.GREEN == colorsens.getColorID())) {
        current = State.COLORFLAG;
        transitionTaken = true;
      }
    }
  }
  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);
  }
Exemple #8
0
 public boolean takeControl() {
   int distance = sonar.getDistance();
   return (distance > floorDistance);
 }
Exemple #9
0
 public void action() {
   LCD.clear();
   LCD.drawInt(sonar.getDistance(), 0, 0);
   LCD.refresh();
 }