6

I'm new to Arduino coding and FastLED. When it comes to coding, I'm a complete novice, however, I've been watching YouTube tutorials to help my knowledge. I have successfully run my code on Wokwi, however, when I upload my code to my Arduino, it uploads without error, yet the WS2812B doesn't run the code correctly and appears to freeze after the first pattern.

The LED strip works just run with my Adafruit code, running a very basic set of patterns I created (different than the below code).

Any help would be greatly appreciated! The code below is what I wrote up in Wokwi and the simulation works flawlessly. The only change when uploading to my Arduino is: NUM_LEDS 135

#include <FastLED.h>
#define NUM_LEDS  16
#define LED_PIN   5
#define BRIGHTNESS  255
#define caseCount 7
CRGB source1[NUM_LEDS];
CRGB source2[NUM_LEDS];
CRGB output[NUM_LEDS];

DEFINE_GRADIENT_PALETTE (christmasBlue_gp) {
  0, 0, 0, 255,
  64, 0, 0, 135,
  127, 255, 255, 255,
  191, 0, 0, 135,
  255, 0, 0, 255
};
CRGBPalette16 bluePal = christmasBlue_gp;

uint8_t blendAmount = 0;
uint8_t patternCounter = 0;
uint8_t source1Pattern = 0;
uint8_t source2Pattern = 1;
bool useSource1 = false;

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(output, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop() {

  EVERY_N_MILLISECONDS(5) {
    blend(source1, source2, output, NUM_LEDS, blendAmount);
    if (useSource1) {
      if (blendAmount < 255) blendAmount++;
      } else {
      if (blendAmount > 0) blendAmount--;
      }
  }

  EVERY_N_SECONDS(5) {
    nextPattern();
  }
  runPattern(source1Pattern, source1);
  runPattern(source2Pattern, source2);
  FastLED.show();
}

void nextPattern() {
  patternCounter = (patternCounter + 1) % caseCount;
  if (useSource1) source1Pattern = patternCounter;
  else source2Pattern = patternCounter;
  useSource1 = !useSource1;
  }

void runPattern(uint8_t pattern, CRGB *LEDArray) {
  switch (pattern) {
    case 0:
      movingDots(LEDArray);
      break;
    case 1:
      whiteFill(LEDArray);
      break;
    case 2:
      greenFill(LEDArray);
      break;
    case 3:
      whiteFill(LEDArray);
      break;
    case 4:
      redFill(LEDArray);
      break;
    case 5:
      greenBlueTwinkle(LEDArray);
      break;
    case 6:
      greenRedSplit(LEDArray);
      break;
  }
}

void movingDots(CRGB *LEDarray) {
  uint16_t posBeat = beatsin16(30, 0, NUM_LEDS -1, 0, 0);
  uint16_t posBeat2 = beatsin16(60, 0, NUM_LEDS -1, 0, 0);
  uint16_t posBeat3 = beatsin16(25, 0, NUM_LEDS -1, 0, 127);
  uint16_t posBeat4 = beatsin16(45, 0, NUM_LEDS -1, 0, 127);
  LEDarray[(posBeat + posBeat2) / 2] = CHSV(0, 255, 255);
  LEDarray[(posBeat3 + posBeat4) / 2] = CHSV(120, 255, 255);
  LEDarray[(posBeat3 + posBeat2) / 2] = CHSV(120, 255, 255);
  LEDarray[(posBeat2 + posBeat4) / 2] = CHSV(0, 255, 255);
  LEDarray[(posBeat2 + posBeat4 + posBeat) / 3] = CHSV(0, 0, 255);
  fadeToBlackBy(LEDarray, NUM_LEDS, 1);
}

void whiteFill(CRGB *LEDarray) {
  fill_solid(LEDarray, NUM_LEDS, CRGB::White);
}

void greenFill(CRGB *LEDarray) {
  fill_solid(LEDarray, NUM_LEDS, CRGB::Green);
}

void redFill(CRGB *LEDarray) {
  fill_solid(LEDarray, NUM_LEDS, CRGB::Red);
}

void greenBlueTwinkle(CRGB *LEDarray) {
  EVERY_N_MILLISECONDS(50){
    LEDarray[random8(0, NUM_LEDS-1)] = ColorFromPalette(bluePal, random8(), 255, LINEARBLEND);
    }
  fadeToBlackBy(LEDarray, NUM_LEDS, 1);
  FastLED.show();
}

void greenRedSplit(CRGB *LEDarray) {
  for(int i = 0; i < NUM_LEDS; i++) {
    LEDarray[i] = CHSV(0, 255, 255);
    LEDarray[(NUM_LEDS-1-i)] = CHSV(120, 255, 255);
    FastLED.show();
    delay(100);
    LEDarray[i] = CHSV(0, 255, 255);
  }

  for(int i = NUM_LEDS - 1; i >= 0; i--) {
    LEDarray[i] = CHSV(120, 255, 255);
    LEDarray[(NUM_LEDS-1-i)] = CHSV(0, 255, 255);
    FastLED.show();
    delay(100);
    LEDarray[i] = CHSV(120, 255, 255);
  }
}
4
  • Does it work with, say, NUM_LEDS 15? Could well be a memory problem.
    – ocrdu
    Commented Dec 10, 2020 at 13:53
  • Good question and suggestion. I'll have to try it when I get home, I'm at work and my Chromebook doesn't support Arduino's IDE.
    – Joey W
    Commented Dec 10, 2020 at 14:02
  • It might also be that your power supply is not powerful enough (see @st2000's answer). You could try reducing the brightness to reduce power consumption and see if that makes it work more reliably.
    – StarCat
    Commented Dec 10, 2020 at 16:16
  • I reduced brightness to 100, which didn't solve the issue. However, I broke the code down more. Ocrdu's suggestion of a memory issue might have been the issue. I removed 3 parts from the blend, so their is only 4 cases running on all 135 LEDs. My "void greenRedSplit(CRGB *LEDarray)" code appears to have been freezing the Arduino anytime it "ran".
    – Joey W
    Commented Dec 10, 2020 at 17:23

1 Answer 1

3

Consider what would happen if your power supply is not powerful enough. Depending on the number of WS2812B you are lighting at the same time you may be exceeding the current rating of your power source. Using the following quote from this source:

... consider how much current your LED strip is going to pull. With every single LED on at full brightness, each breakout board can pull about 60mA (20mA per color channel).

We can calculate for a string of 15 WS2812 set to full brightness it would take 15 x 60mA or almost 1 Amp of power. People commonly source power supply which can handle twice the expected high current load. So, in this case you would need a supply which can source at least 2 Amps

Also consider not using the Arduino's linear voltage regulator for large current loads. Not only does it have current limitations but it is also not as effective as a switching power supply. Instead use a power supply of the correct voltage and capable current rating for large current loads.

Try lighting only 1 LED at a time in the strip and stick to one color at a time. If this works it is likely your power supply is not capable of supplying the necessary current to operate your project as expected.

3
  • Great thought since I would have overlooked this, had it not been for the videos I watched. I made sure I had enough power to run the strip on full bright white prior to the coding.
    – Joey W
    Commented Dec 10, 2020 at 17:27
  • Another thought - you should avoid usig the Arduino's linear power regulator to power large loads. Not only does it have current limitations but it is also not as effective as a switching power supply.
    – st2000
    Commented Dec 10, 2020 at 23:18
  • 1
    It would be very telling if you performed the test described in this answer. Either result would be interesting. If it works or if it does not work.
    – st2000
    Commented Dec 10, 2020 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.