我想使用Arduino和两个超声波hc-sr04使速度检测“设备”like this link。但我想用超声波来代替LDR。使用arduino和超声波hc-sr04传感器进行速度测量?
从该链接。激光器和ldr的工作原理如下
这些电阻器用作下拉电阻,我将传感器接线并放在一个箱子里,以避免它们检测周围的光线。对于每种情况,都要钻一个孔,以便激光束可以照亮传感器,而环境光线不会影响传感器。 工作原理很简单:通过的物体将“切割”激光束,这意味着LDR传感器将检测到这种突然下降的光强度。首先,我定义了一个阈值,在此阈值下传感器被视为触发,一旦该值低于第一个传感器的阈值,则Arduino等待第二个传感器被触发。在此等待时间内,它会计算两次事件之间的经过时间。当第二光束中断时,计时器停止,现在只是简单的数学运算。两个传感器之间的距离是已知的,两个事件之间的时间是已知的,速度可以计算为速度=距离/时间。
下面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传感器的代码? 编码对我来说是个问题。希望有人能帮助我...... :( 请原谅我的英语不好!
AI citit ASTA的数据表获得与pulseIn结果吗? http://www.tautvidas.com/blog/2012/08/distance-sensing-with-ultrasonic-sensor-and-arduino/;)祝你好运! –