[SOLVED]VERY strange behaviour of matrix. Data corruption of matrix?

I am currently programming my 7³RGB LEDCube and therefor I have several classes. The WS2811 LEDs are driven by the FastLED library. This library requires a CRGB matrix of the size of the number of the total leds. For my cube 343.
So I did:

CRGB leds[numLeds]; //numLeds = static const int = 343

To change the color of the LEDs I have to change the rgb values of the CRGB 'leds' matrix.
I have a function called setLED which has a int lednumber and rgb values as arguments. This function sets these values in the 'leds' CRGB matrix:

void utilities::setLED(int lednumber, uint8_t r, uint8_t g, uint8_t b) {
  if (lednumber >= 0) {
    leds[lednumber].r = r;
    leds[lednumber].g = g;
    leds[lednumber].b = b;
  }
}

For testing purposes I am executing this function in a for loop which counts up until 343:

for (int i = 0; i < utils->numLeds; i++) {
      utils->setLED(i, 255, 255, 255);
      utils->setLED(i - 1, 0, 0, 0);
      FastLED.show();
      delay(wait);
}

This creates a single point(led) travelling from the first led to the last.

The cube displays what he sould but not the last 49 LEDs. They light up in random colors. 49 LEDs was the maximum until now but the number of LEDs with wrong colors varies.

I added Serial Output to the function 'setLED'. This Output prints the arguments "r,g and b as SetRGB" (what it should set in the matrix) and what the real value in the matrix after the setting is. They both should be exactly the same.
The Output looks like I was expecting it to, random rgb values but why?!

Lednumber: 294 Set RGB: (R:0 G:0 B:0) RGB in Matrix: (R:0 G:0 B:0)
Lednumber: 296 Set RGB: (R:255 G:255 B:255) RGB in Matrix: (R:255 G:255 B:255)
Lednumber: 295 Set RGB: (R:0 G:0 B:0) RGB in Matrix: (R:0 G:0 B:0)
Lednumber: 297 Set RGB: (R:255 G:255 B:255) RGB in Matrix: (R:255 G:255 B:255)
Lednumber: 296 Set RGB: (R:0 G:0 B:0) RGB in Matrix: (R:0 G:0 B:0)
Lednumber: 298 Set RGB: (R:255 G:255 B:255) RGB in Matrix: (R:255 G:255 B:255) //OK UNTIL HERE BECAUS BOTH RGB VALUES ARE THE SAME
Lednumber: 297 Set RGB: (R:0 G:0 B:0) RGB in Matrix: (R:0 G:0 B:0)
Lednumber: 299 Set RGB: (R:255 G:255 B:255) RGB in Matrix: (R:255 G:255 B:0) //Two RGB are not the same
Lednumber: 298 Set RGB: (R:0 G:0 B:0) RGB in Matrix: (R:0 G:0 B:0)
Lednumber: 300 Set RGB: (R:255 G:255 B:255) RGB in Matrix: (R:7 G:175 B:6)  //random RGB values in the Matrix
Lednumber: 299 Set RGB: (R:0 G:0 B:0) RGB in Matrix: (R:0 G:0 B:0)
Lednumber: 301 Set RGB: (R:255 G:255 B:255) RGB in Matrix: (R:156 G:2 B:182)
Lednumber: 300 Set RGB: (R:0 G:0 B:0) RGB in Matrix: (R:7 G:175 B:6)
Lednumber: 302 Set RGB: (R:255 G:255 B:255) RGB in Matrix: (R:25 G:48 B:33)
Lednumber: 301 Set RGB: (R:0 G:0 B:0) RGB in Matrix: (R:156 G:2 B:182)
Lednumber: 303 Set RGB: (R:255 G:255 B:255) RGB in Matrix: (R:53 G:0 B:1)

I used this function in a previous version of this code without any problems. I reorganized the code and organized everything a bit with classes.

I think a relevant change is the scope of the 'leds' matrix:
If the mistake is not in the posted code, I can also post the whole code.
I explain the scope/way of the 'leds' CRGB matrix to the 'setLED' function in the code below:

/*
utilities.h
*/

#include "Arduino.h"
#include "FastLED.h"
#ifndef UTILITIES_H
#define UTILITIES_H

class utilities {
  public:
  static const int limit = 7;
  static const int limitsqu = limit * limit;
  static const int numLeds = limitsqu * limit;
  //OTHER MEMBERS
  utilities() {
	FastLED.addLeds<WS2811, 7>(leds, 343);
	FastLED.setBrightness(255);
	FastLED.clear();
  }

  private:
  CRGB leds[numLeds];  //<--------HERE DOES IT START
};
#endif

Then a instance of utilities is created in the class LEDCube:

/*
LEDCube2.h
*/

#include "Arduino.h"
#include "utilities.h"
#include "anitilities.h"
#include "test.h"
#include "FastLED.h"
#ifndef LEDCUBE
#define LEDCUBE

