#define BUTTON_PIN 18 // pin GPIO18, which connected to button
#define LED_PIN 21 // pin GPIO21, which connected to led
// variables will change
int button_state = 0; // variable for reading button status
void setup() {
// initialize the LED pin as an output
pinMode(LED_PIN, OUTPUT);
// initialize the button pin as an pull-up input
// the pull-up input pin will be HIGH when the button is open and LOW when the button is pressed.
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
// read the state of the button value
button_state = digitalRead(BUTTON_PIN);
// control LED according to the state of the button
if (button_state == LOW) // if button is pressed
digitalWrite(LED_PIN, HIGH); // turn on LED
else // otherwise, button is not pressing
digitalWrite(LED_PIN, LOW); // turn off LED
}