Single action before a loop

Hi. I am very new to this so I'm sorry if this doesn't make a lot of sense.

I have a working code that flashes some LEDS in sequence once every 2-4 minutes. I want to add into the code to flash the LEDs in sequence once at start up and then go onto the once every 2-4 minute loop.
I've tried putting the code to do it once in the start up section but just keep getting errors.

Is this actually something that is possible to do?

Welcome to the forum

Yes

Code to be run once at startup should be put in the setup() function. Is that what you did ?

Please post your best attempt that caused the errors

1 Like
#include <FastLED.h>

// How many leds are in the strip?
#define NUM_LEDS 16

// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.
#define DATA_PIN 6
#define CLOCK_PIN 13

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

// This function sets up the ledsand tells the controller about them
void setup() {
  
  // sanity check delay - allows reprogramming if accidently blowing power w/leds
  delay(2000);
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);

    // Move a single white led
    for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) 

    // Turn our current led on to white, then show the leds
    leds[whiteLed] = CRGB::White;

    // Show the leds (only one of which is set to white, from above)
    FastLED.show();

    // Wait a little bit
    delay(10);

    // Turn our current led back to black for the next loop around
    leds[whiteLed] = CRGB::Black
  
}

But now I'm getting an error code just for that last line saying "Compilation error: 'whiteLed' was not declared in this scope"
But whiteLed is declared?

Please post your entire sketch. The snippet you posted will not compile so nobody can check it for you. Also, in the IDE, there is a very handle button that copies the entire error message, ready to be posted here, not just the final line. Please do that as well.

#include <FastLED.h>

// How many leds are in the strip?
#define NUM_LEDS 16

// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.
#define DATA_PIN 6
#define CLOCK_PIN 13

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

// This function sets up the ledsand tells the controller about them
void setup() {

  // sanity check delay - allows reprogramming if accidently blowing power w/leds
  delay(2000);
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);

  // Move a single white led
  for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1)

    // Turn our current led on to white, then show the leds
    leds[whiteLed] = CRGB::White;

  // Show the leds (only one of which is set to white, from above)
  FastLED.show();

  // Wait a little bit
  delay(10);

  // Turn our current led back to black for the next loop around
  leds[whiteLed] = CRGB::Black
}

// This function runs over and over, and is where you do the magic to light
// your leds.
void loop() {

  long randNumber;

  // Delay between stars
  randNumber = random(480000, 720000);
  FastLED.delay(randNumber);


  // Move a single white led
  for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {

    // Turn our current led on to white, then show the leds
    leds[whiteLed] = CRGB::White;

    // Show the leds (only one of which is set to white, from above)
    FastLED.show();

    // Wait a little bit
    delay(10);

    // Turn our current led back to black for the next loop around
    leds[whiteLed] = CRGB::Black;
  }
}

That was my entire error message though:

exit status 1

Compilation error: 'whiteLed' was not declared in this scope

You are seeing "scope" of the definition. Here, you defined "whiteLed" to only have a scope of inside the "for" loop (from "for" to the "for" loop close brace).

If you put "int whiteLed" at the beginning of setup(), then the scope will only be inside "setup()"

If you put "int whiteLed" outside of any function, then the scope would be "global" and usable everywhere.

No, it was not.

Doubtful. I get this

Arduino: 1.8.19 (Windows 10), Board: "Arduino Micro"

C:\Users\brianh\AppData\Local\Temp\arduino_modified_sketch_218970\sketch_apr27a.ino: In function 'void setup()':

sketch_apr27a:34:8: error: 'whiteLed' was not declared in this scope

   leds[whiteLed] = CRGB::Black

        ^~~~~~~~

exit status 1

'whiteLed' was not declared in this scope



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

when I click on the "Copy error messages" button

Sorry I miss read the first bit as just my file name

error message is this:

C:\Users\Tarn Hedges\Downloads\shooting_star_no_testbutton_8_to_12_min_random\shooting_star_no_testbutton_8_to_12_min_random.ino: In function 'void setup()':
C:\Users\Tarn Hedges\Downloads\shooting_star_no_testbutton_8_to_12_min_random\shooting_star_no_testbutton_8_to_12_min_random.ino:34:8: error: 'whiteLed' was not declared in this scope

    ^       

exit status 1

Compilation error: 'whiteLed' was not declared in this scope

Try this. Declare int whiteLed before the setup function, not in the for loop

