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);
    }
}