Blinking LED with Toggle Switch

Hi! I'm incredibly new to programming in general, so please bear with me.
I'm working on a project and I want to incorporate an LED that flashes on 1 sec intervals after a push button is pressed, and the LED to keep flashing until the button is pressed again and the system turns off. I have a very simple code that I found online that triggers the LED to stay on when the button is pressed and off when pressed again. As well as the code that Arduino provides for blinking. But I have absolutely no idea how and if codes like these could be combined so that the system works as I want it to.
If you have any explanation, examples or guidance that I could use it would be very much appreciated!

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘posting menu’ to attach the copied sketch.

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

This is the code that I have for toggle switch without blinking. The blinking one is the one Arduino provides.

int button = 2;
int led = 13;
int status = false;

void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(button) == true) {
    status = !status;
    digitalWrite(led, status);
  } while (digitalRead(button) == true);
  delay(50);
}

This is more or less the circuit that I have physically and it seems to work just fine for the purpose it was intended.

Common LEDs need a current limiting resistor, 220R will be enough.


digitalRead(button) returns HIGH or LOW

Wire switches as S3 below and look for a LOW for switch closed/pressed.

Edit

#define OPENED                       HIGH
#define CLOSED                       LOW

#define LEDon                        HIGH
#define LEDoff                       LOW

#define ENABLED                      true
#define DISABLED                     false

const byte button                  = 2;
const byte LED                     = 13;

bool toggleFlag                    = DISABLED;

byte lastStatus                    = OPENED;
byte LEDstatus                     = LEDoff;

//timing stuff
const unsigned long toggleInterval = 1000ul;
unsigned long toggleMillis;


//********************************************^************************************************
void setup()
{
  digitalWrite(LED, LEDoff);
  pinMode(LED, OUTPUT);

  pinMode(button, INPUT_PULLUP);  //+5V---[Internal 50k]---[pin]---[switch]---GND

} //END of   setup()


//********************************************^************************************************
void loop()
{
  //****************************************
  byte currentState = digitalRead(button);

  //has the switch changed state ?
  if (lastStatus != currentState)
  {
    //let the switch settle
    delay(30);

    //are still at the same level ?
    if (digitalRead(button) == currentState)
    {
      //update to the new state
      lastStatus = currentState;

      //is the switch closed ?
      if (currentState == CLOSED)
      {
        //if the TIMER is disabled, enable it now
        if (toggleFlag == DISABLED)
        {
          //enable the TIMER
          toggleFlag = ENABLED;

          //restart the TIMER
          toggleMillis = millis();

          LEDstatus = LEDon;
          digitalWrite(LED, LEDstatus);
        }

        //the TIMER was enabled, disable it now
        else
        {
          //disable the TIMER
          toggleFlag = DISABLED;

          LEDstatus = LEDoff;
          digitalWrite(LED, LEDstatus);
        }
      }
    }
  } //END of   if (lastStatus != currentState)

  //****************************************                  L E D   T I M E R
  //if this TIMER is enabled, is it time to toggle the LED ?
  if (toggleFlag == ENABLED && millis() - toggleMillis >= toggleInterval)
  {
    //restart the TIMER
    toggleMillis = millis();

    //toggle the LED
    LEDstatus = !LEDstatus;
    digitalWrite(LED, LEDstatus);
  }


} //END of   loop()


//********************************************^************************************************
1 Like

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