Answers.Body: <p>It's a little unclear from your question where this is currently failing, but I do see a couple issues with your code.</p>
  
  <p>1. Your <code>if</code> blocks are probably not doing what you expect. You should include braces to conditionally run the block following an <code>if</code> statement. As written, all of your code executes all of the time. This is a tricky bug to spot, as it looks correct, but isn't. You probably want to change this:</p>
  
  <pre><code>if (us1.getDistance() &gt;= 25);
      Motor.A.setSpeed(50);
      Motor.C.setSpeed(50);
      Motor.A.forward();
      Motor.C.forward();
  </code></pre>
  
  <p>To this:</p>
  
  <pre><code>if (us1.getDistance() &gt;= 25) {
      Motor.A.setSpeed(50);
      Motor.C.setSpeed(50);
      Motor.A.forward();
      Motor.C.forward();
  }
  </code></pre>
  
  <p>2. You say that you want the motors to stop, but it looks like you are actually reversing them. You'll likely want to change this:</p>
  
  <pre><code>Motor.A.backward();
  Motor.C.backward();
  </code></pre>
  
  <p>To this:</p>
  
  <pre><code>Motor.A.stop();
  Motor.C.stop();
  </code></pre>
  
  <p>Your code may still need some tweaking, but hopefully those two fixes get you back on track.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-28T16:58:16.963Z
Answers.Text: I suspect the OP is more familiar with Python, hence the mistakes in the if-syntax.....
Answers.UserDisplayName: Phil B.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-28T16:08:43.123Z
Answers.LastActivityDate: 2018-03-28T16:08:43.123Z
Answers.UserDisplayName: jncraton
Answers.Body: <p>Second <code>if</code> needs to be replaced by a loop... Very unlikely that the distance will be immediately less then 25 cm after starting the motors, you need to continuously check...
  And stopping the motors can then be done unconditionally (i.e. outside the while loop)
  Something like</p>
  
  <pre><code>while (us1.getDistance() &gt;= 25 &amp;&amp; !Button.ESCAPE.IsDown())
  {
     Thread.sleep (1000);
  }
  Motor.A.stop();
  Motor.C.stop();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-28T17:35:10.407Z
Answers.LastActivityDate: 2018-03-28T17:35:10.407Z
Answers.UserDisplayName: Michael Verschaeve
Body: <p>I've been struggling for some time on this. I'm trying to code the robot to move when the distance is above a certain integer and stop when the distance becomes lower than the certain integer. I'm coding in Eclipse Helios. My code is below:</p>
  
  <pre><code>import lejos.nxt.Button; 
  import lejos.nxt.Motor;
  import lejos.nxt.SensorPort;
  import lejos.nxt.UltrasonicSensor;
  
  public class robot {
      public static void main(String[] args) {
          UltrasonicSensor us1 = new UltrasonicSensor(SensorPort.S4);
  
          System.out.print(us1.getDistance());
  
          if (us1.getDistance() &gt;= 25);
              Motor.A.setSpeed(50);
              Motor.C.setSpeed(50);
              Motor.A.forward();
              Motor.C.forward();
  
          if (us1.getDistance() &lt; 25);
              Motor.A.backward();
              Motor.C.backward();
              Button.waitForAnyPress();
      }
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-03-28T15:59:44.710Z
Comments.Text: Welcome to Bricks.SE! How does this behave currently? What changes to your program have you tried?
Comments.UserDisplayName: jncraton
Tags: <mindstorms><nxt><sensor-ultrasonic>
Title: How to use the ultrasonic sensor on mindstorm NXT
UserDisplayName: Matt Sutton
ThreadText: # How to use the ultrasonic sensor on mindstorm NXT
  
  (Asked by: username_0 on 2018-03-28)
  
  I've been struggling for some time on this. I'm trying to code the robot to move when the distance is above a certain integer and stop when the distance becomes lower than the certain integer. I'm coding in Eclipse Helios. My code is below:
  import lejos.nxt.Button; 
  import lejos.nxt.Motor;
  import lejos.nxt.SensorPort;
  import lejos.nxt.UltrasonicSensor;
  
  public class robot {
      public static void main(String[] args) {
          UltrasonicSensor us1 = new UltrasonicSensor(SensorPort.S4);
  
          System.out.print(us1.getDistance());
  
          if (us1.getDistance() >= 25);
              Motor.A.setSpeed(50);
              Motor.C.setSpeed(50);
              Motor.A.forward();
              Motor.C.forward();
  
          if (us1.getDistance() < 25);
              Motor.A.backward();
              Motor.C.backward();
              Button.waitForAnyPress();
      }
  }
  
  **Tags:** mindstorms, nxt, sensor-ultrasonic
  > **Comments:**
  > **username_1**: Welcome to Bricks.SE! How does this behave currently? What changes to your program have you tried?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-03-28. Score: 4
  
  It's a little unclear from your question where this is currently failing, but I do see a couple issues with your code.
  1. Your if blocks are probably not doing what you expect. You should include braces to conditionally run the block following an if statement. As written, all of your code executes all of the time. This is a tricky bug to spot, as it looks correct, but isn't. You probably want to change this:
  if (us1.getDistance() >= 25);
      Motor.A.setSpeed(50);
      Motor.C.setSpeed(50);
      Motor.A.forward();
      Motor.C.forward();
  
  To this:
  if (us1.getDistance() >= 25) {
      Motor.A.setSpeed(50);
      Motor.C.setSpeed(50);
      Motor.A.forward();
      Motor.C.forward();
  }
  
  2. You say that you want the motors to stop, but it looks like you are actually reversing them. You'll likely want to change this:
  Motor.A.backward();
  Motor.C.backward();
  
  To this:
  Motor.A.stop();
  Motor.C.stop();
  
  Your code may still need some tweaking, but hopefully those two fixes get you back on track.
  
  > **Comments:**
  > **username_2**: I suspect the OP is more familiar with Python, hence the mistakes in the if-syntax.....  
  
  ---
  
  ### Answer by username_3 on 2018-03-28. Score: 1
  
  Second if needs to be replaced by a loop... Very unlikely that the distance will be immediately less then 25 cm after starting the motors, you need to continuously check...
  And stopping the motors can then be done unconditionally (i.e. outside the while loop)
  Something like
  while (us1.getDistance() >= 25 && !Button.ESCAPE.IsDown())
  {
     Thread.sleep (1000);
  }
  Motor.A.stop();
  Motor.C.stop();
  
  
  ---
  
  