惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 聂微东
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
C
Check Point Blog
M
MIT News - Artificial intelligence
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
DataBreaches.Net
大猫的无限游戏
大猫的无限游戏
D
Docker
Last Week in AI
Last Week in AI
IT之家
IT之家
F
Fortinet All Blogs
A
About on SuperTechFans
P
Proofpoint News Feed
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog RSS Feed
博客园_首页
月光博客
月光博客
博客园 - 司徒正美
Y
Y Combinator Blog

博客园 - 玛雅人

五大学习方法 Python AI 与深度学习:每周任务拆分及代码练手 Python AI 与深度学习 - D2.MNIST 手写数字识别 Python基础 - 常用类库汇总 Python AI 与深度学习 - D1.PyTorch 深度学习环境一键配置 ARDUINO - P1:BLink Python调用SQLLite3 IIS8.5 安装证书 Kendo 计算字段 Kendo UI 的 k-template UpdatePanel中用后台CS代码调用JS代码,先执行控件事件,后触发JS SQL常用 Node.js 安装 生成缩略图 有用的JS函数 vs2010 mvc3 运算符 || && 如何阻止ASP.NET的按钮控件提交页面 IIS Form 认证 保护HTML页面
ARDUINO - P2:按钮控制LED
玛雅人 · 2026-02-03 · via 博客园 - 玛雅人

 1 //全局变量定义
 2 int   pinBtn=2;//引脚号
 3 int   pinLED = LED_BUILTIN;
 4 bool  isBtnStateChanged = false;//记录开关的状态是否变化
 5 bool  ledState = LOW; //默认低电平(灭)
 6 bool  btnIdleState=HIGH;//开关默认(空闲)状态
 7 bool  btnState;
 8 
 9 void setup() {
10   pinMode(pinLED, OUTPUT);  //设置LED工作引脚模式:OUTPUT
11   pinMode(pinBtn,INPUT_PULLUP); //设置开关工作引脚模式:INPUT
12   Serial.begin(9600);      //设置串口数据传输速率
13 }
14 
15 void loop() {
16   while(digitalRead(pinBtn)==btnIdleState){
17     isBtnStateChanged=true;
18   }
19 
20   //按钮按下并状态改变时执行
21   if(isBtnStateChanged){
22     ledState=!ledState; //切换LED的状态
23     isBtnStateChanged=false;  //设置开关变化状态:false
24     digitalWrite(pinLED,ledState);    
25     Serial.println(ledState?"LED-亮":"LED-灭");          
26   }
27   delay(20);//消除抖动
28 }


知识点或说明

1. 以上程序,解决了按钮长时间按下,LED闪烁的问题

2.开关 - 默认带10K的上拉电阻,空闲状态默认高电平