Problem with FastLED

Hi all,
I am having great fun playing around with a strip of TM1809 leds. I have managed to create some great scenes using mainly white light.
One ‘scene’ is two groups of four leds travelling from each end to the centre of the strip. Works great. I want to play around with the colour though and have hit a problem.
Please see the sketch below with the comments.

//Sketch which has four leds moving from both ends of an led strip to the
//centre, then back to the ends repeatedly.

#include <FastLED.h>

// How many leds are in the strip?
#define NUM_LEDS 56

// Data pin that led data will be written out over
#define DATA_PIN 3

int i;
int j;
int hue;
int d =10;

// Defining the array of leds.  
CRGB leds[NUM_LEDS];

void setup() {
    delay(500);
       FastLED.addLeds<TM1809, DATA_PIN, BRG>(leds, NUM_LEDS);
Serial.begin(9600);
    // randomSeed(analogRead(1));
          
}
void loop() {

hue=150;
  for( i =0; i<= (NUM_LEDS-4)/2; i++)  //Sets up the loop for moving leds to centre
  {
    
    leds[i].setHue( hue);
    j = (NUM_LEDS - i)- 1;  
    leds[j].setHue( hue);              
    leds[i, i+1].setHue( hue);   //sets second led
    leds[j, j-1].setHue( hue);
    leds[i, i+1, i+2].setHue( hue);  // sets third led
    leds[j, j-1, j-2] .setHue( hue);
    leds[i,i+1,1+2,i+3].setHue( hue); //sets fourth led
    leds[j,j-1,j-2,j-3] .setHue( hue);
    FastLED.show();
    leds[i] = CHSV(0,0,0);   // sets led to black
    leds[j] = CHSV(0,0,0);
    FastLED.show();
    //Serial.println( i);
    delay(d);  
  }
  FastLED.clear();
 
   for ( i = (NUM_LEDS-4)/2; i>=0; i--)  // Sets up the loop for returning to edges
  {
    leds[i].setHue( hue);
    j = NUM_LEDS-i;
    leds[j].setHue( hue);                   
    leds[i, i-1].setHue( hue);
    leds[j, j+1] .setHue( hue);
    leds[i, i-1, i-2].setHue( hue);
    leds[j, j+1, j+2] .setHue( hue);
    leds[i,i-1,i-2,i-3].setHue( hue);
    leds[j,j+1,j+2,j+3].setHue( 150); //changing this to 'hue' stops sketch from working
                                      // correctly. Performs the loop once and 'crashes'.
    delay(5);
    FastLED.show();
    leds[i] = CHSV(0,0,0);
    leds[j] = CHSV(0,0,0);
    FastLED.show();
    delay (d);
  }
  FastLED.clear();

}

It took me ages to narrow this down to the error. The sketch works fine with defined hues and different methods of setting the colour/hue. It’s just when I try and set hue to a variable.

Any help would save me from going completely mad!

Cheers

leds is a one dimensional array. You're treating it variously as a one, two, three and four dimensional array. It should not be a surprise that things go wrong as you'll be writing on memory well outside the bounds of the array.

Stick to one index e.g.

leds[i+1].setHue(hue);

Thanks for the quick reply Bill. I understand what you are saying…I think.
I’ve adjusted the code but the same thing is happening.

//Sketch which has four leds moving from both ends of an led strip to the
//centre, then back to the ends.

#include <FastLED.h>

// How many leds are in the strip?
#define NUM_LEDS 56

// Data pin that led data will be written out over
#define DATA_PIN 3

int i;
int j;
int hue;
int d =10;

// Defining the array of leds.  
CRGB leds[NUM_LEDS];

