Go Down

Topic: Combining LED effects in loops (Read 606 times) previous topic - next topic

coldjungles

Hello, i have created an LED matrix using WS2812 strip lights that is 21 by 7 in size. I have created several different code files for different effects, from the fastLED Cylon demo to scrolling text. My problem is when i attempted to combine them with different functions with two loops it will only do the cylon demo colors and then briefly flashes and then continues the cylon demo and I cant understand why. Here is my code that doesnt work properly



#include <WS2812FX.h>

#include <FastLED.h>

#define LED_COUNT 147

#define LED_PIN 7

#define TIMER_MS 5000

// Parameter 1 = number of pixels in strip

// Parameter 2 = Arduino pin number (most are valid)

// Parameter 3 = pixel type flags, add together as needed:

//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)

//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)

//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)

//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)

//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

CRGB leds[LED_COUNT];

WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800);

unsigned long last_change = 0;

unsigned long now = 0;

void setup() {

 ws2812fx.init();

 ws2812fx.setBrightness(75);

 ws2812fx.setSpeed(1000);

 ws2812fx.setColor(0x007BFF);

ws2812fx.setMode(FX_MODE_STATIC);

 ws2812fx.start();

 Serial.begin(57600);

 Serial.println("resetting");

 LEDS.addLeds<WS2812,LED_PIN,RGB>(leds,LED_COUNT);

 LEDS.setBrightness(84);

}

void loop1() {

 now = millis();

 ws2812fx.service();



 if(now - last_change > TIMER_MS) {

   ws2812fx.setMode((ws2812fx.getMode() + 1) % ws2812fx.getModeCount());

   last_change = now;

 }

}



void fadeall() { for(int i = 0; i < LED_COUNT; i++) { leds.nscale8(250); } }



void loop2() {

 static uint8_t hue = 0;

 Serial.print("x");

 // First slide the led in one direction

 for(int i = 0; i < LED_COUNT; i++) {

   // Set the i'th led to red

   leds = CHSV(hue++, 255, 255);

   // Show the leds

   FastLED.show();

   // now that we've shown the leds, reset the i'th led to black

   // leds = CRGB::Black;

   fadeall();

   // Wait a little bit before we loop around and do it again

   delay(10);

 }

 Serial.print("x");



 // Now go in the other direction.

 for(int i = (LED_COUNT)-1; i >= 0; i--) {

   // Set the i'th led to red

   leds = CHSV(hue++, 255, 255);

   // Show the leds

   FastLED.show();

   // now that we've shown the leds, reset the i'th led to black

   // leds = CRGB::Black;

   fadeall();

   // Wait a little bit before we loop around and do it again

   delay(10);

 }

}



void loop(){

 loop1();

 loop2();

}

J-M-L

« Manually walk » through your code execution and you'll see where the time goes ...

You need to decompose your effect functions in such a way that they take the next steps at the right moment, that there is no huge loop nor delays

----

Please correct your post above and add code tags around your code:
[code] // your code is here [/code].

It should look like this:
Code: [Select]
// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Deva_Rishi

and post the
Code: [Select]
#include <WS2812FX.h>file (within code tags if the size is not to big otherwise as attachment) so we can see what it is that is happening under the hood. FastLED is not my preferred library for controlling LEDS due to the possibility of writing beyond the size of the array (which many beginners tend to do.) and the added 'delay() function that is used within it's built in pattern functions. Consider using another Library. (eg adafruit_neopixel or makuna_neopixelbus)
To 'Correct' you have to be Correct. (and not be condescending..)

gfvalvo

#3
May 03, 2019, 02:03 pm Last Edit: May 03, 2019, 02:05 pm by gfvalvo
FastLED is not my preferred library for controlling LEDS due to the possibility of writing beyond the size of the array (which many beginners tend to do.)
But, if you know what you're doing, FastLed's direct access to the pixel object array allows you to write efficient code. If you have other time-critical functions in your application, both FastLed and Teensy have DMA-based drivers for WS2812-type strips so interrupts don't have to be disabled. For a really fast POV Display, I use FastLed and APA102 LEDS that I can blast over SPI at 18MHz.
No technical questions via PM. They will be ignored. Post your questions in the forum so that all may learn.

J-M-L

Agree APA102 are really good to use and don't have the refresh rate constraint that takes a toll on your ISRs
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Deva_Rishi

FastLed's direct access to the pixel object array allows you to write efficient code.
If you want direct access it is not a big deal to do that with adafruit_neopixel, by retrieving the pointer to the buffer. Main advantage is that you need to know what you are doing, which saves a lot of time on this forum.
both FastLed and Teensy have DMA-based drivers for WS2812-type strips so interrupts don't have to be disabled.
Makuna_neopixelbus has both DMA & UART modes for any ESP (as is teensy..) as well as the bit-banged mode but who wants that ?
To 'Correct' you have to be Correct. (and not be condescending..)

Go Up