LED - 帶按鈕控制
你也可以使用此程式碼設定帶有上拉電阻的按鈕開關的 LED,最好在設定初始 LED 控制器後進行下一步操作
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
// initialize the LED pin as an output:
pinMode(13, OUTPUT); // You can set it just using its number
// initialize the pushbutton pin as an input:
pinMode(2, INPUT);
}
void loop()
{
// read the state of the pushbutton value:
buttonState = DigitalRead(2);
// check if the pushbutton is pressed.
// If it's not, the buttonState is HIGH : if (buttonState == HIGH)
{
// turn LED off:
digitalWrite(13, LOW);
}
else
{
// turn LED off:
digitalWrite(13, HIGH);
}
}