Search code examples
carduino-unofastled

Setting LED value using one integer changes another integer


When I set a led using the int "rite" it changes the int "cnt" and I can't figure out why.

FYI, the code is a modified larsen scanner, it should run a single blip down the string, light up the last LED, a single blip returns to the start and lights the first led, it loops over and over, the ends each getting closer to center with each pass until they meet in the middle

You will see a comment "THIS LINE" about halfway down the shrink function. cnt increments up until (cnt < rite -1) as it should, after that it should decrement until it's greater than (left_end + 1). But cnt changes to 13824 before it starts decrementing.

The code runs fine, if you comment out the line or change the variable to a number, it doesn't look like I want on the LED strip but the cnt doesn't change to 13824. I've included a portion of the serial monitor output, at 20:00:51.387 you can see int change.

Any ideas why?

Thanks

/*  Used to play with LED patterns.    Select functions you want and add them to the Loop function.    9/16/24    */
#include "FastLED.h"
#define NUM_LEDS 20  //   144
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
int cnt = 0, c1 = 150, c2 = 160, v1 = 0, v2 = 75, v3 = 150, v4 = 255, g = NUM_LEDS;

void setup() {
  Serial.begin(115200);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
}

void loop() {
  Serial.println("loop");
  clear();
  shrink();
  //larsen();
}

void clear() {
  while (cnt < NUM_LEDS) {
    leds[(cnt)] = CHSV(c2, 255, v1);
    cnt++;
  }
  cnt = 0;
  FastLED.show();
  Serial.println("CLEARED");
}

void larsen() {
  clear();
  while (cnt < NUM_LEDS - 2) {  //   Larsen Scanner
    leds[(cnt - 3)] = CHSV(c2, 255, v1);
    leds[(cnt - 2)] = CHSV(c2, 255, v2);
    leds[(cnt - 1)] = CHSV(c1, 255, v3);
    leds[(cnt)] = CHSV(c1, 255, v4);
    leds[(cnt + 1)] = CHSV(c1, 255, v3);
    leds[(cnt + 2)] = CHSV(c2, 255, v2);
    leds[(cnt + 3)] = CHSV(c2, 255, v1);
    cnt++;
    FastLED.show();
    delay(8);
  }
  while (cnt > 0) {
    Serial.print("cnt= ");
    Serial.println(cnt);
    leds[(cnt - 3)] = CHSV(c2, 255, v1);
    leds[(cnt - 2)] = CHSV(c2, 255, v2);
    leds[(cnt - 1)] = CHSV(c1, 255, v3);
    leds[(cnt)] = CHSV(c1, 255, v4);
    leds[(cnt + 1)] = CHSV(c1, 255, v3);
    leds[(cnt + 2)] = CHSV(c2, 255, v2);
    leds[(cnt + 3)] = CHSV(c2, 255, v1);
    cnt--;
    FastLED.show();
    delay(8);
  }
}

void bricks() {  //        Stacking Bricks Demo
  clear();
  Serial.println("TOP");
  int c1 = 160, c2 = 160, c3 = 170, b = 0, cnt = 1, g = NUM_LEDS;
  cnt=0;

  while (g > 0) {
    while (cnt < 5) {
      leds[cnt] = CHSV(c1, 255, 255);
      Serial.print("Brick cnt= ");
      Serial.println(cnt);
      cnt++;
      FastLED.show();
    }
    cnt = 0;
    while (cnt < g - 5) {
      Serial.print("cnt1= ");
      Serial.println(cnt);
      Serial.print("g1= ");
      Serial.println(g);
      leds[cnt] = CHSV(c1, 255, 0);
      leds[cnt + 5] = CHSV(c1, 255, 255);
      delay(20);
      FastLED.show();
      cnt++;
      Serial.print("cnt2= ");
      Serial.println(cnt);
      Serial.print("g2= ");
      Serial.println(g);
    }
    delay(20);
    g = g - 5;
    cnt = 0;
  }
}

void shrink(){  
  clear();
  int left_end = 0, rite = NUM_LEDS, test;  
  cnt=0;
  leds[cnt] = CHSV(c1,255,v4);
  FastLED.show(); 
  cnt++;
  left_end++;
  Serial.print("RESET cnt= ");
  Serial.println(cnt);
  Serial.print("left_end= ");
  Serial.println(left_end);
  Serial.print("rite= ");
  Serial.println(rite);

    while ( left_end< rite) {
      while (cnt < rite-1) {
        cnt++;
        leds[cnt-1]=CHSV(c1,255,v1);
        leds[cnt]=CHSV(c1,255,v4);  
        Serial.print("cnt= ");
        Serial.print(cnt); 
        Serial.print(" cnt = on");
        Serial.println(" cnt -1 = off"); 
        FastLED.show(); 
        delay(500);
      } 
        leds[rite]=CHSV(c1,255,v4);   //   ****  This Line  *****
      Serial.print("rite= ");
      Serial.print(rite); 
      Serial.println(" rite = on");
      FastLED.show(); 
      Serial.print("cnt= ");            //   why 13824??
      Serial.println(cnt); 
      rite--;
      while (cnt > left_end+1) {
        cnt--;
        leds[cnt+1] = CHSV(c1,255,v1);
        leds[cnt] = CHSV (c1,255,v4); 
        Serial.print("cnt= ");
        Serial.print(cnt); 
        Serial.print(" cnt+1 = on");
        Serial.println(" cnt = off");
        FastLED.show();   
        delay(500);  
      }
    leds[left_end]=CHSV(c1,255,v4);     
    Serial.print("left_end= ");
    Serial.print(left_end); 
    Serial.println(" left_end = on");
    FastLED.show();   
    delay(500);
    left_end++; 
    }
}

Serial Monitor Output

20:00:50.393 -> cnt= 18 cnt = on cnt -1 = off

20:00:50.888 -> cnt= 19 cnt = on cnt -1 = off

20:00:51.387 -> rite= 20 rite = on

20:00:51.387 -> cnt= 13824

Solution

  • You're running off the end of the array. leds is defined with 20 elements, Those are numbered 0 to 19. When you store into leds[20], that's off the end of the array, and the next thing in memory is cnt.