按键控制LED灯亮和灭的试验(新疆乌市机器人)

实验所需材料包括 A r d u i n o U N O 、面包板 、 1个 L E D 、 1个按键 、 1个 2 2 0 Ω电阻  。

[ccenl_cpp tab_size="4" theme="default"]
/*
www.wlmqyzrobot.com
*/

int buttonPin = 2;
int ledPin = 13;
int buttonState = 0; 

void setup() 
{
  //初始化I/O口
  pinMode(buttonPin,INPUT_PULLUP);
  pinMode(ledPin,OUTPUT);
}

void loop() 
{
  buttonState = digitalRead(buttonPin);
  //按住按键时,点亮LED;放开按键后,熄灭LED。
  if(buttonState==HIGH)
  {
    digitalWrite(ledPin,LOW);
  }
  else
  {
    digitalWrite(ledPin,HIGH);
  }
}
[/ccenl_cpp]