Fastled printing to set leds

okay so along side my warp core project i also have this hyrule project.

i have a strip of WS2812B leds with only 16 leds contained within the strip.

now heres basic idea, and it is basic,

0-5 = pink
6-13 = Yellow
14-16 = Green

all static

so i used what ive learnt (sort of) from my warp core thread to throw together a somewhat crude sketch mixed with another code i found online.

the result?

0-5 = white
6-13 = Yellow
14-16 = Red

i have no idea why that happened.

code =

#include "FastLED.h"
#define NUM_LEDS 16
#define DATA_PIN 2
#define BRIGHTNESS 50

CRGB leds[NUM_LEDS];
CRGB myColors[] = {CRGB::HotPink, CRGB::HotPink, CRGB::HotPink, CRGB::HotPink, CRGB::HotPink, CRGB::HotPink, CRGB::Yellow, CRGB::Yellow, CRGB::Yellow, CRGB::Yellow, CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green};
int j = 0;        // Starting color number
byte numColors;   // Will contain the number of colors in the myColors array.

void setup() {
  Serial.begin(115200);
  Serial.println();

  numColors = (sizeof(myColors) / sizeof(myColors[0]));
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  FastLED.setBrightness( BRIGHTNESS );
  FastLED.clear ();
}

void loop() {
  for (int i = 0; i <= NUM_LEDS; i++) {
    leds[i] = myColors[(i + j) % numColors];    // mod 3 means the color number will be 0-2.
  }
  FastLED.show();
}

Oops

please explain instead of just pointing out where the error is, why is this section a problem and what direction do i need to go in with intentions of correcting it?

consider i have much more to learn lol. but hey at least they light up XD

Work through the for loop, and note down the values of i

What values do you need to write to the LEDs to get those 3 colours ?

the colors and order/quantity are setup in the array, so i shouldnt have needed to write any values as remainder of the sketch should just call up the array and print it

So just iterate through the array with a for loop and set the current LED to the corresponding value in the array

No need for any calculations as in

leds[i] = myColors[(i + j) % numColors];

i dont know how to do that? do you mean to set a pixel value within the array or modify the for loop?

as far as i was aware this code would pull up the array when writing to the strip and simply display each CRGB in order from the first (0) pixel

You have an array with 16 values, presumably ne for each LED although the values do not match
0-5 = pink
6-13 = Yellow
14-16 = Green
but put that aside for now

Use a for loop from 0 to 15
Use the for loop value to pick the value at that position in the array and use it again to write the value to the corresponding LED

ok yes my example there should have finished at 15 not 16, but it was only an example there.

the actual code only calls for 0-15 in the array.

i dont get what you mean about the for loop, well i do i just dont get how to do that.

nor do i understand why one segment is acting correctly with the color, all 3 with the quantities but 2 arent writing the correct color.

Until you stop accessing memory you don't own, all bets about how the code will behave are off

sorry what? can you explain this a bit more?

In reply #2, I highlighted a fault with your for loop.
This causes the code in your for loop to write to memory that you don't have any right to read or write

When the variable i has the value 16, you are filling some memory location(s). They do not belong to your leds array and you have the risk that you overwrite something critical.

The order of the colours in the WS2812B might be different from what you told FastLED that it will be (the RGB in below).

FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);

Change RGB in above to e.g. one of the below (GRB will probably do); it's trial and error

	RGB=0012,
	RBG=0021,
	GRB=0102,
	GBR=0120,
	BRG=0201,
	BGR=0210
#include "FastLED.h"
#define NUM_LEDS 16
#define DATA_PIN 2
#define BRIGHTNESS 50

CRGB leds[NUM_LEDS];
CRGB myColors[] = {CRGB::HotPink, CRGB::HotPink, CRGB::HotPink, CRGB::HotPink, CRGB::HotPink, CRGB::HotPink, CRGB::Yellow, CRGB::Yellow, CRGB::Yellow, CRGB::Yellow, CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green, CRGB::Green};

void setup()
{
  Serial.begin(115200);
  Serial.println();
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
  FastLED.setBrightness( BRIGHTNESS );
  FastLED.clear ();
}

void loop()
{
  for (int i = 0; i < NUM_LEDS; i++)
  {
    leds[i] = myColors[i];
  }
  FastLED.show();
}

so i gave this a go, even tinkered with it to see if i needed to make adjustments, however, the result is the same as the previous attempt.

well i got highly fustrated with this, i cant help but feel like there should be a far simpler way to send a color to a specific led in a strip and duplicate that and merely change the led number and color

all these for statements are confusing me and im not really understanding the statements

Arduinos are innoculated against feelings before leaving the factory.
Let us know how it goes.

Take a look at this https://wokwi.com/arduino/projects/323949869399016020
To see it working in the simulation click the green button at the top of the righthand pane

In the code there is a single for loop that sets the colour of each LED in turn and it gets the colour from the myColors array. In your project you can add as many LEDs as you like as long as there is a colour for each one in the myColors array

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