forked from coloz/Arduino-book
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.5.1-ButtonAndLED.ino
More file actions
44 lines (35 loc) · 1.01 KB
/
Copy path2.5.1-ButtonAndLED.ino
File metadata and controls
44 lines (35 loc) · 1.01 KB
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
41
42
43
44
/*
使用通过2号引脚连接的按键,控制13号引脚连接的LED
备注:大多数Arduino的13号引脚上都连接了名为L的LED.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
// 设置各引脚别名
const int buttonPin = 2; // 连接按键的引脚
const int ledPin = 13; // 连接LED的引脚
// 变量定义
int buttonState = 0; // 存储按键状态的变量
void setup() {
// 初始化LED引脚为输出状态
pinMode(ledPin, OUTPUT);
// 初始化按键引脚为输入状态
pinMode(buttonPin, INPUT);
}
void loop(){
// 读取按键状态并存储在变量中
buttonState = digitalRead(buttonPin);
// 检查按键是否被按下
// 如果按键按下,那buttonState应该为高电平
if (buttonState == HIGH) {
//点亮LED
digitalWrite(ledPin, HIGH);
}
else {
// 熄灭LED
digitalWrite(ledPin, LOW);
}
}