// Constants
const int buttonPin = 2; // Pin connected to the button
const int ledPin = 13; // Pin connected to the LED
// Variables
int buttonState = 0; // Variable to store the button state
void setup() {
// Initialize serial communication:
Serial.begin(115200); // Increased baud rate for faster output
// Initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// Initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT_PULLUP); // Use INPUT_PULLUP for internal resistor
}
void loop() {
// Read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// Check if the pushbutton is pressed.
// If it is, the buttonState is LOW:
if (buttonState == LOW) {
// Print "Button pressed!" to the serial monitor:
Serial.println("Button pressed!");
// Turn the LED on:
digitalWrite(ledPin, HIGH);
// Wait for a short time (debounce)
delay(200);
// Turn the LED off:
digitalWrite(ledPin, LOW);
// Wait for a short time (debounce)
delay(200);
}
}