Light that blinks three times when a button is pressed

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.

Brief Introduction

Light that blinks three times when a button is pressed (arduino)
DESCRIPTION:
This Arduino project turns an LED on and off three times each time a button is pressed. The LED is connected to digital pin 8 and the button to digital pin 2 with internal pull-up resistor enabled. The blinking occurs with intervals of 300 milliseconds.

ADDITIONAL NOTES:
The button is connected with INPUT_PULLUP configuration, so the pin reads HIGH when the button is not pressed and LOW when it is pressed. The code handles button debounce simply by checking the previous state. The blinking happens 3 times with 300ms on and 300ms off.

EXPLANATIONS:
- The 220Ω resistor limits the current passing through the LED to protect it.
- The button is connected between digital pin 2 and GND, with the pin configured as INPUT_PULLUP to avoid external resistors.
- The variable 'buttonState' reads the current state of the button, while 'lastButtonState' is used to detect state changes (press).
- The function 'lampeggiaTreVolte()' turns the LED on and off three times with 300ms delay to make the blinking visible.
- The button control happens in the main loop, blinking only when a new press is detected (transition from HIGH to LOW).

Example Code: arduino

// 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
  }
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

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)

Similar Topics

Pedestrian traffic light with two LEDs and button
Simple Arduino pedestrian traffic light with red and green LEDs controlled by a button using INPUT_PULLUP and millis timer for non-blocking operation.
LED that turns on in the dark with photoresistor
Arduino project using a photoresistor to detect darkness and automatically turn on an LED with adjustable light threshold and serial monitoring.
Night Light Activated by PIR Motion Sensor
Automatic night light using Arduino and PIR motion sensor to detect movement and turn on an LED, saving energy with easy setup and reliable performance.
Ambient Light Meter with LED Bar
Arduino ambient light meter project using LDR sensor and 1 LED bar to display light intensity with voltage divider and LED control.
Simple Traffic Light with Three LEDs and Reset Button
Simple Arduino traffic light project with three LEDs and a reset button featuring software debounce and classic light cycle control for beginners.
Courtesy light with delayed shutdown after button press
Arduino project for a courtesy LED with delayed shutdown after button press using internal pull-up resistor for simple wiring and adjustable timing.
Welcome light that turns on when a doorbell button is pressed
Arduino project that lights an LED as a welcome light when a doorbell button is pressed, staying on for 5 seconds using millis for non-blocking timing.
Potentiometer Rotation Indicator with Three Threshold LEDs
Arduino project using a potentiometer to control three LEDs lighting up progressively at set rotation thresholds for a clear visual indicator.