Switching between/executing two "for" statements

Hello all and please be forgiving for I’m sure this will seem like a stupid question.

Very new to programming and trying to piece together two different pieces of code.

Each of the “for” statements will work individually. Would anyone be able to advise me on how to have the first "for "statement run except when the second “for” statement would be triggered? (Hopefully that question even makes sense)

#include <FastLED.h>

#define LED_PIN 9                           // hardware SPI pin SCK
#define NUM_LEDS 50
#define COLOR_ORDER GRB
#define LED_TYPE WS2811
#define MAX_BRIGHTNESS 50                   // watch the power!
#define FPS 50
#define FLASHES 8
#define FREQUENCY 200


CRGB leds[NUM_LEDS];

uint8_t hue = 0;
uint8_t pattern = 0;
unsigned int dimmer = 1;


void setup()  {
  FastLED.addLeds<WS2811, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);

}

void loop() {

  for (int i = 0; i < NUM_LEDS; i++)  {
    leds[i] = CHSV(hue, 255, 255);
  }

  EVERY_N_MILLISECONDS(100) {
    hue++;

    FastLED.show();
  }

  {
    for (int flashCounter = 0; flashCounter < random8(3, FLASHES); flashCounter++)
    {
      if (flashCounter == 0) dimmer = 5;    // the brightness of the leader is scaled down by a factor of 5
      else dimmer = random8(1, 3);          // return strokes are brighter than the leader

      FastLED.showColor(CHSV(255, 0, 255 / dimmer));
      delay(random8(4, 20));                // each flash only lasts 4-10 milliseconds
      FastLED.showColor(CHSV(255, 0, 0));

      if (flashCounter == 0) delay (150);   // longer delay until next flash after the leader
      delay(50 + random8(100));             // shorter delay between strokes
    }
    delay(random8(FREQUENCY) * 100);        // delay between strikes
  }
}

Any guidance is greatly appreciated

Do you mean that you want either the first for loop to execute or the second one to execute but not both ?

If so, what action will switch between them ?

UKHeliBob:
Do you mean that you want either the first for loop to execute or the second one to execute but not both ?

If so, what action will switch between them ?

Yes. Is this a situation that switch would be used? if/else? while? Admittedly I'm struggling to understand what makes the second "for" loop flash intermittently. Presumably it's the "delay between strikes" line, though perhaps that line using delay means that the first loop wouldn't be able to run while the second loop is "off"?

Am I thinking about any of this correctly?

Declare a global boolean variable. Let's name it firsLoop and set it to true

Then

if (firstLoop == true)
{
  //first for loop goes here
}
else
{
  //second for loop goes here
}

In order to change which for loop is executed you need something to change the value of firstLoop from true to false and vice versa to switch between them

UKHeliBob:
Declare a global boolean variable. Let’s name it firsLoop and set it to true

Then

if (firstLoop == true)

{
 //first for loop goes here
}
else
{
 //second for loop goes here
}



In order to change which for loop is executed you need something to change the value of firstLoop from true to false and vice versa to switch between them

Thank you.

I ended up going a slightly different direction as I preferred to have the switch happen based on time rather than a button or other method. However your answer pointed me in the right direction and I managed to get it working, so many thanks.

Here’s the code in case it would help anyone else:

#include <FastLED.h>

#define LED_PIN 9                           // data pin
#define NUM_LEDS 50
#define COLOR_ORDER GRB
#define LED_TYPE WS2811
#define MAX_BRIGHTNESS 50                   // watch the power!
#define FPS 50
#define FLASHES 8


CRGB leds[NUM_LEDS];

uint8_t hue = 0;
uint8_t pattern = 0;
unsigned int dimmer = 1;
unsigned long previousMillis = 0; // last time update
unsigned long interval = 90000;   // interval at which to do something i.e. lightning (milliseconds)

void setup()  {
  FastLED.addLeds<WS2811, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(50);

}

void loop() {
  unsigned long currentMillis = millis();      //time

  if (currentMillis - previousMillis > interval) {    //lightning trigger
    previousMillis = currentMillis;

    for (int flashCounter = 0; flashCounter < random8(3, FLASHES); flashCounter++)
    {
      if (flashCounter == 0) dimmer = 5;    // the brightness of the leader is scaled down by a factor of 5
      else dimmer = random8(1, 3);          // return strokes are brighter than the leader

      FastLED.showColor(CHSV(255, 0, 255 / dimmer));
      delay(random8(10, 40));                // each flash only lasts 4-10 milliseconds
      FastLED.showColor(CHSV(255, 0, 0));

      if (flashCounter == 0) delay (150);   // longer delay until next flash after the leader
      delay(50 + random8(100));             // shorter delay between strokes
    }
  }

  else  {
    for (int i = 0; i < NUM_LEDS; i++)  {
      leds[i] = CHSV(hue, 255, 255);
    }

    EVERY_N_MILLISECONDS(200) {
      hue++;

      FastLED.show();
    }
  }
}