0

I'm using an addressable 150 led strip and an Arduino Nano to control it, using FastLed library.

I'm tring do create a function turn_on_leds, which gets 4 parameters:

1) delay time for lighting a led in the loop, 2) direction ( not relevant for now), 3) color index for colors array , 4) brightness percentage.

Problem #1: a color array is defined to select from a preset definitions of colors (Fastled reference, but when selecting col_indx =2, meaning colors[2], leds are not lit in Red as they should, AND when setting leds[i]=0xFF0000, the right result is set.

Problem #2: at loop there is a for intended for raise bightness from 0 to 100, BUT the results are 0 for first iteration and 9 for all others.

Appreciate any HELP!!

Guy

Code:

#include <FastLED.h>

#define NUM_LEDS 10
#define BRIGHTNESS  55
#define DATA_PIN 7

char *colors[] = {0x000000,0xFFFFFF,0xFF0000,0x008000,0x0000FF}; // black, white,r,g,b
CRGB leds[NUM_LEDS];
int t = 0;

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  //  FastLED.setBrightness(BRIGHTNESS);
}


void turn_leds_on(int del_1, bool dir_1, int col_indx, int bright_1) {
  if (dir_1 == true ) { // start to end
    for (int i = 0; i <= NUM_LEDS; i++) {
      leds[i] = 0xFF0000;//colors[col_indx];
      FastLED.setBrightness(bright_1*255/100);
      FastLED.show();
      delay(del_1);
    }
  }
}


void loop() {
  Serial.println(t);
  turn_leds_on(50, true, 2, 100);
  delay(1000);
  //  turn_leds_on(50, true, 0, t);
  //  delay(1000);

  if (t < 100 ) {
    t += 10;
  }
  else {
    t = 0;
  }
} 

1 Answer 1

1

Your colors array should be CRGB type, not char *.

CRGB colors[] = {0x000000,0xFFFFFF,0xFF0000,0x008000,0x0000FF}; // black, white,r,g,b

For your second problem - you are overflowing the bounds of your LEDs array:

for (int i = 0; i <= NUM_LEDS; i++) {
  leds[i] = 0xFF0000;//colors[col_indx];

You are going from 0 to 10 inclusive. That's 11 iterations. There is no such array slice as leds[10], so the value you write to it ends up overwriting the next two bytes in memory - and those happen to be occupied by the variable t.

3
  • thank you. that solved problem 1, what about 2 ?
    – guyd
    Feb 22, 2019 at 13:17
  • 1
    @Guy.D You are overflowing the bounds of an array.
    – Majenko
    Feb 22, 2019 at 13:35
  • OMG , you are so RIGHT. ThankYou!
    – guyd
    Feb 22, 2019 at 13:42

Your Answer

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

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