const int button = 6; // Pin connected to the button
const int led = 3; // Pin connected to the LED
// Variables
bool buttonPressed = false; // Flag to track if button is pressed
int buttonPressCount = 0; // Counter for the number of button presses
void setup()
{
pinMode(button, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
pinMode(led, OUTPUT); // Set LED pin as output
}
void loop()
{
// Read the state of the button
bool buttonState = digitalRead(button);
// Check if the button is pressed
if (buttonState == LOW && !buttonPressed)
{
delay(50); // Debounce delay to prevent multiple counts for one press
buttonPressed = true;
buttonPressCount++; // Increment button press count
// If button pressed two times, reset the press count
if (buttonPressCount == 2)
{
buttonPressCount = 0;
}
}
else if (buttonState == HIGH && buttonPressed)
{
delay(50); // Debounce delay
buttonPressed = false;
}
// Check if the button has been pressed twice
if (buttonPressCount == 1)
{
digitalWrite(led, HIGH); // Turn on the LED
}
else
{
digitalWrite(led, LOW); // Turn off the LED
}
}