用Tinkercad学arduino之下拉电阻按钮开灯

项目地址:https://www.tinkercad.com/things/fxAUiYvu7aG

 

 

 

const int buttonPin = 2;  //定义GPIO2输入开关按钮电平
const int ledPin =  7;    //定义GPIO7为电平输出
int buttonState = 0;      //GPIO2初使电平为低电平  

void setup() {
  pinMode(ledPin, OUTPUT);   //设置输出引脚
  pinMode(buttonPin, INPUT); //设置输入引脚
}

void loop() {
  buttonState = digitalRead(buttonPin); //读取输入电平
  if (buttonState == HIGH) {     //如输入高电平
    digitalWrite(ledPin, HIGH);   //则输出高电平
  }else {
    digitalWrite(ledPin, LOW);     //否则输出低电平
  }
}

 

posted @ 2020-12-03 00:27  meetrice  阅读(40)  评论(0编辑  收藏