Half the code works...the other half doesn't

Good Evening,
I'm using An Arduino Nano with a 5 Volt; 1 Amp Power supply. I have 2 WS2812B LED strips (18 LED's each) for a Total of 36 LED's. The LED strips each have a capacitor (220uF I think) at the 5V/Grd terminals and and 220 Ohlm resistors at the LED pins on the nano

My code has 6 options controlled by a simple button...The first 3 options create random flashing lights either in white, or rainbow colors. THESE SEEM TO WORK FINE.

The last three options (Constant white, Constant blue and a fading rainbow) DO NOT WORK...for these I get a few LED's dimly lit in random colors. they do not change. When I cycle through these options using the push button this does not change. Once I get back to the first option everything works fine again for the first 3 options; Then back to dim, random LED's that don't change.

#include <FastLED.h>
#include <OneButton.h>

#define NUM_LEDS_PER_STRIP 18
#define BTN_PIN 10

CRGB leds[NUM_LEDS_PER_STRIP];
CRGB ledsa[NUM_LEDS_PER_STRIP];

uint8_t paletteIndex = 0;
uint8_t patternCounter = 0;
uint8_t hue = 0;

OneButton btn = OneButton(BTN_PIN, true, true);

DEFINE_GRADIENT_PALETTE(BlueWhite){
  0, 175, 219, 252,
  255, 132, 141, 237
};

DEFINE_GRADIENT_PALETTE(Sunset_Real_gp){
  0, 120, 0, 0,
  22, 179, 22, 0,
  51, 255, 104, 0,
  85, 167, 22, 18,
  135, 100, 0, 103,
  198, 16, 0, 130,
  255, 0, 0, 160
};

DEFINE_GRADIENT_PALETTE(rainbow_gp){
  0, 57, 0, 31,
  1, 54, 0, 34,
  2, 51, 0, 36,
  3, 48, 0, 38,
  4, 46, 0, 41,
  5, 43, 0, 43,
  6, 40, 0, 46,
  7, 38, 0, 49,
  8, 35, 0, 52,
  9, 33, 0, 54,
  10, 31, 0, 57,
  11, 29, 0, 61,
  12, 27, 0, 64,
  13, 25, 0, 67,
  14, 23, 0, 71,
  15, 21, 0, 74,
  16, 20, 0, 78,
  17, 18, 0, 82,
  18, 17, 0, 85,
  19, 15, 0, 89,
  20, 14, 0, 93,
  21, 12, 0, 98,
  22, 11, 0, 102,
  23, 10, 0, 106,
  24, 9, 0, 111,
  25, 8, 0, 115,
  26, 7, 0, 120,
  27, 6, 0, 125,
  28, 5, 0, 130,
  29, 5, 0, 135,
  30, 4, 0, 140,
  31, 3, 0, 145,
  32, 3, 0, 151,
  33, 2, 0, 156,
  34, 2, 0, 162,
  35, 1, 0, 168,
  36, 1, 0, 174,
  05, 255, 17, 0,
  206, 255, 15, 0,
  207, 255, 13, 0,
  208, 255, 11, 0,
  209, 255, 10, 0,
  210, 255, 8, 0,
  211, 255, 7, 0,
  212, 255, 6, 0,
  213, 255, 5, 0,
  214, 255, 4, 0,
  215, 255, 3, 0,
  216, 255, 2, 0,
  217, 255, 1, 0,
  218, 255, 1, 0,
  219, 255, 1, 0,
  220, 255, 1, 0,
  221, 255, 1, 0,
  222, 255, 1, 0,
  223, 255, 1, 0,
  224, 255, 0, 0,
  225, 242, 0, 0,
  226, 229, 0, 0,
  227, 217, 0, 0,
  228, 206, 0, 0,
  229, 194, 0, 0,
  230, 184, 0, 0,
  231, 173, 0, 0,
  232, 163, 0, 0,
  233, 153, 0, 0,
  234, 144, 0, 0,
  235, 135, 0, 0,
  236, 126, 0, 0,
  237, 118, 0, 0,
  238, 110, 0, 0,
  239, 103, 0, 0,
  240, 95, 0, 0,
  241, 88, 0, 0,
  242, 82, 0, 0,
  243, 75, 0, 0,
  244, 69, 0, 0,
  245, 64, 0, 0,
  246, 58, 0, 0,
  247, 53, 0, 0,
  248, 48, 0, 0,
  249, 44, 0, 0,
  250, 39, 0, 0,
  251, 35, 0, 0,
  252, 32, 0, 0,
  253, 28, 0, 0,
  255, 25, 0, 0
};

CRGBPalette16 nextPal = Rainbow_gp;

CRGBPalette16 myPal = Sunset_Real_gp;

CRGBPalette16 Pal = BlueWhite;

