1

センサーから情報を受信しようとしていますが、出力は常に 0 です。コードに何か問題がありますか? ハードウェア関連はすべてうまくいっています。

loop()
{
    long duration, inches, cm;

    pinMode(pingPin, OUTPUT);
    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin, LOW);

    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);

    inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);

    Serial.print(inches);
    Serial.print("in; ");
    Serial.print(cm);
    Serial.print("cm");
    Serial.println();
}

long microsecondsToInches(long microseconds)
{
    return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
    return microseconds / 29 / 2;
}
4

3 に答える 3

2

あなたが持っているセンサーは、距離を送信する方法として PWM を使用しません。むしろ、シリアル接続を使用します。問題は、Arduino に追加のシリアル ハードウェアがないことです。

arduino の serail ポートを使用してセンサー データを読み取ることはできますが、画面に何も記録することはできません。

シリアル接続の実行速度は 9600 ボーで、ソフトウェアでエミュレートするには速すぎます。

標準の PWM モードの通信を使用するセンサーを購入することをお勧めします。これを行うと、いくつかの頭痛の種が解消されます。しかし、方法があることをお伝えしておきます。ソフトウェアシリアルライブラリを使用しています。ライブラリは、シリアル ピンのようにデジタル ピンを使用するのに役立ちます。

http://arduino.cc/en/Reference/SoftwareSerial

http://www.suntekstore.com/goods-14002212-3-pin_ultrasonic_sensor_distance_measuring_module.html

http://iw.suntekstore.com/attach.php?id=14002212&img=14002212.doc

于 2013-07-03T13:18:34.850 に答える