Need programming help for blinking

I'm a total newbie, and I'm trying to write a program to blink 8 LEDs with different HIGH and LOW. When I use the code below (I shortened it to 2 LEDs), the LEDs blink consecutively. I want them to blink independently so as to create a random appearance.
Any help would be great.

int blinkPin2 = 2;
int blinkPin3 = 3;
void setup() {
  pinMode(blinkPin2, OUTPUT);
  pinMode(blinkPin3, OUTPUT);;
}

void loop() {
  digitalWrite(blinkPin2, HIGH);
  delay(1400);
  digitalWrite(blinkPin2, LOW);
  delay(100);
  digitalWrite(blinkPin3, HIGH);
  delay(1400);
  digitalWrite(blinkPin3, LOW);
  delay(20);
}

Study the BlinkWithoutDelay example sketch that ships with the IDE.

Info about multi things

Or do you have an ESP32?

You cannot do what you want by using the delay() statement. You need a different technique for handling the timing - the BlinkWithoutDelay example shows how to do this.

For each LED you will need to hold previousMillis
And you will need 2 interval variables, as in your case the time ON is different to the time OFF (intervalOn & intervalOff).

Try it with 1 LED first... once you get that working it will be very easy to add more LEDs.

While it looks like a simple project, as a beginner, you’ll need to learn some new things and think about them…

But it’s a valuable learning project.

A random appearance makes the blink multiple LED harder for beginner.
To make it easy, you can use the LED library.

Below are example code and blink 3 LED with different frequency, different phase (appearance)

#include <ezLED.h> // ezLED library

#define NUM_LED 3 // three LEDs

#define PIN_LED_1 7
#define PIN_LED_2 8
#define PIN_LED_3 9

ezLED ledArray[NUM_LED] = {
  ezLED(PIN_LED_1),  // create ezLED object that attach to pin PIN_LED_1
  ezLED(PIN_LED_2),  // create ezLED object that attach to pin PIN_LED_2
  ezLED(PIN_LED_3)   // create ezLED object that attach to pin PIN_LED_3
};

void setup() {
  Serial.begin(9600);

  ledArray[0].blink(500, 500, 0);   // 500ms ON, 500ms OFF, blink immediately
  ledArray[1].blink(250, 500, 100); // 250ms ON, 500ms OFF, start blinking after 100ms
  ledArray[2].blink(500, 250, 200); // 500ms ON, 250ms OFF, start blinking after 200ms
  
}

void loop() {
  for (int i = 0; i < NUM_LED; i++)
    ledArray[i].loop(); // MUST call the led.loop() function in loop()
}

consider

int blinkPin2 = 2;
int blinkPin3 = 3;

void loop ()
{
    digitalWrite (blinkPin2, 50 < random(0, 100));
    digitalWrite (blinkPin3, 50 < random(0, 100));
    delay (200);
}

void setup () {
    pinMode (blinkPin2, OUTPUT);
    pinMode (blinkPin3, OUTPUT);
}

use an array of LED pins if you have many LEDs

int blinkPin3 = 3;

void loop ()
{
    digitalWrite (blinkPin2, 50 < random(0, 100));
    digitalWrite (blinkPin3, 50 < random(0, 100));
    delay (200);
}

void setup () {
    pinMode (blinkPin2, OUTPUT);
    pinMode (blinkPin3, OUTPUT);
}

How does this code randomly blink LEDS?

Post the entire code, please.

that is the entire code

random() returns a value from 0 to 100 and the conditional argument to digitalWrite either sets the LED to 0 or 1 depending if the random value > 50

so the LEDs are turned on randomly every 200 msec

yes in loop().
what don't you understand?

Oi! I see. loop() is first. Sorry. My bad.

loop() before setup()? What sorcery be this?

1 Like

Hello davidoakland

Try this small example sketch using C++.

// https://forum.arduino.cc/t/need-programming-help-for-blinking/1055045
constexpr byte Leds[] {2,3,4,5,6,7,8,10}; 
enum LedNames{One,Two,Three,Four,Five,Six,Seven,Eight};

struct LEDBLINK {
  const byte Led; 
  unsigned long stamp;
  unsigned long duration;
};
LEDBLINK ledBlinks[]
{
  {Leds[One],0,0},
  {Leds[Two],0,0},
  {Leds[Three],0,0},
  {Leds[Four],0,0},
  {Leds[Five],0,0},
  {Leds[Six],0,0},
  {Leds[Seven],0,0},
  {Leds[Eight],0,0},
};
void setup() 
{
  for (auto ledBlink:ledBlinks) pinMode(ledBlink.Led,OUTPUT); 
}

void loop() 
{
  unsigned long currentTime=millis(); 
  for (auto &ledBlink:ledBlinks) {
    if (currentTime-ledBlink.stamp >= ledBlink.duration) 
    {
      ledBlink.stamp=currentTime;
      ledBlink.duration=(unsigned long) random(100,1000);
      digitalWrite(ledBlink.Led,!digitalRead(ledBlink.Led));
    }
  }
}

I think the OP was going for different frequencies for the different blinking LEDs.

@davidoakland is advised in #2 to come to grips with "blink without delay" and in #4 that if she did, the way to make multiple independent blinking LEDs woukd be obvious.

A library is useful, but these are basic programming patterns that should be mastered in any case.

a7

presumably the code provides enough info for the OP to implement exactly what is desired.

i think seeing different interpretations of what is described as well as partially implemented may also be insightful

of course each lead could have its own timing, such as

struct Valve {
    const byte    Pin;
    unsigned long MsecOn;
    unsigned long MsecOff;
    const char   *desc;

    unsigned long msecLst;
    unsigned long msecPeriod;
};

Valve valves [] = {
    { 10, 500, 500, "V1" },
    { 11, 100, 300, "V2" },
    { 12, 250, 100, "V3" },
    { 13, 450, 200, "V4" },
};
#define Nvalve  (sizeof(valves)/sizeof(Valve))

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void
loop (void)
{
    unsigned long msec = millis ();

    Valve *v = valves;
    for (unsigned n = 0; n < Nvalve; n++, v++)  {
        if (msec - v->msecLst > v->msecPeriod)  {
            v->msecLst = msec;

            Serial.println (v->desc);
            if (On == digitalRead (v->Pin))  {
                v->msecPeriod = v->MsecOff;
                digitalWrite(v->Pin, Off);
            }
            else  {
                v->msecPeriod = v->MsecOn;
                digitalWrite(v->Pin, On);
            }
        }
    }
}

void
setup (void)
{
    Serial.begin (9600);

    for (unsigned n = 0; n < Nvalve; n++)  {
        pinMode (valves [n].Pin, OUTPUT);
    }
}

True, and it is also possible to almost cut and paste the BWD sketch, rename loop() to loopLED1() and loopLED2(), each with their own variables, and then just call both of them as functions from loop().

Hardly any programming work involved.

Perfect code for someone struggling with "blink without delay"!

a7

Thanks for the help.

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