int led = 10; // LED connected to digital pin 10
int buttonPin = 7; // Pushbutton connected to digital pin 7
int val = 0; // Variable to store the button state
void setup() {
// Initialize LED pin as an output
pinMode(led, OUTPUT);
// Initialize button pin as an input with an internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Read the button state
val = digitalRead(buttonPin);
// If the button is pressed (LOW), turn on the LED
if (val == LOW) {
digitalWrite(led, HIGH);
}
// Otherwise, turn off the LED
else {
digitalWrite(led, LOW);
}
}