我想要制造速度检测“设备”使用与阿杜诺和两个超声波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 04传感器实现代码?编码对我来说是个问题。希望有人能帮我.(请原谅我糟糕的英语!)
发布于 2014-06-11 16:47:47
互联网上已经有很多例子,所以如果你只想复制,google arduino sr04
但如果你想知道怎么做..。sr04有4个引脚,vin,gnd,触发器和回声。连接vin和接地到+5和gnd连接触发器到数字输出引脚连接回波到数字输入引脚
通过降低2微秒触发(我们),然后高到10微秒,然后再低一次,然后用pulseIn从回波引脚得到结果。
阅读数据表以获得更多信息。
https://stackoverflow.com/questions/21234888
复制相似问题