简体   繁体   中英

Arduino Sonar and Stepper Motor

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.

/*
   Nathan Verdonk
   3/15/2019
*/

#include <NewPing.h>
#include <Stepper.h>

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 < 50; i++){
    delay(50);

    val = sonar1.ping_cm();
    Serial.println(val);

    stepper1.step(1);
  }

  delay(3000);

}

if you want to trap the distance you could do this process to validate your sensor has no problem (i suppose wiring is right):

// defines pins numbers
const int triggerPin = 7;
const int echoPin = 6;
// defines variables
long duration;
int distance;
void setup() {
    pinMode(triggerPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
    Serial.begin(115200); // Starts the serial communication
}

void loop() {
    delay(50);
    // Clears the triggerPin
    digitalWrite(triggerPin, LOW);
    delayMicroseconds(2);
    // Sets the triggerPin on HIGH state for 10 micro seconds
    digitalWrite(triggerPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(triggerPin, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance= duration*0.034/2;
    // Prints the distance on the Serial Monitor
    Serial.print("Distance: ");
    Serial.println(distance);
}

In order to generate the ultrasound you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.

the speed of the sound is 340 m/s or 0.034 cm/µs so you divide by 2 to trap the distance

Problem is not with the code.

As it turns out, the sensor is picky about needing nearly exactly 5 V to run. With the servo and sensor using the same power source, the voltage would drop below 5 V when the servo was running.

Thank you to all who helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM