当前位置: 首页 > news >正文

Arduino示例代码讲解:Ping

Arduino示例代码讲解:Ping

  • Ping
    • 功能概述
        • 硬件部分:
        • 软件部分:
    • 代码逐行解释
        • 定义常量
        • `setup()` 函数
        • `loop()` 函数
        • `microsecondsToInches()` 函数
        • `microsecondsToCentimeters()` 函数
    • 工作原理

Ping

这段代码是一个Arduino示例程序,用于读取Parallax PING)))超声波测距传感器的数据,并计算出距离最近物体的距离。它将距离值通过串行通信发送到计算机,适用于需要测量距离的场景。

/* Ping))) Sensor

   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse
   to return.  The length of the returning pulse is proportional to
   the distance of the object from the sensor.

   The circuit:
	* +V connection of the PING))) attached to +5V
	* GND connection of the PING))) attached to ground
	* SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping

   created 3 Nov 2008
   by David A. Mellis
   modified 30 Aug 2011
   by Tom Igoe

   This example code is in the public domain.

 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {
   
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
   
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite
http://www.dtcms.com/a/111845.html

相关文章:

  • c语言学习16——内存函数
  • 面向对象(2)
  • 多模态技术概述(一)
  • Visio | 将(.vsdx)导出为更清楚/高质量的图片(.png) | 在Word里面的visio图
  • 从感光sensor到显示屏,SOC并非唯一多样
  • 手动将ModelScope的模型下载到本地
  • Eclipse怎么创建java项目
  • 前端快速入门学习2-HTML
  • 编写实现一个简易的域名服务器
  • 长龙通信机CAN数据查看(工具版)
  • AI Agent设计模式一:Chain
  • 出现次数最多的子树元素和——深度优先搜索
  • 如何将Android 应用上传到国内各大应用市场
  • Webpack中loader的作用。
  • 【AI4CODE】5 Trae 锤一个基于百度Amis的Crud应用
  • AI+OCR:解锁数字化新视界
  • 33、web前端开发之JavaScript(二)
  • KingbaseES之KDts迁移Mysql
  • 【11408学习记录】英语写作黄金模板+语法全解:用FTC数据泄漏案掌握书信结构与长难句拆解(附思维导图)
  • 【统计方法】LASSO筛变量
  • 循环引用问题和专门用来解决的weak_ptr
  • 第二十八章:Python可视化图表扩展-和弦图、旭日图、六边形箱图、桑基图和主题流图
  • 算法设计学习8
  • 从零构建大语言模型全栈开发指南:第五部分:行业应用与前沿探索-5.2.3前沿方向:MoE架构、世界模型与具身智能
  • html5炫酷3D文字效果项目开发实践
  • Flink 1.20 Kafka Connector:新旧 API 深度解析与迁移指南
  • 泰博云平台solr接口存在SSRF漏洞
  • Docker安装、配置Mysql5.7
  • hackmyvm-Principle
  • Java 大视界 -- 基于 Java 的大数据机器学习模型在图像识别中的迁移学习与模型优化(173)