// Define constants for pin numbers
const int ledPin = 17; // LED connected to digital pin 17
const int buttonPin = 14; // Push button connected to digital pin 14
// Define variables to store button state
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// Initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
// Initialize the push button pin as an input
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the state of the push button
buttonState = digitalRead(buttonPin);
// Check if the button is pressed
if (buttonState != lastButtonState) {
// If the button is pressed
if (buttonState == HIGH) {
// Turn on the LED
digitalWrite(ledPin, HIGH);
} else {
// Turn off the LED
digitalWrite(ledPin, LOW);
}
// Delay a short time to debounce
delay(50);
}
// Save the current state as the last state,
// for the next time through the loop
lastButtonState = buttonState;
}