我想使用 Arduino 和两个超声波 hc-sr04 制作速度检测“设备”,就像这个链接一样。但我想用超声波代替LDR。
从那个链接。激光和 ldr 是如何工作的,像这样
电阻器用作下拉电阻器,我将传感器接线并将它们放在一个盒子里,以避免它们检测到周围的光。对于每种情况,都钻了一个孔,以便激光束可以照亮传感器,而环境光不会影响传感器。工作原理很简单:经过的物体会“切断”激光束,这意味着 LDR 传感器会检测到这种突然下降的光强度。首先,我定义了一个阈值,在该阈值下传感器被认为是触发的,一旦该值低于第一个传感器的阈值,Arduino 就会等待第二个传感器被触发。在此等待时间内,它计算两个事件之间经过的时间。当第二道光束被中断时,计时器停止,现在只是简单的数学运算。2 个传感器之间的距离是已知的,
在 Arduino 代码下方:
/*
by Claudiu Cristian
*/
unsigned long time1;
int photocellPin_1 = 0; // 1st sensor is connected to a0
int photocellReading_1; // the analog reading from the analog port
int photocellPin_2 = 1; // 2nd sensor is connected to a1
int photocellReading_2; // the analog reading from the analog port
int threshold = 700; //value below sensors are trigerd
float Speed; // declaration of Speed variable
float timing;
unsigned long int calcTimeout = 0; // initialisation of timeout variable
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
photocellReading_1 = analogRead(photocellPin_1); //read out values for sensor 1
photocellReading_2 = analogRead(photocellPin_2); //read out values for sensor 2
// if reading of first sensor is smaller than threshold starts time count and moves to calculation function
if (photocellReading_1 < threshold) {
time1 = millis();
startCalculation();
}
}
// calculation function
void startCalculation() {
calcTimeout = millis(); // asign time to timeout variable
//we wait for trigger of sensor 2 to start calculation - otherwise timeout
while (!(photocellReading_2 < threshold)) {
photocellReading_2 = analogRead(photocellPin_2);
if (millis() - calcTimeout > 5000) return;
}
timing = ((float) millis() - (float) time1) / 1000.0; //computes time in seconds
Speed = 0.115 / timing; //speed in m/s given a separation distance of 11.5 cm
delay(100);
Serial.print(Speed);
Serial.print("\n");
}
如何使用超声波 HC-SR04 传感器实现代码?编码对我来说是个问题。希望有人可以帮助我...... :(请原谅我的英语不好!