I want to control 343 WS2811 LEDs via the FastLED library. The LED Colors are stored in a CRGB matrix that contains the r, g and b value for every led. 24bit per led. That means If I want to change the color of the leds what an animation needs to I have to set values in this matrix. I already made several animations that can access this matrix because they are also in the LEDCube class. My complete Code looks like this:
I would not recommend looking at all functions of LEDCube because there is nothing doing that but I am posting the whole code that you have an overview of my Code and how it works.
/*
LEDCube.h
*/
#ifndef LEDCube_H
#define LEDCube_H
#include "Arduino.h"
#include <FastLED.h>
#include <Adafruit_NeoPixel.h>
#include "colorgnt.h"
#include "rainbowgnt.h"
#define NUMLEDS 343
typedef struct xy8bit {
short x;
short y;
};
class LEDCube
{
protected:
static const int limit = 7;
static const int limitsqu = limit * limit;
static const int numLeds = limitsqu * limit;
public:
LEDCube() {
FastLED.addLeds<WS2811, 7>(leds, 343);
FastLED.setBrightness(255);
FastLED.clear();
};
int xyz2lednum(uint8_t x, uint8_t y, uint8_t z);
void setLED(int lednumber, uint8_t r, uint8_t g, uint8_t b);
void led(uint8_t x, uint8_t y, uint8_t z, uint8_t r, uint8_t g, uint8_t b);
void setslice(char a, uint8_t pos, uint8_t r, uint8_t g, uint8_t b);
void test(int wait);
void ranColor(uint8_t r, uint8_t g, uint8_t b, uint8_t maxpercoffset, uint8_t *rgb);
void sticks(int wait1, int wait2, boolean normalgravity, uint8_t r, uint8_t g, uint8_t b, uint8_t perc);
void sticksi(int wait1, int wait2, boolean normalgravity, uint8_t r, uint8_t g, uint8_t b, uint8_t perc);
void ani1();
void fade(uint8_t startr, uint8_t startg, uint8_t startb, uint8_t endr, uint8_t endg, uint8_t endb, int wait, uint8_t step, int lednumber);
void fademulti(uint8_t startr, uint8_t startg, uint8_t startb, uint8_t endr, uint8_t endg, uint8_t endb, int wait, uint8_t step, int quantity, int ledmultitest[]);
void clearLEDs();
void setall(uint8_t r, uint8_t g, uint8_t b);
void loadLetter(char letter, xy8bit *setxy);
int textxy2lednum(xy8bit xy);
void displayHG(uint8_t r1, uint8_t g1, uint8_t b1, uint8_t r2, uint8_t g2, uint8_t b2, uint8_t wait, int cycles);
void testprogress();
void rendergradient(colorgnt &gradient, int cycles, char a, uint8_t wait);
short LEDCube::cut24(short num);
Adafruit_NeoPixel simulated = Adafruit_NeoPixel(1, 3, NEO_GRB + NEO_KHZ800);
CRGB leds[343];
};
#endif
/*LEDCube.ino*/
#include <FastLED.h>
#include <Adafruit_NeoPixel.h>
#include "LEDCube.h"
#define DATA_PIN 7
Adafruit_NeoPixel simulated = Adafruit_NeoPixel(1, 3, NEO_GRB + NEO_KHZ800);
void setup() {
delay(1000);
Serial.begin(9600);
}
static const int limit = 7;
static const int limitsqu = limit * limit;
static const int numLeds = limitsqu * limit;
LEDCube cube;
rainbowgnt gnt1;
rainbowgnt &gnt1ref = gnt1;
void loop() {
cube.ani1();
cube.clearLEDs();
cube.sticks(40, 60, true, 255, 255, 255, 20);
for (int i = 0; i < 21; i++) {
cube.setall(255, 255, 255);
delay(100);
}
cube.clearLEDs();
cube.rendergradient(gnt1, 256, 'x', 5);
cube.rendergradient(gnt1, 256, 'y', 5);
cube.rendergradient(gnt1, 500, 'z', 5);
cube.clearLEDs();
cube.displayHG(255,125,10,255,10,125,200, 100);
for (int i = 0; i < limit; i++) {
cube.setslice('x', i, 255, 0, 0);
FastLED.show();
delay(100);
}
for (int i = 0; i < limit; i++) {
cube.setslice('y', i, 0, 255, 0);
FastLED.show();
delay(100);
}
for (int i = 0; i < limit; i++) {
cube.setslice('z', i, 0, 0, 255);
FastLED.show();
delay(100);
}
cube.sticksi(28, 0, false, 0, 0, 0, 0);
}
All .cpp files combined:
#include "Arduino.h"
#include "LEDCube.h"
void LEDCube::clearLEDs() {
for (int i = 0; i < numLeds; i++) {
setLED(i, 0, 0, 0);
}
FastLED.show();
}
void LEDCube::setall(uint8_t r, uint8_t g, uint8_t b) {
for (int i = 0; i < numLeds; i++) {
setLED(i, r, g, b);
}
FastLED.show();
}
void LEDCube::setslice(char a, uint8_t pos, uint8_t r, uint8_t g, uint8_t b) {
switch (a) {
case 'x':
for (int z = 0; z < limit; z++) {
for (int y = 0; y < limit; y++) {
led(pos, y, z, r, g, b);
}
}
break;
case 'y':
for (int z = 0; z < limit; z++) {
for (int x = 0; x < limit; x++) {
led(x, pos, z, r, g, b);
}
}
break;
case 'z':
for (int x = 0; x < limit; x++) {
for (int y = 0; y < limit; y++) {
led(x, y, pos, r, g, b);
}
}
break;
}
}
/*
colorgnt.h
*/
#ifndef colorgnt_H
#define colorgnt_H
#include "LEDCube.h"
typedef struct rgb8bit {
uint8_t r;
uint8_t g;
uint8_t b;
};
class colorgnt {
public:
colorgnt() {}
virtual rgb8bit rgb(uint8_t position) {}
};
#endif
/*
rainbowgnt.h
*/
#ifndef rainbowgnt_H
#define rainbowgnt_H
#include "colorgnt.h"
#include "LEDCube.h"
class colorgnt; //is this neccessary?
class rainbowgnt: public colorgnt {
public:
rainbowgnt() {}
virtual rgb8bit rgb(uint8_t position) {
rgb8bit rgb;
position = 255 - position;
if (position < 85) {
rgb.r = 255 - position * 3;
rgb.g = 0;
rgb.b = position * 3;
return rgb;
}
if (position < 170) {
position -= 85;
rgb.r = 0;
rgb.g = position * 3;
rgb.b = 255 - position * 3;
return rgb;
}
position -= 170;
rgb.r = position * 3;
rgb.g = 255 - position * 3;
rgb.b = 0;
return rgb;
}
};
#endif
I had to leave some functions because of the character limitation out.
The member functions of LEDCube are using each other to create animations. The last step of these animations is the function
void setLED(int lednumber, uint8_t r, uint8_t g, uint8_t b);
this function changes values in the CRGB 'leds' matrix.
To display the animations I am creating one instance of LEDCube in the .ino file and calling the public member functions of the LEDCube instance.
I want to make an animation that lets random LEDs light up and then fade to black. These LEDs should not fade all at the same time. Therefor I would make a class(lets call it ledclass) that represents one LED with member functions handling the fading and lighting up and other stuff needed therefor. To create the animation I would create in an member function of LEDCube an array of instances of ledclass.
My problem and reason why I created this topic is that one member function of ledclass has to change values in the CRGB 'leds' matrix and I ask how a member of ledclass can access/change the CRGB 'leds' matrix that is declared in LEDCube.