2014-01-20 57 views
1

我想使用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传感器的代码? 编码对我来说是个问题。希望有人能帮助我...... :( 请原谅我的英语不好!

+0

AI citit ASTA的数据表获得与pulseIn结果吗? http://www.tautvidas.com/blog/2012/08/distance-sensing-with-ultrasonic-sensor-and-arduino/;)祝你好运! –

回答

1

现在已经有很多在互联网上的例子,所以如果你想要做的就是复制,谷歌的Arduino SR04

但是如果你想知道如何去做... sr04有4个引脚,vin,gnd,触发器和回显 将vin和地连接到+5和gnd 将触发器连接到数字输出引脚 连接回到数字输入引脚

通过低电平触发2微秒(us),然后高电平触发10 us然后再触发低电平 然后,从回波销

阅读更多信息

相关问题