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

Arduino示例代码讲解: Project 12 - Knock Lock 锁

Arduino示例代码讲解: Project 12 - Knock Lock 锁

  • Project 12 - Knock Lock 锁
    • 程序功能概述
        • 功能:
        • 硬件要求:
        • 输出:
    • 代码结构
        • 全局变量
        • `setup()` 函数
        • `loop()` 函数
        • 未锁定状态:
        • 锁定状态:
        • `checkForKnock()` 函数
    • 运行过程
    • 注意事项

Project 12 - Knock Lock 锁

这段代码是一个Arduino程序,用于实现一个“敲击锁”项目,通过敲击传感器(压电传感器)和按钮控制一个伺服电机,模拟一个锁的开合。

/*Arduino Starter Kit exampleProject 12  - Knock LockThis sketch is written to accompany Project 12 in theArduino Starter KitParts required:1 Megohm resistor10 kilohm resistorthree 220 ohm resistorspiezoservo motorpush buttonone red LEDone yellow LEDone green LED100 uF capacitorCreated 18 September 2012by Scott FitzgeraldThanks to Federico Vanzati for improvementshttp://arduino.cc/starterKitThis example code is part of the public domain*/// import the library
#include <Servo.h>
// create an instance of the servo library
Servo myServo;const int piezo = A0;      // pin the piezo is attached to
const int switchPin = 2;    // pin the switch is attached to
const int yellowLed = 3;    // pin the yellow LED is attached to
const int greenLed = 4;    // pin the green LED is attached to
const int redLed = 5;   // pin the red LED is attached to// variable for the piezo value
int knockVal;
// variable for the switch value
int switchVal;// variables for the high and low limits of the knock value
const int quietKnock = 10;
const int loudKnock = 100;// variable to indicate if locked or not
boolean locked = false;
// how many valid knocks you've received
int numberOfKnocks = 0;void setup() {// attach the servo to pin 9myServo.attach(9);// make the LED pins outputspinMode(yellowLed, OUTPUT);pinMode(redLed, OUTPUT);pinMode(greenLed, OUTPUT);// set the switch pin as an inputpinMode(switchPin, INPUT);// start serial communication for debuggingSerial.begin(9600);// turn the green LED ondigitalWrite(greenLed, HIGH);// move the servo to the unlocked positionmyServo.write(0);// print status to the serial monitorSerial.println("the box is unlocked!");
}void loop() {// if the box is unlockedif (locked == false) {// read the value of the switch pinswitchVal 
http://www.xdnf.cn/news/1339.html

相关文章:

  • # 06_Elastic Stack 从入门到实践(六)
  • 【MySQL】(7) 数据库设计
  • 【集合】底层原理实现及各集合之间的区别
  • 数据库操作
  • 遥感生物多样性产品
  • 【LLM】Ollama:容器化并加载本地 GGUF 模型
  • Agent系统工程实践:Langchain-Chatchat框架定制与优化
  • 计算机视觉算法实现——垃圾分类系统
  • 理解RAG第六部分:有效的检索优化
  • C++异步操作 - future async package_task promise
  • Java学习手册:RESTful API 设计原则
  • AI日报 - 2025年4月23日
  • css3新特性第六章(2D变换)
  • 使用Python+OpenCV将多级嵌套文件夹下的视频文件抽帧为JPG图片
  • 创建表结构
  • 树莓派超全系列教程文档--(39)树莓派config.txt旧版选项
  • 线程同步——线程安全
  • jQuery — 总结
  • 逻辑思维与软件开发:从选定方向到风险管理的全流程
  • BeeWorks:低成本的国产化企业内网即时通讯
  • 使用Multipart Form-Data一次请求获取多张图片
  • Linux网络IP协议
  • 【ROS2】ROS2 插件开发流程(基于 pluginlib)
  • 2023蓝帽杯初赛内存取证-4
  • 数据结构-树
  • 美乐迪电玩客户端打包与资源替换实战教程
  • Shader属性讲解+Cg语言讲解
  • pda数据采集器,是如何采集数据的?
  • Docker底层原理浅析 | namespace+cgroups+文件系统
  • windows搭建xwiki17服务器