Go Down

Topic: Can't call a FastLED function outside the "void loop()" function (Read 1 time) previous topic - next topic

Av8ter

Hello,
Would someone please tell me why the first sketch compiles, runs, and correctly lights the appropriate leds, while the second sketch compiles without errors, but does nothing?  Thanks.

Sketch 1:

Code: [Select]

#include "FastLED.h"

#define NUM_LEDS 4

CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];
CRGB leds4[NUM_LEDS];
CRGB leds8[NUM_LEDS];
uint8_t gBrightness = 128;

void setup() {
  FastLED.addLeds<WS2812,3>(leds2, NUM_LEDS);  // Binary 2 position
  FastLED.addLeds<WS2812,4>(leds1, NUM_LEDS);  // Binary 1 position
  FastLED.addLeds<WS2812,5>(leds4, NUM_LEDS);  // Binary 4 position
  FastLED.addLeds<WS2812,6>(leds8, NUM_LEDS);  // Binary 8 position
}

void loop() {
  FastLED.clear();
  for(int i = 0; i < NUM_LEDS; i++) {
    // set our current dot to red
    leds1[i] = CRGB::Red;
    FastLED.show();
    // clear our current dot before we move on
    leds1[i] = CRGB::Black;
    delay(100);
  }
  // draw led data for the first strand into leds
  fill_solid(leds1, NUM_LEDS, CRGB::Red);
  FastLED[1].showLeds(gBrightness);
  delay(500);
  
  for(int i = 0; i < NUM_LEDS; i++) {
    // set our current dot to Green
    leds2[i] = CRGB::Green;
    FastLED.show();
    // clear our current dot before we move on
    leds2[i] = CRGB::Black;
    delay(100);
  }
  // draw led data for the second strand into leds
  fill_solid(leds2, NUM_LEDS, CRGB::Green);
  FastLED[0].showLeds(gBrightness);
 delay(500);
  
  for(int i = 0; i < NUM_LEDS; i++) {
    // set our current dot to Blue
    leds4[i] = CRGB::Blue;
    FastLED.show();
    // clear our current dot before we move on
    leds4[i] = CRGB::Black;
    delay(100);
  }
    // draw led data for the third strand into leds
  fill_solid(leds4, NUM_LEDS, CRGB::Blue);
  FastLED[2].showLeds(gBrightness);
  delay(500);
  
 
 
   for(int i = 0; i < NUM_LEDS; i++) {
    // set our current dot to White
    leds8[i] = CRGB::White;
    FastLED.show();
    // clear our current dot before we move on
    leds8[i] = CRGB::Black;
    delay(100);
  }
 

  // draw led data for the fourth strand into leds
  fill_solid(leds8, NUM_LEDS, CRGB::White);
  FastLED[3].showLeds(gBrightness);
 delay(500);
}



Sketch 2:

Code: [Select]

#include "FastLED.h"

#define NUM_LEDS 4

CRGB leds1[NUM_LEDS];
CRGB leds2[NUM_LEDS];
CRGB leds3[NUM_LEDS];
CRGB leds4[NUM_LEDS];
uint8_t gBrightness = 128;

void setup() {
  
  FastLED.addLeds<WS2812,3>(leds2, NUM_LEDS);  // Binary 2 position
  FastLED.addLeds<WS2812,4>(leds1, NUM_LEDS);  // Binary 1 position
  FastLED.addLeds<WS2812,5>(leds3, NUM_LEDS);  // Binary 4 position
  FastLED.addLeds<WS2812,6>(leds4, NUM_LEDS);  // Binary 8 position
}

void loop()
{
FastLED.clear();
void redLeds();
void greenLeds();
void blueLeds();
void whiteLeds();
}

void redLeds()
  {
  for(int i = 0; i < NUM_LEDS; i++) {
    // set our current dot to red
    leds1[i] = CRGB::Red;
    FastLED.show();
    delay(100);
  }
    // draw led data for the first strand into leds
  fill_solid(leds1, NUM_LEDS, CRGB::Red);
  FastLED[0].showLeds(gBrightness);
   delay(500);
  }
  
  void greenLeds()
  {
  for(int i = 0; i < NUM_LEDS; i++) {
    // set our current dot to Green
    leds2[i] = CRGB::Green;
    FastLED.show();
    delay(100);
  }
  // draw led data for the second strand into leds
  fill_solid(leds2, NUM_LEDS, CRGB::Green);
  FastLED[1].showLeds(gBrightness);
  delay(500);
 }
  
  
 void blueLeds()
 {
 for(int i = 0; i < NUM_LEDS; i++) {
    // set our current dot to Blue
    leds3[i] = CRGB::Blue;
    FastLED.show();
    delay(100);
  }
  // draw led data for the third strand into leds
  fill_solid(leds3, NUM_LEDS, CRGB::Blue);
  FastLED[2].showLeds(gBrightness);
  delay(500);
  }
 
 void whiteLeds()
 {
   for(int i = 0; i < NUM_LEDS; i++) {
    // set our current dot to White
    leds4[i] = CRGB::White;
    FastLED.show();
    delay(100);
  }
   // draw led data for the fourth strand into leds
  fill_solid(leds4, NUM_LEDS, CRGB::White);
  FastLED[3].showLeds(gBrightness);
  delay(500);
 }



pert

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool. I recommend you to use the standard IDE instead.

A very useful troubleshooting technique is to add some Serial.print() statements throughout your code so you can get an idea of what is happening.

When you run into a problem it's a good idea to reduce your sketch to the minimum amount of code that will still reproduce the problem. Often you will be able to find the problem after doing this, otherwise it makes it much easier for others to help you.

ieee488

Hello,
Would someone please tell me why the first sketch compiles, runs, and correctly lights the appropriate leds, while the second sketch compiles without errors, but does nothing?  Thanks.
Because
Code: [Select]


void redLeds();
void greenLeds();
void blueLeds();
void whiteLeds();


inside loop() is wrong
and should be

Code: [Select]

redLeds();
greenLeds();
blueLeds();
whiteLeds();


and you need to declare

redLeds, greenLeds , blueLeds, whiteLeds functions





Go Up