led coding help

so i am trying to make a ghostbusters proton pack and this is my first time trying to use arduino. i am having some trouble with trying to get my lighting to work and i was wondering if i could get some help.

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop()
{
  digitalWrite(2, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(5, LOW); 
    int flicker = random(1, 100);
  digitalWrite(6, HIGH);
  delay(flicker); // Wait for 1000 millisecond(s)
  digitalWrite(6, LOW);
  delay(flicker); // Wait for 1000 millisecond(s)
}

I tried doing it like this but the flickering light (pin 6) will only activate in order with the other lights. i tried making the flickering light its own separate loop down below but that didn't work either.

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop()
{
  digitalWrite(2, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  delay(1000); // Wait for 1000 millisecond(s)
  digitalWrite(5, LOW); 
}
    
 void loop()
{   
    int flicker = random(1, 100);
  digitalWrite(6, HIGH);
  delay(flicker); // Wait for 1000 millisecond(s)
  digitalWrite(6, LOW);
  delay(flicker); // Wait for 1000 millisecond(s)
}

You can’t have two “ void loop”s in your sketch .

If you look at the code , that in the “loop” operates in sequence - the first bit happens first, the next bits afterwards.

Have a look at this : https://www.instructables.com/id/Realistic-Fire-Effect-with-Arduino-and-LEDs/

You may be interested in the several things at a time tutorial. Shows how to do many timed events independently and without using delay().

Read the Arduino reference, https://www.arduino.cc/reference/en/language/functions/random-numbers/random/

You call random(1,100) and uses that for delay. That means You get a delay between 1 and 99 mS. How do You intend to se the 1mS pulse? Try random(500, 2000)!

Open up the example BlinkWithoutDelay. Study it. Make it blink one of your proton pack LEDs.

Add another LED to the code. Give it a different period. Give it a random period.

Why you can only do one thing at a time with delay() is because it blocks everything else from running until it is done.

Lesson and code: http://www.gammon.com.au/blink

I added the highlights below, staring at the watch until time is up is what delay() does and your code should not do.

Let's look at an analogy. Say you want to cook breakfast. You need to cook:

Coffee - takes 1 minute Bacon - takes 2 minutes Eggs - takes 3 minutes

Now a seasoned cook would NOT do this:

Put coffee on. Stare at watch until 1 minute has elapsed. Pour coffee. Cook bacon. Stare at watch until 2 minutes have elapsed. Serve bacon. Fry eggs. Stare at watch until 3 minutes have elapsed. Serve eggs.

The flaw in this is that whichever way you do it, something is going to be cooked too early (and get cold).

In computer terminology this is blocking. That is, you don't do anything else until the one task at hand is over.

What you are likely to do is this:

Start frying eggs. Look at watch and note the time. Glance at watch from time to time. When one minute is up then ... Start cooking bacon. Look at watch and note the time. Glance at watch from time to time. When another minute is up then ... Put coffee on. Look at watch and note the time. When 3 minutes are up, everything is cooked. Serve it all up.

In computer terminology this is non-blocking. That is, keep doing other things while you wait for time to be up.

So, adapting this idea to blinking two LEDs at different rates, we can't afford to use:

delay (1000);

... because this blocks. That is, you can't be doing anything else during those 1000 milliseconds.

Instead we note the time, like this:

unsigned long when_eggs_started; // declare variable

...

when_eggs_started = millis (); // note the time now

Later on we can see if 3 minutes are up by subtracting when we started from the time now. This, by the way, works even if the millis () function call wraps around after approximately 50 days.

if ((millis () - when_eggs_started) >= (1000UL * 60 * 3)) { // eggs are cooked }

The "UL" after 1000 is to force the compiler to make an "unsigned long" literal, which can hold a larger number than the default of an int (integer) which can only hold -32768 to +32767.

Note that millis () returns milliseconds, thus 1000 milliseconds are a single second. So 1000 milliseconds * 60 * 3 is three minutes.

The full sketch that blinks two LEDs at different rates is:

Go to the link to get the full sketch and maybe soak in the whole lesson? Knowledge is power, this lesson will power up your code.

Consider that a little delay(1) actually wastes 16000 cpu cycles where your sketch loop might average 200 cpu cycles. Get rid of delays and your Arduino will have massively more speed than is easy to appreciate.