一、按键介绍
按键相连不同侧,同侧不相连
按键是一种数字输入,可以识别两种状态
- HIGH
- LOW
二、搭建电路
上拉电阻为10K
三、代码
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);//串口通信初始,波特率为9600,Serial.begin()表示用的是Serial库里面的begin函数
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton)//读取引脚2的状态,digitalRead()可以读取引脚是HIGH还是LOW
// print out the state of the button:
Serial.println(buttonState);//打印串口数据,HIGH为1 LOW为0
delay(1); // delay in between reads for stability
}