“超声波测距示例”的版本间的差异

来自Microduino Wikipedia
跳转至: 导航搜索
(创建页面,内容为“{| style="width: 800px;" |- | <p style="color: #4F4E4E;font-size:220%">'''Sensor-Ultrasonic测距示例'''</p> <br> 本示例给出了使用超声波传感器测距...”)
 
 
(未显示同一用户的3个中间版本)
第16行: 第16行:
 
*[[mCookie-Hub/zh|mCookie-Hub]]
 
*[[mCookie-Hub/zh|mCookie-Hub]]
 
*[[Sensor-Ultrasonic/zh|Sensor-Ultrasonic]]
 
*[[Sensor-Ultrasonic/zh|Sensor-Ultrasonic]]
<br>
 
[[File:.jpg|600px|center]]
 
<br>
 
 
<br>
 
<br>
 
<p style="color: #E87E05;font-size:135%">电路搭建</p>
 
<p style="color: #E87E05;font-size:135%">电路搭建</p>
第24行: 第21行:
 
<br>
 
<br>
 
将Battery、Core、Hub堆叠在一起,通过MicroUSB数据线接入电脑。初次使用请参考:[[AVR核心:Getting_started/zh|Getting Started]]。
 
将Battery、Core、Hub堆叠在一起,通过MicroUSB数据线接入电脑。初次使用请参考:[[AVR核心:Getting_started/zh|Getting Started]]。
<br>
 
[[File:.jpg|600px|center]]
 
 
<br>
 
<br>
 
<br>
 
<br>
第37行: 第32行:
  
 
void setup() {
 
void setup() {
   Serial.begin(9600);                       // 串口初始化
+
   Serial.begin(9600); // 串口初始化
   if (Ultrasonic1.begin()) {                //如果超声波初始化成功
+
   Ultrasonic1.begin(); //超声波初始化
    Serial.println("Ultrasonic is online");//串口打印信息
+
   delay(2000);
  }
 
   else {                                  //如果超声波初始化不成功
 
    Serial.println("error");              //串口打印信息
 
    while (1);                             //程序在此循环运行,即不再向下运行
 
  }
 
 
}
 
}
  
 
void loop() {
 
void loop() {
   Distance = Ultrasonic1.requstDistance(); //获取超声波测得的距离
+
   Distance = Ultrasonic1.getDistance(); //获取超声波测得的距离
 
   Serial.println(Distance);              //串口打印距离值
 
   Serial.println(Distance);              //串口打印距离值
 
   delay(30);                              //延时30ms
 
   delay(30);                              //延时30ms
第54行: 第44行:
 
</source>
 
</source>
 
<br>
 
<br>
 
<p style="color: #E87E05;font-size:135%">相关案例</p>
 
 
 
|}
 
|}
 
<br>
 
<br>
 
<br>
 
<br>
  
 
+
[[Sensor-Ultrasonic/zh|返回Sensor-Ultrasonic 界面]]
<p style="font-size:115%">[[Sensor-Ultrasonic/zh|返回Sensor-Ultrasonic 界面]]</p>
 

2018年5月14日 (一) 10:24的最新版本

Sensor-Ultrasonic测距示例


本示例给出了使用超声波传感器测距离的方法

并将测得的距离通过串口打印出来

距离单位毫米(mm)

所需硬件


电路搭建

将超声波传感器接到Hub的IIC引脚。
将Battery、Core、Hub堆叠在一起,通过MicroUSB数据线接入电脑。初次使用请参考:Getting Started

代码

#include <Microduino_Ultrasonic.h>         //引用超声波库文件

Ultrasonic Ultrasonic1(ULTRASONIC_ADDR_1);//将Ultrasonic实例化

uint16_t Distance;   //定义变量

void setup() {
  Serial.begin(9600);  // 串口初始化
  Ultrasonic1.begin();  //超声波初始化
  delay(2000);
}

void loop() {
  Distance = Ultrasonic1.getDistance(); //获取超声波测得的距离
  Serial.println(Distance);               //串口打印距离值
  delay(30);                              //延时30ms
}




返回Sensor-Ultrasonic 界面