How to programme a button to toggle a blinking LED on and off

Can anybody help me with the Arduino Uno? I want to program a tactile switch as a toggle/latch switch and then be able to switch on a blinking LED and then switch it off again. I’m very much a beginner.

Welcome to the forum

Start by looking at the StateChangeDetection example in the IDE to see how to detect when an input changes its state rather than when it is in a particular state

Once the code has detected a change of state from say HIGH to LOW then set a boolean variable to true to flag the fact that blinking is occurring

In loop(), if the boolean is true use the technique in the BlinkWithoutDelay example to turn the LED on and off without blocking the detection of further button presses

you need two parts:

a) debounce a button to switch a state on / off
b) blink a LED

a) is explained in the IDE examples called "Debounce"
b) is explained in the IDE as "Blink without Delay"

if you don't want to learn c++, you can use my library:

https://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm

quick demo:

#include <Noiasca_led.h>               // download library from http://werner.rothschopf.net/microcontroller/202202_tools_led_en.htm
#include <utility/Noiasca_discrete.h>  // use the discrete pins on the Arduino/Microcontroller
#include <Noiasca_button.h>            // some tools to read buttons

BlinkPin blinkLed {13};                // declare several blinking LED and assign pins
Button button {A0};                    // button connects pin with GND, "active LOW"

void setup() {
  Serial.begin(115200);
    blinkLed.begin();   // you have to call the .begin() method for each LED object
    blinkLed.on();      // you can switch the blinking on in setup

    button.begin();   // you have to call the .begin() method for each button

  // optional: you can set different blink intervals for your LEDs
  blinkLed.setOnInterval(500);
  blinkLed.setOffInterval(200);
}

void loop() {
  if (button.wasPressed())
  {
    Serial.println(F("button 0 was pressed, switch ON LED 0"));
    blinkLed.toggle();  // if button was pressed, toggle (change state) of blinkLed
  }

  blinkLed.update();   // you have to call update() for each LED
}

Hello

In this class less example are running the same parts, as mentioned by my dear user noiasca.

Take some time and play around. This example is running with three sets of buttons and leds.

enum Control {Off, On};
struct TIMER
{
  const unsigned long Interval;
  unsigned long previousMillis;
  int onOff;
};
struct BUTTON2LED
{
  const byte LedPin;
  const byte ButtonPin;
  int actionState;
  int stateOld;
  TIMER flash;
  TIMER debounce;
} button2Leds[] {
  {9, A0, Off, Off, 1000, 0, Off, 20, 0, On},
  {10, A1, Off, Off, 1000, 0, Off, 20, 0, On},
  {11, A2, Off, Off, 1000, 0, Off, 20, 0, On},
};

void setup()
{
  Serial.begin(115200);
  for (auto &button2Led : button2Leds)
  {
    pinMode (button2Led.ButtonPin, INPUT_PULLUP);
    pinMode (button2Led.LedPin, OUTPUT);
    digitalWrite (button2Led.LedPin, On);
    delay (1000);
    digitalWrite (button2Led.LedPin, Off);
  }
}
void loop()
{
  unsigned long currentMillis = millis();
  for (auto &button2Led : button2Leds) {
    if (currentMillis - button2Led.debounce.previousMillis >= button2Led.debounce.Interval)
    {
      button2Led.debounce.previousMillis = currentMillis;
      int stateNew = !digitalRead(button2Led.ButtonPin);
      if (button2Led.stateOld != stateNew)
      {
        button2Led.stateOld = stateNew;
        if (stateNew) button2Led.actionState = !button2Led.actionState;
        digitalWrite (button2Led.LedPin, button2Led.actionState);
      }
    }
    if (currentMillis - button2Led.flash.previousMillis >= button2Led.flash.Interval && button2Led.actionState)
    {
      button2Led.flash.previousMillis = currentMillis;
      digitalWrite (button2Led.LedPin, !digitalRead (button2Led.LedPin));
    }
  }
}

Have a nice day and enjoy coding in C++.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.