0

我有一个正确连接的 ping))) 传感器 (HC - SR04)。

我在没有接地的情况下启动它(否则它甚至不想启动)并且它一直将 0 写入串行窗口(距离)。一旦我将它插入接地引脚,我会得到几行正确的距离读数,然后它会停止并挂起,串行窗口中没有更多结果,并且电路板本身似乎处于故障状态,我需要拔掉插头将其从 USB 断开,断开接地,然后重新插入 USB。

问题的原因可能是什么?

代码:

#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 >= maximumRange || distance <= 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);
}

- - - - - - - - - - - - 更新 - - - - - - - - - - - - - -----

似乎问题不在于传感器,而在于串行接口:我在板上连接了一个LED,并根据距离给它一个模拟值。一旦 Arduino 被“卡住”,LED 就会继续正常工作,所以我猜问题是 Arduino 关闭了串行接口并停止通过 USB 传输数据。

如何解决这个问题?

4

3 回答 3

1

传感器卡在零位的解决方案在此链接中。这是 docdoc 的第 2 篇文章。您将需要使用更好的 NewPing 库。

工作代码:

#include <NewPing.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
    Serial.begin(9600);
}

void loop() {
    delay(50);
    unsigned int uS = sonar.ping();
    pinMode(ECHO_PIN,OUTPUT);
    digitalWrite(ECHO_PIN,LOW);
    pinMode(ECHO_PIN,INPUT);
    Serial.print("Ping: ");
    Serial.print(uS / US_ROUNDTRIP_CM);
    Serial.println("cm");
}

链接: http: //forum.arduino.cc/index.php?topic=55119.15

NewPing 链接:http ://playground.arduino.cc/Code/NewPing

于 2015-11-22T15:05:29.230 回答
0

I was able to find the cause of the problem. It seems that the Arduino's serial monitor was causing the "crash", because while using a server utility that allows the Arduino output to be used in Flash (serproxy) the Arduino performs consistently and without problems.

A video of the project is Arduino + Ping))) Sensor - Change display in application according to distance.

于 2013-07-07T12:21:10.833 回答
0

从你说的情况来看,地面和设备之间似乎有一条间接的捷径......或者可能有一个有故障的组件捷径到地面。所以我的建议是:

  • Arduino 工作正常吗?
    • 在没有连接传感器的情况下测试 Arduino 本身是否完美工作(上传回声草图),
  • 传感器设备是否正常工作?
    • 测试所有焊接点的连续性(寻找接地和信号之间的捷径桥),
    • 使用另一个传感器设备进行测试(如果您碰巧有备用设备)以查看问题是否仍然存在
    • 或者用另一个 Arduino 尝试相同的设备。

关于您的代码本身,据我所知,这不是问题的根源,我建议您将全局常量的声明更改为:

const uint8_t maximumRange = 200; // Maximum range needed
const uint8_t minimumRange = 0;   // Minimum range needed
const uint8_t currentDistance = 0;

或者使用预处理器:

#define maximumRange 200  // Maximum range needed
#define minimumRange 0    // Minimum range needed
#define currentDistance = 0

并移动:

long duration, distance;   // Duration used to calculate distance

在你的loop()函数里面。尽可能避免非常量的全局变量总是一个好主意(尽管对于 Arduino 对象并不总是可行)。

于 2013-06-20T16:37:33.413 回答