WS2812 LED Flashing

HI there ! I have an issue with my project, and I don’t know if it’s an hardware/wiring one or if it’s the code.
The goal is to create a led strip picking color randomly in a palette and slowy fading to another one ect…

The problem is that, sometime, a led flash (get suddently brighter for 1/10th of a second or even less).
I curently only have 2 ideas :

  • It’s a code problem (There was more flashing before, who have disappeared when I moved l.52-60 from under to above the l.62-64 part.

  • It’s a wiring problem, and I need to add a resistor between the DIN of the WS2812 and the 7 PIN (I’ve read this somewhere, but can’t find where and why).

Can you help me ? Thanks !
(and sorryfor the mistakes, English is not my primary language).

Code :

#include <FastLED.h>
#define LED_PIN     7
#define NUM_LEDS    50
#define FASTLED_ALLOW_INTERRUPTS 0
CRGB leds[NUM_LEDS];
//float fadeTimeMillis[NUM_LEDS] = {};
int Nstep[NUM_LEDS] = {};
unsigned long time = 0;
int PixelData[NUM_LEDS][5] = {};
unsigned long ResetData[NUM_LEDS] = {};
int minSpeed = 50;
int maxSpeed = 1000;
CHSV colors[] = {CHSV(213 , 179, 107) , CHSV(15, 162, 245) };
CRGB colors2[] = {CRGB  (146, 246, 70), CRGB  (253, 254, 3), CRGB(203, 251, 7), CRGB(0, 117, 94), CRGB(170, 240, 209) }; //forest
CRGB colors1[] = {CRGB  (128, 17, 0), CRGB  (182, 34, 3), CRGB (215, 53, 2), CRGB(252, 100, 0), CRGB(255, 117, 0), CRGB(250, 122, 0) }; //fire
CRGB colorsRGB[] = {CRGB  (255, 0, 0), CRGB  (0, 255, 0), CRGB(0, 0, 255) }; //RGB
 
 
void setup() {
  randomSeed(analogRead(0));
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.clear();
  FastLED.delay(500);
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }
  Serial.println("Start");
  for (int i = 0; i <= (NUM_LEDS - 1); i++) {
    PixelData[i][0] = random(6);//ColorStart*/
    PixelData[i][1] = random(6);//ColorDestination*/
    PixelData[i][2] = random(minSpeed, maxSpeed);//FadeTime*/
    PixelData[i][3] = random(255);//Target Brightness (0 max light)*/
    PixelData[i][4] = 0;//Source Brightness (0 max light)*/
    ResetData[i] = 0;
    Nstep[i] = 0;
  }
}
 
void loop()
{
  time = millis();
  for (int i = 0; i <= (NUM_LEDS - 1); i++) {
    byLedValue(i);
  }
  FastLED.show();
}
 
 
int byLedValue(int i) {
 
  if ((time - ResetData[i]) > PixelData[i][2]) { //reseting when target color il reached
    Nstep[i] -= Nstep[i];//reset Timer;
    PixelData[i][2] = random(minSpeed, maxSpeed);//*FadeTime;
    PixelData[i][0] = PixelData[i][1]; //startColor is previous destination color
    PixelData[i][1] = random(6);
    PixelData[i][4] = PixelData[i][3];
    PixelData[i][3] = random(255);//Brightness (0 max light);
    ResetData[i] = time;//New Start Time
  }
 
  Nstep[i] = (time - ResetData[i]) % PixelData[i][2];
  leds[i] = blend(colors1[PixelData[i][0]], colors1[PixelData[i][1]], (255.0 / PixelData[i][2]) * Nstep[i]);
  leds[i].fadeToBlackBy(lerp((float)PixelData[i][4], (float)PixelData[i][3], (float)Nstep[i] / (float)PixelData[i][2]));
 
 
}
float lerp(float v0, float v1, float t) {
  return (1 - t) * v0 + t * v1;

    }

LedLoop.ino (2.39 KB)

Can you select More:Modify and use the Attachments button at the bottom of the message box to Attach your code to your post? Most of us will not go to pastebin to download anything.

Here is my code :

Please post it here using code tags when you do.

If it is over 9000 characters and, therefore, too big to post inline then attach it to a post

See Read this before posting a programming question

Ok ! It's done

Jaggerwock: Ok ! It's done

+1 karma. However, you seem to have pasted it x 2!

Try putting FastLED.delay(20); at the end of loop(), after FastLED.show();.

Also try a large cap, eg. 470uF or 1000uF across the power lines to the strip.

What type of Arduino are you using?

The FastLed.delay(20) didn’t fix it, and I don’t have any capacitor left for now, I’ll go buy some.
I’m on an Arduino Uno, but I’ve also tried it on a Mega 2560 and the problem was still there.

(you were right, I had pasted it x2, now fixed again )