极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9422|回复: 1

delay()和 超声波 不能在一起用吗

[复制链接]
发表于 2013-6-8 22:21:49 | 显示全部楼层 |阅读模式
本帖最后由 jju0808 于 2013-6-9 00:03 编辑

我的一个小程序,想每隔一段时间(100ms)用超声波探测一次距离。但却发现delay()不能用。
只要我把delay()去掉就能测距,加上就不行,只显示距离为0.00。难道他们有仇吗,不能在一起使用?
这么个小程序都运行不了,真受打击。很多地方都需要延迟后再测距,如果我的程序不行,怎么才能实现这样的功能呢?

  1. int in=0;
  2. int out=1;
  3. float d;
  4. void setup()
  5. {
  6.   pinMode(in,INPUT);
  7.   pinMode(out,OUTPUT);
  8. Serial.begin(9600);
  9. }
  10. void loop()
  11. {
  12.   delay(100);     ///////////////////////////////////////
  13.   digitalWrite(out,LOW);
  14.   delayMicroseconds(2);
  15.   digitalWrite(out,HIGH);
  16.   delayMicroseconds(10);
  17.   digitalWrite(out,LOW);
  18.   d=pulseIn(in,HIGH);
  19.   d=d/58;
  20.      Serial.print(d);
  21.     Serial.print("---");
  22. }
复制代码

回复

使用道具 举报

 楼主| 发表于 2013-6-9 00:04:22 | 显示全部楼层
已经解决。是引脚要用2和3才好,我用的是0和1.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-5 00:14 , Processed in 0.033566 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表