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

Arduino示例代码讲解:Knock Sensor 敲击感知器

Arduino示例代码讲解:Knock Sensor 敲击感知器

  • Knock Sensor 敲击感知器
    • 功能概述
        • 硬件部分:
        • 软件部分:
    • 代码逐行解释
        • 定义常量
        • 定义变量
        • `setup()` 函数
        • `loop()` 函数
    • 工作原理

Knock Sensor 敲击感知器

这段代码是一个Arduino示例程序,用于检测敲击声。它通过读取一个压电元件(piezo element)的模拟输入值,并将其与一个设定的阈值进行比较。如果读取的值超过阈值,它会在串行端口输出“knock”,并切换引脚13上的LED的状态。这种方法适用于需要检测敲击或其他声音信号的场景。

/* Knock Sensor

   This sketch reads a piezo element to detect a knocking sound.
   It reads an analog pin and compares the result to a set threshold.
   If the result is greater than the threshold, it writes
   "knock" to the serial port, and toggles the LED on pin 13.

   The circuit:
	* + connection of the piezo attached to analog in 0
	* - connection of the piezo attached to ground
	* 1-megohm resistor attached from analog in 0 to ground

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

   created 25 Mar 2007
   by David Cuartielles <http://www.0j0.org>
   modified 30 Aug 2011
   by Tom Igoe

   This example code is in the public domain.

 */


// these constants won't change:
const int ledPin = 13;      // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 100;  // threshold value to decide when the detected sound is a knock or not


// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
int ledState = LOW;         // variable used to store the last LED status, to toggle the light

http://www.dtcms.com/a/113562.html

相关文章:

  • 每日一题(小白)模拟娱乐篇14
  • BN测试和训练时有什么不同, 在测试时怎么使用?
  • 【C++项目】从零实现RPC框架「四」:业务层实现与项目使用
  • 【51单片机】2-7【I/O口】点亮数码管
  • 线程池的工作原理
  • 线代[12]|《高等几何》陈绍菱(1984.9)(文末有对三大空间的分析及一个合格数学系毕业生的要求)
  • Python 语法学习 1(类比 java 学习)-附Python 中 self
  • 前端用用jsonp的方式解决跨域问题
  • [ICLR 2025]Biologically Plausible Brain Graph Transformer
  • Reids 的io并发模型
  • 程序化广告行业(60/89):算法优化与DSP系统实例解析
  • Linux系统程序设计:从入门到高级Day03
  • 第八章:流量治理_《凤凰架构:构建可靠的大型分布式系统》
  • DDPM 做了什么
  • 2007-2019年各省地方财政其他支出数据
  • 格式工厂怎样插入内置音频文件
  • 硬件工程师面试问题(五):蓝牙面试问题与详解
  • 在响应式网页的开发中使用固定布局、流式布局、弹性布局哪种更好
  • vllm作为服务启动,无需额外编写sh文件,一步到位【Ubuntu】
  • 『Linux_网络』 第一章 网络基础概念
  • 分表字段选择策略:以电商交易订单为例的最佳实践
  • Java项目之基于ssm的怀旧唱片售卖系统(源码+文档)
  • 大数据时代的隐私保护:区块链技术的创新应用
  • 通过构造函数和几何条件,研究了不同函数的最近点存在性、性质及单调性
  • ZKmall开源商城多云高可用架构方案:AWS/Azure/阿里云全栈实践
  • 紧急更新!MinIO发布RELEASE.2025-04-03T14-56-28Z版本,修复高危漏洞CVE-2025-31489,用户需立即升级!
  • raft协议中一条数据写入流程
  • Java 实现插入排序:[通俗易懂的排序算法系列之三]
  • 文献总结:TPAMI综述BEV感知—Delving into the devils of bird‘s-eye-view perception
  • Socket编程TCP