FastLED library messing with my variable

I'm making an arduino project using an addressable LED strip with a WS2812B chip. I'm using the FastLED library (3.6.0) and here's the shortened version of my code (the rest of the is irrelevant):

// Setup for LEDs
  #include <FastLED.h>

  #define NUM_LEDS 50
  #define DATA_PIN A1
  #define COLOR_ORDER GRB
  #define CHIPSET WS2812B
  #define BRIGHTNESS 50
  #define VOLTS 5
  #define MAX_AMPS 19000

  CRGB leds[NUM_LEDS];

  float two_colors_time = 0;
  int red;
  int green;
  int blue;


void setup(){

  // Setup for LEDs
    FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
    FastLED.setMaxPowerInVoltsAndMilliamps(VOLTS, MAX_AMPS);
    FastLED.setBrightness(BRIGHTNESS);
    FastLED.clear();
    FastLED.show();

  //setup serial
    Serial.begin(9600);
 }

void loop(){

  two_colors(255, 255, 0, 0, 0, 255, 1, 0, NUM_LEDS);

  FastLED.show();
}

void two_colors(int r1, int g1, int b1, int r2, int g2, int b2, float speed, int start, int stop){

  //it's supposed to change smoothly between tow colors defined in the arguments

  float proportion;

  two_colors_time += speed;

  //looping the two_colors_time variable to not overflow 

  if (two_colors_time > 360){two_colors_time = two_colors_time - 360;}
  if (two_colors_time < 0){two_colors_time = two_colors_time + 360;}
  
  for (int i = start; i <= stop; i++){

      proportion = (cos(two_colors_time / 57.29 + 180) + 1) / 2;
      red = round(r1 * proportion + r2 * (1 - proportion));
      green = round(g1 * proportion + g2 * (1 - proportion));
      blue = round(b1 * proportion + b2 * (1 - proportion));
      
      leds[i] = CRGB(red, green, blue);
  }

  Serial.println(two_colors_time);
}

the problem:
two_lights_time variable gets stuck at a weird value, like 0.52 (it's diffrient for every color i choose), but when i remove leds[i] = CRGB(red, green, blue); everything works correctly. The strangest thing is that i use a very simular piece of code for an another function and it works fine. I'm using arduino nano. Is there any explenation for this weird behavior? Is there a way to overcome this problem?

You are indexing off the end of the array.

Should be

two_colors(255, 255, 0, 0, 0, 255, 1, 0, NUM_LEDS-1);

1 Like

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