Arduino project to blink an LED three times when a button is pressed using digital pins 8 and 2 with internal pull-up and 300ms intervals.
// Project: Light that blinks three times when a button is pressed
const int ledPin = 8; // Pin to which the LED is connected
const int buttonPin = 2; // Pin to which the button is connected
int buttonState = HIGH; // Current state of the button
int lastButtonState = HIGH; // Previous state of the button
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with internal pull-up
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the current state of the button
// Check if the button was pressed (transition from HIGH to LOW)
if (lastButtonState == HIGH && buttonState == LOW) {
lampeggiaTreVolte(); // Perform the blinking
}
lastButtonState = buttonState; // Update the previous state
}
// Function that blinks the LED three times
void lampeggiaTreVolte() {
for (int i = 0; i < 3; i++) {
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(300); // Wait 300 milliseconds
digitalWrite(ledPin, LOW); // Turn off the LED
delay(300); // Wait 300 milliseconds
}
}Arduino Uno (or compatible)
5mm LED (x1)
220Ω Resistor (x1)
Button (x1)
CONNECTIONS: LED Cathode -> GND
CONNECTIONS: LED Anode -> Pin D8 through 220Ω resistor
CONNECTIONS: Button -> Pin D2 and GND (uses internal INPUT_PULLUP)