可以一次弄兩個了
但是突然發現一個問題
HC SR04不是可以測2cm-450cm嗎?
怎麼我的測出來最高都到60cm左右而已?
以下是我的程式碼
----------------------------------------------------------------------------------------------------------------------------------
#include <Ultrasonic.h>
#define TRIGGER_PIN1 12
#define ECHO_PIN1 13
#define TRIGGER_PIN2 2
#define ECHO_PIN2 3
Ultrasonic ultrasonic1(TRIGGER_PIN1, ECHO_PIN1);
Ultrasonic ultrasonic2(TRIGGER_PIN2, ECHO_PIN2);
void setup()
{
Serial.begin(9600);
}
void loop()
{
float cmMsec1, cmMsec2;
long microsec1 = ultrasonic1.timing();
long microsec2 = ultrasonic2.timing();
cmMsec1 = ultrasonic1.convert(microsec1,
Ultrasonic::CM); // 計算距離,單位: 公分
cmMsec2 = ultrasonic2.convert(microsec2,
Ultrasonic::CM);
Serial.print("1MS: ");
Serial.print(microsec1);
Serial.print(", CM: ");
Serial.println(cmMsec1);
Serial.print("2MS: ");
Serial.print(microsec2);
Serial.print(", CM: ");
Serial.println(cmMsec2);
Serial.println();
delay(1000);
}
|