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...