void setup() {
  FastLED.addLeds<WS2812B, 4>(leds, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 6>(ledsa, NUM_LEDS_PER_STRIP);

  FastLED.setBrightness(255);

  btn.attachClick(nextPattern);
}

void loop() {

  switch (patternCounter) {
    case 0:
      BlueWhiteFlash();
      break;
    case 1:
      SunsetRealFlash();
      break;
    case 2:
      RainbowFlash();
      break;
case3:
      ColorFade();
      break;
case4:
      WhiteLight();
      break;
case5:
      BlueLight();
  }

  FastLED.show();
  btn.tick();
}

void nextPattern() {
  patternCounter = (patternCounter + 1) % 6;  //Number after %, Equals total of pattern
}

void BlueWhiteFlash() {

  EVERY_N_MILLISECONDS(75) {
    //Switch on an LED at random, choosing a random color from the palette
    leds[random8(0, NUM_LEDS_PER_STRIP - 1)] = ColorFromPalette(Pal, random8(), 255, LINEARBLEND);
    ledsa[random8(0, NUM_LEDS_PER_STRIP - 1)] = ColorFromPalette(Pal, random8(), 255, LINEARBLEND);
  }
  // Fade all LEDs down by X in brightness each time this is called
  fadeToBlackBy(leds, NUM_LEDS_PER_STRIP, 2);
  fadeToBlackBy(ledsa, NUM_LEDS_PER_STRIP, 2);
}

void SunsetRealFlash() {
  EVERY_N_MILLISECONDS(75) {
    //Switch on an LED at random, choosing a random color from the palette
    leds[random8(0, NUM_LEDS_PER_STRIP - 1)] = ColorFromPalette(myPal, random8(), 255, LINEARBLEND);
    ledsa[random8(0, NUM_LEDS_PER_STRIP - 1)] = ColorFromPalette(myPal, random8(), 255, LINEARBLEND);
  }

  // Fade all LEDs down by 1 in brightness each time this is called
  fadeToBlackBy(leds, NUM_LEDS_PER_STRIP, 2);
  fadeToBlackBy(ledsa, NUM_LEDS_PER_STRIP, 2);
}

void RainbowFlash() {

  EVERY_N_MILLISECONDS(75) {
    //Switch on an LED at random, choosing a random color from the palette
    leds[random8(0, NUM_LEDS_PER_STRIP - 1)] = ColorFromPalette(nextPal, random8(), 255, LINEARBLEND);
    ledsa[random8(0, NUM_LEDS_PER_STRIP - 1)] = ColorFromPalette(nextPal, random8(), 255, LINEARBLEND);
  }

  // Fade all LEDs down by 1 in brightness each time this is called
  fadeToBlackBy(leds, NUM_LEDS_PER_STRIP, 7);
  fadeToBlackBy(ledsa, NUM_LEDS_PER_STRIP, 7);
}

void ColorFade() {

  for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
    leds[i] = CHSV(hue, 255, 255);
    ledsa[i] = CHSV(hue, 255, 255);
  }
  EVERY_N_MILLISECONDS(200) {
    hue++;
  }
}

void WhiteLight() {

  for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
    leds[i] = CHSV(100, 0, 255);
    ledsa[i] = CHSV(100, 0, 255);
  }
}

void BlueLight() {

  for (int i = 0; i < NUM_LEDS_PER_STRIP; i++) {
    leds[i] = CHSV(160, 128, 255);
    ledsa[i] = CHSV(160, 128, 255);
  }
}

I don't think it's a wiring issue given the first 3 cases work as expected. This leads me to wonder if it's an Amp issue... 36 LED's at full white run ~2.1 amps assuming 60mA/LED; and I have a 1Amp plug
BUT
I tried changing the FastLED.setBrightness from 255 down to 50 and nothing changes.
I tried changing the brightness component in the CHSV's of each of the last 3 cases to 50 again with no change.
I tried doing both together w/ no change.

I'm at a loss...I even redid the circuit with a new button and nano just in case...
Any thoughts would be appreciated.

Sam

I need to make a correction...The Capacitors are 100uF, not 220.
The LED's are: LOAMLIN WS2812B Individually Addressable RGB LED Strip 16.4FT 5050SMD for what it's worth

Sam

Please post a wiring diagram and a link to the exact LED strips you purchased.

As a rough estimate, 36 LEDs consuming 20 mA each require 0.72 Amperes. If the power supply is overrated by the seller, it might just shut down.

Here's the link: https://a.co/d/ayL24LM
Diagram to follow

Sam

You need at the least a 2 amp power supply if they are all going to be lit at the same time.
Just to get the other ones working, can you disconnect the ones that work. Then run the code again.

I have a variable power supply and set it @ 5 Volts and anywhere from 2 to 3 Amps and the problem still exists...

I think your case statement for those 3 not working states is incorrect. The numbers have to be separated from the "case" statement.

//Still disconnect the working ones and try and isolate what is not working. If that is doable.

Good eye!

Those fly because syntactically they're valid labels, targets for goto statements at least.

a7

I was wondering how that got by the compiler.

Oh if it's that simple I'll be a happy man.
Thanks and I'll let you know.
Sam

Just fixed the three case statements putting a space between case and the # and lo & behold it works :slight_smile:
I am surprised that the format check didn't flag that???

Thanks to all and especially noweare for seeing such a small thing.

Sam

It maybe might have warned you, but what you first wrote is totally valid code, even if it didn't do what you wanted.

C/C++ is like that. There is a presumption that you know what you are doing, and you have to go out of your way to have the compiler,er warn you about (some) things that it thinks mightn't be what you meant.

Make sure you have warnings and verbosity and anything like that cranked up in the IDE.

If you didn't before, set those prefs and replace the error and see if you get any red ink.

a7

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