Arduino 按键实验

cyang Lv6

一、功能

实现按键控制LED亮灭。按下点亮,再次按下熄灭。

二、原理图

这里写图片描述

三、代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int ButtonState;
int ButtonLastState;
int ButtonCounter;

void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT);
pinMode(11,INPUT_PULLUP);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
ButtonState = digitalRead(11);

if(ButtonState != ButtonLastState)
{
if(ButtonState)
{
Serial.println("on");
ButtonCounter++;
}
else
{
Serial.println("off");
}
delay(100);
}

ButtonLastState = ButtonState;
if(ButtonCounter%2)
{
//Serial.println(ButtonCounter);
digitalWrite(13,1);
}
else
{
digitalWrite(13,0);
}
}

四、解析

  • 1、开关通过一个数字接口连接到 arduino,端口配置为上拉输入。在默认情况下,端口电平为高,按键按下时,端口被拉低。
  • 2、理想情况下,一次按键对应着一个下降沿,一段低电平,一个上升沿。程序通过ButtonStateButtonLastState两个标志位来判断电平情况,通过这两个标志位实现了下降沿和上升沿进入第一个判断语句,即if(ButtonLastState != ButtonState),只不过下降沿和上升沿所处理的操作不同,下降沿不做处理,仅打印一条语句,上升沿时候则代表按键被按下并且已经放开了,则对按键次数进行加一。
  • 3、要实现,按下点亮,再次按下熄灭,只需要对按键次数进行模2操作。

原文链接:本人CSDN博客

  • 标题: Arduino 按键实验
  • 作者: cyang
  • 创建于 : 2017-08-19 20:14:35
  • 更新于 : 2020-02-19 21:32:44
  • 链接: https://blog.cyang.tech/2017/08/19/Arduino 按键实验/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
此页目录
Arduino 按键实验