class LEDCube {
  public:
  utilities *utils;  //HERE
  anitilities *anitils;
  LEDCube() {
	utilities utilsp;
	utils = &utilsp;  //AND HERE
	anitilities anitilsp(utils);
	anitils = &anitilsp;
  };
  
  void displaytest(uint8_t wait) {
	test test(wait);
	test.init(/* here ->*/utils/* <- here */,anitils);  //IT IS PASSED HERE TO THE CLASS THAT EXECUTES/CALLS 'setLED'
	test.render();
  }
};
#endif

The class I talked about above:

/*
test.h
*/
#include "Arduino.h"
#include "FastLED.h"
#ifndef rainbowtest_H
#define rainbowtest_H
class test {
  public:
  test(uint8_t waitp) {
	wait = waitp;
  };
  
  void init(utilities *utilsp, anitilities *anitilsp) {  //HERE DOES IT COME FROM CLASS "LEDCube"
	anitils = anitilsp; //AND PASSED TO THE CLASS
	utils = utilsp;  //BECUASE IT WOULD NOT BE IN THE SCOPE OF 'virtual void render()'
  };
  
  virtual void render() {
	for (int i = 0; i < utils->numLeds; i++) {
      utils->setLED(i, 255, 255, 255);  //GETS EXECUTED
      utils->setLED(i - 1, 0, 0, 0);
      FastLED.show();
      delay(wait);
	}
  }
  private:
  uint8_t wait;
  anitilities *anitils;  //HERE
  utilities *utils;  //for passing to the class
};
#endif

Thanks in Advance,
I am not understanding anything anymore...

What board are you loading this code on? After allocating space for 323 LEDs, how much memory do you still have available?

for (int i = 0; i < utils->numLeds; i++) {
  utils->setLED(i, 255, 255, 255);
  utils->setLED(i - 1, 0, 0, 0); //0 - 1 = -1 = out of bounds = weird stuff may happen :)
  FastLED.show();
  delay(wait);
}

PaulS:
What board are you loading this code on? After allocating space for 323 LEDs, how much memory do you still have available?

Arduino Mega, It does not show how much I still have available because this matrix is not global but 343 * 3Bytes are 1029Bytes so 7kB free RAM.

Danois90:

for (int i = 0; i < utils->numLeds; i++) {

utils->setLED(i, 255, 255, 255);
  utils->setLED(i - 1, 0, 0, 0); //0 - 1 = -1 = out of bounds = weird stuff may happen :slight_smile:
  FastLED.show();
  delay(wait);
}

I removed this line, did not solve the issue, still weird stuff happening with the last LEDs

I can see that the out of bounds issue is already handled in setLED, but this implies an unnecessary if statement for each LED, but OK! I am wondering, your cube has 7x7 pixels for each side? And the cube itself has 6 sides, so where is the last side going? 776 = 294 LED's and not 343 LED's..?

Danois90:
I can see that the out of bounds issue is already handled in setLED, but this implies an unnecessary if statement for each LED, but OK! I am wondering, your cube has 7x7 pixels for each side? And the cube itself has 6 sides, so where is the last side going? 776 = 294 LED's and not 343 LED's..?

The Cube also has inner LEDs. 7 * 7 * 7 = 343

EDIT: The Code and all the stuff already worked with several animations. I am just organizing the animations in classes.

Can you provide a link to that cube? How many inner LED's per side?

Danois90:
Can you provide a link to that cube? How many inner LED's per side?

I built it myself. I do not have a link. It is like this Cube just 7 * 7 * 7

  LEDCube() {
  utilities utilsp;
  utils = &utilsp;  //AND HERE
  anitilities anitilsp(utils);
  anitils = &anitilsp;
  };

You can't do this. utilsp and anitilsp are local variables that are allocated on the stack and are only valid within the function (in this case, a constructor). Once the function finishes, these variables disappear. You are trying to cheat by copying their addresses into a pointer, but that can't work and will only corrupt the stack.

If you want to allocate dynamic memory for them then use the new operator.

I get that you want to restructure your code to make it classier (pardon the pun) but I'm not sure you are going about it the right way. Perhaps you should post your original working code and fish for suggestions on how it could be restructured to achieve the specific purpose that you have in mind - you need to state the requirements.

arduarn:

  LEDCube() {

utilities utilsp;
  utils = &utilsp;  //AND HERE
  anitilities anitilsp(utils);
  anitils = &anitilsp;
  };




You can't do this. *utilsp* and *anitilsp* are local variables that are allocated on the stack and are only valid within the function (in this case, a constructor). Once the function finishes, these variables disappear. You are trying to cheat by copying their addresses into a pointer, but that can't work and will only corrupt the stack.

If you want to allocate dynamic memory for them then use the **new** operator.

I get that you want to restructure your code to make it classier (pardon the pun) but I'm not sure you are going about it the right way. Perhaps you should post your original working code and fish for suggestions on how it could be restructured to achieve the specific purpose that you have in mind - you need to state the requirements.

Thank you so much. It works now.

I will post the original code and what I want to do/did in a extra post in this topic.