title: Arduino Sonar and Stepper Motor
body: <p>I am trying to create an Arduino powered sonar / radar.  I currently have a sonar sensor attached to a motor and working on the code.  The issue is with the for loop below.  The sensor will ping and the motor will then move, repeating the correct number of times.  However the values that the sonar sensor returns are either 0 or 1 no matter what the distance is. Any help on identifying the issue would be very appreciated.</p>
  
  <pre><code>/*
     Nathan Verdonk
     3/15/2019
  */
  
  #include &lt;NewPing.h&gt;
  #include &lt;Stepper.h&gt;
  
  const int stepsPerRevolution = 2048;                      // Steps per revolution
  const int rotSpeed = 10;                                  // Speed of rotation in RPM
  const int triggerPin = 7;                                 // Trigger pin on sonar sensor
  const int echoPin = 6;                                    // Echo pin on sonar sensor
  const int maxDistance = 300;                              // Max distance expected from sensor in cm; do not exceed 400
  
  int val;
  
  
  Stepper stepper1(stepsPerRevolution, 8, 10, 9, 11);           // initialize the stepper library on pins 8 through 11:
  NewPing sonar1(triggerPin, echoPin, maxDistance);             // initialize the new ping library with predefined values
  
  void setup() {
  
    stepper1.setSpeed(rotSpeed);
  
    Serial.begin(115200);
  
  }
  
  void loop() {
  
    for(int i = 0; i &lt; 50; i++){
      delay(50);
  
      val = sonar1.ping_cm();
      Serial.println(val);
  
      stepper1.step(1);
    }
  
    delay(3000);
  
  }
  </code></pre>