Several LEDs blinking independently - learning

Hi Arduino Community,

I'm new here and having great fun learning Arduino myself during lockdown (Tinkercad is a brilliant tool).

I have completed basic code to set various LEDs at different blinking patterns with the delay function in a loop.
Next I would like to run these different blinking pattern LEDs at the same time.

I have trawled this forum and there are lots of mentions of millis and state functions along with plenty of previous examples of code including 'SeveralThingsAtTheSameTime'.
I'm still very knew so I don't quite fully understand everything just yet to be able to copy and write myself.
I tried running the above code but kept getting errors with servos and buttons I really don't want to get messed up with with yet.
Just trying to 'keep-it-simple-stupid' as I learn my way through it all.

I understand the millis function is probably what I need to run these LEDs simultaneously and put the delay code in the bin?

Below is just an example of the loop code LED blinking I was able to do.
(to all you hardcore-pro-coders, I know this code is probably not very handsome but I'm learning!).
How would I run these LEDs simultaneously? and let it just run again and again and again.
No buttons, no messing, just simple-stupid so I can get the hang of it!

Thanks!

int red=13;
int redTime=250;
int redBlink=5;
int green=12;
int greenTime=700;
int greenBlink=3;
int yellow=11;
int yellowTime=500;
int white=10;
int whiteTime=1500;
int whiteBlink=6;
int j;

void setup()
{
  pinMode(red,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(yellow,OUTPUT);
  pinMode(white,OUTPUT);
}

void loop() {
  
  for(j=1;j<=redBlink;j=j+1){
  digitalWrite(red,HIGH);
  delay(redTime);
  digitalWrite(red,LOW);
  delay(redTime);
  }
  
  for(j=1;j<=greenBlink;j=j+1){
  digitalWrite(green,HIGH);
  delay(greenTime);
  digitalWrite(green,LOW);
  delay(greenTime);
  }
    
  digitalWrite(yellow,HIGH);
  delay(yellowTime);
  digitalWrite(yellow,LOW);
  delay(yellowTime);
  digitalWrite(yellow,HIGH);
  delay(yellowTime);
  digitalWrite(yellow,LOW);
    
  digitalWrite(white,HIGH);
  delay(whiteTime);
  digitalWrite(white,LOW);
  delay(whiteTime);
  for(j=1;j<=whiteBlink;j=j+1){
  digitalWrite(white,HIGH);
  delay(600);
  digitalWrite(white,LOW);
  delay(600);
  }
}

The delay() function will block other code during delay time. To blink the multiple LED, you are better to use millis() instead of delay(). Or you can use the output library, which supports blink multiple led independently without using delay() function. You can see this Multiple Blink Without Delay Example

An example

//blink 3 LEDs, each with different periods on/off - non blocking

const byte ledPins[] = {3, 5, 6};
const byte NUMBER_OF_LEDS = sizeof(ledPins) / sizeof(ledPins[0]);
unsigned long startTimes[NUMBER_OF_LEDS];
unsigned long periods[][NUMBER_OF_LEDS] = { {500, 300, 100}, {500, 200, 450} };  //on, off periods

void setup()
{
  for (int led = 0; led < NUMBER_OF_LEDS; led++)
  {
    pinMode(ledPins[led], OUTPUT);
  }
}

void loop()
{
  unsigned long currentTime = millis();
  for (int led = 0; led < NUMBER_OF_LEDS; led++)
  {
    byte ledState = digitalRead(ledPins[led]);
    if (currentTime - startTimes[led] >= periods[ledState][led])
    {
      digitalWrite(ledPins[led], !ledState);
      startTimes[led] = currentTime;
    }
  }
}

Great thanks.
That output library looks really useful.
I will try that.
I am waiting for my actual kit to arrive so I have been using the online tinkercad simulator.

Thanks
UKHeliBob

I will try make heads or tails out of that.

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