title: Arduino Ping))) Sensor suddenly stops after showing a few results
body: <p>I have a ping))) sensor (HC - SR04) which is connected correctly.</p>
  
  <p>I start it up without being connected to ground (otherwise it doesn't even want to start) and it keeps writing 0 to the serial window (distance). Once I plug it in the ground pin, I get a few lines of correct distance readings and than it stops and hangs, no more results in the serial window, and the board itself seems to be in a fault state, and I need to unplug it from the USB, disconnect the ground, and then re-plug to the USB.</p>
  
  <p>What can be the cause of the problem?</p>
  
  <p>Code:</p>
  
  <pre><code>#define echoPin 2 // Echo Pin
  #define trigPin 4 // Trigger Pin
  #define LEDPin 13 // Onboard LED
  
  int maximumRange = 200;  // Maximum range needed
  int minimumRange = 0;    // Minimum range needed
  long duration, distance; // Duration used to calculate distance
  int currentDistance = 0;
  
  void setup() {
      Serial.begin (9600);
      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
      pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)
  }
  
  void loop() {
      /* The following trigPin/echoPin cycle is used to determine the
         distance of the nearest object by bouncing soundwaves off of it. */
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
  
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
  
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
  
      //Calculate the distance (in cm) based on the speed of sound.
      distance = duration/58.2;
  
      if (distance &gt;= maximumRange || distance &lt;= minimumRange){
          /* Send a negative number to computer and Turn LED ON
             to indicate "out of range" */
          Serial.println("-1");
          digitalWrite(LEDPin, HIGH);
      } else {
          /* Send the distance to the computer using Serial protocol, and
             turn LED OFF to indicate successful reading. */
          Serial.println(distance);
          digitalWrite(LEDPin, LOW);
      }
  
      //Delay of 50 ms before next reading.
      delay(50);
  }
  </code></pre>
  
  <p>------------------------ UPDATE ------------------------------</p>
  
  <p>It would seem that the problem is not with the sensor, but with the serial interface: I have attached an <a href="http://en.wikipedia.org/wiki/Light-emitting_diode" rel="nofollow">LED</a> to the board and am giving it an analog value according to the distance. Once the Arduino gets "stuck", the LED keeps working correctly, so I guess the problem is with the Arduino shutting down the serial interface and stopping data transmission through the USB.</p>
  
  <p>How can this problem be fixed?</p>