int whiteLed;
void setup() {

It only seems to be line 38 that causes the error even though whiteLed is used a couple of lines above

Have you tried the suggested change?

Yes,
This is the new error message

C:\Users\Tarn Hedges\Downloads\shooting_star_no_testbutton_8_to_12_min_random\shooting_star_no_testbutton_8_to_12_min_random.ino:42:1: error: expected initializer before 'void'
delay(10);
^
C:\Users\Tarn Hedges\Downloads\shooting_star_no_testbutton_8_to_12_min_random\shooting_star_no_testbutton_8_to_12_min_random.ino: In function 'void setup()':
C:\Users\Tarn Hedges\Downloads\shooting_star_no_testbutton_8_to_12_min_random\shooting_star_no_testbutton_8_to_12_min_random.ino:37:8: error: 'whiteLed' was not declared in this scope

    ^       

exit status 1

Compilation error: expected initializer before 'void'

Post the new code that gives these errors. Cannot tell what is wrong without seeing what is wrong.

Ok I can't seem to recreate that error now!

my code is currently this:

#include <FastLED.h>

// How many leds are in the strip?
#define NUM_LEDS 16

// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.
#define DATA_PIN 6
#define CLOCK_PIN 13

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

// This function sets up the ledsand tells the controller about them
int whiteLed;
void setup() {

  // sanity check delay - allows reprogramming if accidently blowing power w/leds
  delay(2000);
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);

  // Move a single white led
  for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1)

    // Turn our current led on to white, then show the leds
    leds[whiteLed] = CRGB::White;

  // Show the leds (only one of which is set to white, from above)
  FastLED.show();

  // Wait a little bit
  delay(10);

  // Turn our current led back to black for the next loop around
  leds[whiteLed] = CRGB::Black;
}

// This function runs over and over, and is where you do the magic to light
// your leds.
void loop() {

  long randNumber;

  // Delay between stars
  randNumber = random(480000, 720000);
  FastLED.delay(randNumber);


  // Move a single white led
  for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {

    // Turn our current led on to white, then show the leds
    leds[whiteLed] = CRGB::White;

    // Show the leds (only one of which is set to white, from above)
    FastLED.show();

    // Wait a little bit
    delay(10);

    // Turn our current led back to black for the next loop around
    leds[whiteLed] = CRGB::Black;
  }
}

No errors but all of the LEDs are just constantly on now

Haven't used FastLED library, so not sure what the FastLED.delay(randNumber) is supposed to do.
However, in your for loop, you turn all the leds on, call FastLED.show(), wait 10 milliseconds, set the leds off (with no FastLED.show() call), then immediately go back to your for loop and turn them all on again.

remove the int from for(int whiteLed=0;
redeclaring it confuses the problem, because you have two different instances of the variaable now.

You also forgot to open { and } your multi-statement for loop.
Try this(untested):

#include <FastLED.h>

// How many leds are in the strip?
#define NUM_LEDS 16

// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.
#define DATA_PIN 6
#define CLOCK_PIN 13

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

// This function sets up the ledsand tells the controller about them
int whiteLed;
void setup() {

  // sanity check delay - allows reprogramming if accidently blowing power w/leds
  delay(2000);
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);

  // Move a single white led
  for (whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {

    // Turn our current led on to white, then show the leds
    leds[whiteLed] = CRGB::White;

    // Show the leds (only one of which is set to white, from above)
    FastLED.show();

    // Wait a little bit
    delay(10);

    // Turn our current led back to black for the next loop around
    leds[whiteLed] = CRGB::Black;
  }
}
// This function runs over and over, and is where you do the magic to light
// your leds.
void loop() {

  long randNumber;

  // Delay between stars
  randNumber = random(480000, 720000);
  FastLED.delay(randNumber);


  // Move a single white led
  for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {

    // Turn our current led on to white, then show the leds
    leds[whiteLed] = CRGB::White;

    // Show the leds (only one of which is set to white, from above)
    FastLED.show();

    // Wait a little bit
    delay(10);

    // Turn our current led back to black for the next loop around
    leds[whiteLed] = CRGB::Black;
  }
}
1 Like

There's still a few whiteLed variables to get confused about.

You could move the global one so setup() has its own local variable:

//int whiteLed;
void setup() {
  int whiteLed;

And in loop(), it now works because it is only used within the body of the for loop, after which it no longer exists.

I like the ability to declare a very local variable like the for loop index in this case, but I find that more often than you might think that variable is useful after it would have gone out of scope.

a7

1 Like

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