这次试试按键的操作,直接上
连线图:
按键一个引脚接pin-2,另一边是一个电阻,以及5v,当按键按下,开关闭合,引脚2就会变成高电平,也就是1,松开就会变成低电平0 。
这里没有接led,所以变化情况可以通过arduino IDE自带的serial monitor来读取。
代码:
/* DigitalReadSerial Reads a digital input on pin 2, prints the result to the serial monitor This example code is in the public domain. */
// digital pin 2 has a pushbutton attached to it. Give it a name:
int pushButton = 2;
void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
}
void loop(){
int buttonState =digitalRead(pushButton);
Serial.println(buttonState);
delay(1);
}