void setup() {
    delay(500);
       FastLED.addLeds<TM1809, DATA_PIN, BRG>(leds, NUM_LEDS);
Serial.begin(9600);
    // randomSeed(analogRead(1));
          
}
void loop() {

hue=150;
  for( i =0; i<= (NUM_LEDS-4)/2; i++)  //Sets up the loop for moving leds to centre
  {
    
    leds[i].setHue( hue);
    j = (NUM_LEDS - i)- 1;  
    leds[j].setHue( hue);              
    leds[i+1].setHue( hue);   //sets second led
    leds[j-1].setHue(hue);
    leds[i+2].setHue( hue);
    leds[j-2].setHue(hue);
    leds[j-3].setHue( hue);
    leds[i+3].setHue( hue); //sets fourth led
    FastLED.show();
    leds[i] = CHSV(0,0,0);   // sets led to black
    leds[j] = CHSV(0,0,0);
    FastLED.show();
    //Serial.println( i);
    delay(d);  
  }
  FastLED.clear();
 
   for ( i = (NUM_LEDS-4)/2; i>=0; i--)  // Sets up the loop for returning to edges
  {
    leds[i].setHue( hue);
    j = NUM_LEDS-i;
    leds[j].setHue( hue);                            
    leds[i-1].setHue( hue);   //sets second led
    leds[j+1].setHue(hue);
    leds[i-2].setHue( hue);
    leds[j+2].setHue(hue);
    leds[j+3].setHue( hue);
    leds[i-3].setHue( hue); //sets fourth led
    delay(5);
    FastLED.show();
    leds[i] = CHSV(0,0,0);
    leds[j] = CHSV(0,0,0);
    FastLED.show();
    delay (d);
  }
  FastLED.clear();

}

The thing is I have used my previous method in long sketches with multiple ‘scenes’ with no problems at all until I try to change the colour or hue with a variable rather than a constant.

Actually, that's not multi-dimensional syntax - my mistake. It's just abuse of the comma operator. The fix is the same though - one index, as you've done.

Bill,

I really can't understand why changing so little in the code... 150 to hue makes so much difference. I've messed about all evening with your idea with no success, this is looking like a weird glitch.

In this section:

   for ( i = (NUM_LEDS-4)/2; i>=0; i--)  // Sets up the loop for returning to edges
  {
    leds[i].setHue( hue);
    j = NUM_LEDS-i;
    leds[j].setHue( hue);                            
    leds[i-1].setHue( hue);   //sets second led
    leds[j+1].setHue(hue);
    leds[i-2].setHue( hue);
    leds[j+2].setHue(hue);
    leds[j+3].setHue( hue);
    leds[i-3].setHue( hue); //sets fourth led
    delay(5);
    FastLED.show();
    leds[i] = CHSV(0,0,0);
    leds[j] = CHSV(0,0,0);
    FastLED.show();
    delay (d);
  }

Your loop counter i will descend to zero. At that point, any statement with a subtraction from i is writing outside the bounds of your array e.g.

    leds[i-1].setHue( hue);   //sets second led

That's causing your crash I suspect.

Thanks again for your reply Bill, much appreciated.
I hadn’t noticed that before about the loop counter…Still doesn’t work though!!!
It’s really weird because the ‘scene’ works fine when I have this: leds[j,j+1,j+2,j+3].setHue( 150);
But it crashes when I put: leds[j,j+1,j+2,j+3].setHue( hue);
Please see my first post and the commented line.
Cheers
Steve

Please post your latest version and note that whatever you think you're doing with lines like:

leds[j,j+1,j+2,j+3].setHue( hue);

It probably isn't doing what you expect.

As to the difference between Hue and 150, if you're still writing outside of your array, all bets are off; behavior will be odd and the fact that you only saw it when you made that change is not really important.

For example, when I was trying to figure out what was going on with your code, I ran the sketch and it performed flawlessly (as far as I could tell without the hardware). But I added a single Serial.print to help debug and it crashed. Illustrating nicely that what's where in memory matters a lot if your code is going on a rampage writing on things it has no business touching.

Bill, I played around with the loop counter, you were quite right....Works perfectly now so now I can change colour with a pot which is what I wanted to do in the first place. Lesson learned, I am new to all this and there is so much to learn for a 60 year old. Thanks again Bill.