arduino buttons

Hardware
Here Insert Picture Description
Source: Arduino circuit accessories

Specifically includes the following:

Arduino UNO circuit board (1)
Breadboard (1)
10K resistor (1)
a key switch (1)
bread line (3)

Software
Arduino official provide the IDE, as before.
Here Insert Picture Description
code show as below


const int buttonPin = 2;

const int ledPin =  13;
// 定义按键状态变量初始值为0
int buttonState = 0;


void setup() {
  // 设置ledPin端口为输出端口
  pinMode(ledPin, OUTPUT);
  // 设置buttonPin端口为输入端口
  pinMode(buttonPin, INPUT);
}


void loop() {
  // 读取按键状态
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    // 点亮LED神灯
    digitalWrite(ledPin, HIGH);
  } else {
    // 熄灭LED神灯
    digitalWrite(ledPin, LOW);
  }
}

Published 111 original articles · won praise 177 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_45172832/article/details/105156086