Answers.Body: <p>You can use the <a href="http://www.lejos.org/ev3/docs/lejos/robotics/RangeFinder.html" rel="nofollow"><code>lejos.ev3.robotics.RangeFinder</code></a> abstraction class:</p>
  
  <pre><code>EV3UltrasonicSensor sensorUS;
  RangeFinder sonar;
  
  public UltrasonicSensor(Port port) {
      sensorUS = new EV3UltrasonicSensor(port);
      sonar = new RangeFinder(sensorUS);
  }
  
  @Override
  public boolean takeControl() {
      return sonar.getRange() &lt; 0.3;
  }
  </code></pre>
  
  <p>Use the <code>float getRange()</code>  method to retrieve the distance to the nearest object.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-29T09:52:32.090Z
Answers.LastActivityDate: 2015-05-29T09:52:32.090Z
Answers.UserDisplayName: shea
Body: <p>I'm using ev3 ultrasonic sensor. And I'm writing a behavior based lejos code. I want to do some tasks when the ultrasonic sensor detects an object which is not further than 30 cm's. And this is my code:</p>
  
  <pre><code>public UltrasonicSensor(Port port)
      {
          sonar = new EV3UltrasonicSensor(port);
          sonar.getDistanceMode().fetchSample(value, 0);
      }
  
      @Override
      public boolean takeControl() {
          // TODO Auto-generated method stub
          return value[0] &lt; 0.3;
  
      public void action() {
          ....
      }
  
      @Override
      public void suppress() {
          // TODO Auto-generated method stub
          suppressed = true;
      }
  </code></pre>
  
  <p>But I think it is not the right way. How can I implement it in the right way?</p>
  
  <p>Thanks for help,</p>
  
Tags: <ev3><sensor-ultrasonic><lejos>
Title: usage of ev3 ultrasonic sensor in lejos behavior based programming
UserDisplayName: yusuf
ThreadText: # usage of ev3 ultrasonic sensor in lejos behavior based programming
  
  (Asked by: username_0 on 2015-05-28)
  
  I'm using ev3 ultrasonic sensor. And I'm writing a behavior based lejos code. I want to do some tasks when the ultrasonic sensor detects an object which is not further than 30 cm's. And this is my code:
  
  ```
  public UltrasonicSensor(Port port)
      {
          sonar = new EV3UltrasonicSensor(port);
          sonar.getDistanceMode().fetchSample(value, 0);
      }
  
      @Override
      public boolean takeControl() {
          // TODO Auto-generated method stub
          return value[0] < 0.3;
  
      public void action() {
          ....
      }
  
      @Override
      public void suppress() {
          // TODO Auto-generated method stub
          suppressed = true;
      }
  
  ```
  
  But I think it is not the right way. How can I implement it in the right way?
  
  Thanks for help,
  
  **Tags:** ev3, sensor-ultrasonic, lejos
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-05-29. Score: 1
  
  You can use the [`lejos.ev3.robotics.RangeFinder`](http://www.lejos.org/ev3/docs/lejos/robotics/RangeFinder.html) abstraction class:
  
  ```
  EV3UltrasonicSensor sensorUS;
  RangeFinder sonar;
  
  public UltrasonicSensor(Port port) {
      sensorUS = new EV3UltrasonicSensor(port);
      sonar = new RangeFinder(sensorUS);
  }
  
  @Override
  public boolean takeControl() {
      return sonar.getRange() < 0.3;
  }
  
  ```
  
  Use the `float getRange()` method to retrieve the distance to the nearest object.
  
  
  ---
  
  