找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4026|回复: 1
打印 上一主题 下一主题
收起左侧

arduino多个超声波测距程序

[复制链接]
跳转到指定楼层
楼主
ID:458796 发表于 2019-3-1 19:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
//T代表trig
//E代表echo
const int T2= 14;
const int E2= 15;
const int T1= 16;
const int E1= 17;
float s1;
float s2;
void setup()
{
  Serial.begin(9600); //设置波特率
  pinMode(T1, OUTPUT);
  pinMode(E1, INPUT);
  pinMode(T2, OUTPUT);
  pinMode(E2, INPUT);
}

void loop()
{  
  digitalWrite(T1, LOW); //低高低电平发一个短时间脉冲去TrigPin
  delayMicroseconds(2);       // delayMicroseconds在更小的时间内延时准确
  digitalWrite(T1, HIGH);
  delayMicroseconds(10);
  digitalWrite(T1, LOW); //通过这里控制超声波的发射  
  s1 = pulseIn(E1, HIGH) / 58.0; //将回波时间换算成cm
  s1 = (int(s1 * 100.0)) / 100.0; //保留两位小数


  digitalWrite(T2, LOW);
  delayMicroseconds(2);
  digitalWrite(T2, HIGH);
  delayMicroseconds(10);
  digitalWrite(T2, LOW);
  s2 = pulseIn(E2, HIGH) / 58.0; //将回波时间换算成cm
  s2 = (int(s2 * 100.0)) / 100.0; //保留两位小数

  Serial.print("Distance:");
  Serial.print(s1);
  Serial.print("cm");  
  Serial.print("Distance:");
  Serial.print(s2);
  Serial.print("cm");
  Serial.println();
  delay(50);

}

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:92746 发表于 2019-8-18 22:01 | 只看该作者
超声波传感器测距
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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