|
|
本帖最后由 jju0808 于 2013-6-9 00:03 编辑
我的一个小程序,想每隔一段时间(100ms)用超声波探测一次距离。但却发现delay()不能用。
只要我把delay()去掉就能测距,加上就不行,只显示距离为0.00。难道他们有仇吗,不能在一起使用?
这么个小程序都运行不了,真受打击。很多地方都需要延迟后再测距,如果我的程序不行,怎么才能实现这样的功能呢?
- int in=0;
- int out=1;
- float d;
- void setup()
- {
- pinMode(in,INPUT);
- pinMode(out,OUTPUT);
- Serial.begin(9600);
- }
- void loop()
- {
- delay(100); ///////////////////////////////////////
- digitalWrite(out,LOW);
- delayMicroseconds(2);
- digitalWrite(out,HIGH);
- delayMicroseconds(10);
- digitalWrite(out,LOW);
- d=pulseIn(in,HIGH);
- d=d/58;
- Serial.print(d);
- Serial.print("---");
- }
复制代码
|
|