I have been trying to make an array of LEDs have individual behavior using classes, but I really believe that I have not done well.
This is what i have in pix.h:
//#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel pixels(60, 4, NEO_GRB + NEO_KHZ800);
class pix {
private:
int i = 100, r = 1;
int _npix, _e;
long _colorn;
public:
pixx() {}
void disp(int npix, int e, long colorn) {
_npix = npix;
_e = e;
_colorn = colorn;
if (e == 0) {
_colorn = colorn;
}
if (e == 1) {
_colorn = pixels.Color(colorr(), 0, 0); //fade red color
}
pixels.setPixelColor(_npix, _colorn);
pixels.show();
}
int colorr() {//trying to fade a color
i = i + r;
if (i >= 255 || i <= 10) {
r *= -1;
}
return i;
}
};
This reads data sent from processing3 and color every LED...
#include <Adafruit_NeoPixel.h>
#include "pix.h";
#define PIN 4
#define NUMPIXELS 60
int numleds = 60;
int blockWidth;
int xs = 0, ys = 0, zs = 0;
byte r = 255, g = 255, b = 0, e = 0;
int npix;
pix *pixs[60];
void setup() {
Serial.begin(1000000);
delay(100);
blockWidth = 10;
pixels.begin();
pixels.clear();
delay(100);
for (int i = 0; i < numleds; i++) {
pixs[i] = new pix();
}
}
void loop() {
if (Serial.available() == 18)
{
xs = (Serial.read() - 48) * 100;
xs = xs + (Serial.read() - 48) * 10;
xs = xs + Serial.read() - 48;
ys = (Serial.read() - 48) * 100;
ys = ys + (Serial.read() - 48) * 10;
ys = ys + Serial.read() - 48;
r = (Serial.read() - 48) * 100 ;
r = r + (Serial.read() - 48) * 10;
r = r + Serial.read() - 48;
g = (Serial.read() - 48) * 100 ;
g = g + (Serial.read() - 48) * 10;
g = g + Serial.read() - 48;
b = (Serial.read() - 48) * 100 ;
b = b + (Serial.read() - 48) * 10;
b = b + Serial.read() - 48;
//State changer
e = (Serial.read() - 48) * 100 ;
e = e + (Serial.read() - 48) * 10;
e = e + Serial.read() - 48;
int d = Serial.read();
Serial.write(0);
delay(50);
if (ys % 2 == 0) //solving snake type led strip format
npix = xs + 12 * ys;
if (ys & 0x01)
npix = 11 + 12 * ys - xs;
for (int i = 0; i < numleds; i++) {
pixs[i]->disp(npix, e, pixels.Color(r, g, b));
}
}
Serial.write(1);
}
How can I make each LED fade (or blink) depending on the state (e) 0, 1, ...?
I've not looked at your class and not sure why it's useful since you have the Adafruit_NeoPixel class encapsulating your led array.
I noticed though that your Serial line is open at 1 meg bauds. That's pretty fast, you receive 100,000 bytes per second --> ie 100 bytes per millisecond.
I don't know how quickly the Processing program on your PC streams information to the Serial line, but a PC is fast... so if it keeps dumping stuff there, you'll have a super fast influx of bytes.
Your code if (Serial.available() == 18) awaits for exactly 18 bytes to be available which might never happen or from time to time, you would be better off checking with >= 18
The other challenge is that the Serial buffer on your Arduino is only 64 bytes long. When you receive your 18 bytes, you execute some code which will take some time but worse you have a delay(50); in there.
in 50ms (assuming Processing keeps streaming) you'll have possibly received 5000 bytes, so you'll be missing out tons of data...
The communication between the arduino and processing3 is very good (almost 99.99%), before doing the trick Serial.write (0) and (1), to allow the serial process, everything was a disaster.
But then I'll take care of polishing the subject of memory and baud.
What I want is to learn to do the classes and do the methods. I am reading all I can but this of the classes is already another level of programming.
frankly spoken, before you step into classes, consider to recap your variables and variable types.
#define PIN 4
#define NUMPIXELS 60
int numleds = 60;
int blockWidth;
int xs = 0, ys = 0, zs = 0;
Why do you need twice 60 in your code, even three times if we take your pix.h into account also?
Use const variables instead of precompiler defines,
Check your variable types. Each int is suspected, that you didn't think about the needed range of number before.
int ... does it need to have 2 bytes? does it need to be an unsigned type?
Check your code for local variables and all the for loops also.
What I want is to learn to do the classes and do the methods. I am reading all I can but this of the classes is already another level of programming.
have you started with simple examples of class and class hierarchies ?
Like creating a ** **Vehicle** **
class having attributes (number of wheels, motor size, energy type, consumption, number of passengers) and subclasses such as ** **Car** **
and ** **Truck** **
with their own attributes (or Animal with Mammals and Birds subclasses, Mammals could have Herbivore (Horse, Cow, Sheep) and Carnivore (Lions, Wofle) and Omnivore (Dog, Man) subclasses...
That will get you to understand how to design a class and its attributes and the syntax and typical mechanisms for instantiating and deleting objects.
As I said, I'm not sure what your class above is about. what abstraction does it represent ? is pix just an individual pixel ? how does it interact with the rest of the pixels ? does it need to exit in itself or is the matrix a better idea ?
My question was, what is the Pix class doing, what's its purpose in life ? pixels are already directly addressable through the Adafruit_NeoPixel class which represents your full strip.
robertoxyz20:
pix class let show every led, only color change can do for now.
I still don't get it. the Adafruit_NeoPixel will let you address every led individually to set its color and manage strip update.
that code for exemple should turn all the LEDs in some sort of mid bright red.
#include <Adafruit_NeoPixel.h>
const uint8_t PIN = 4;
const uint8_t NUMPIXELS = 60;
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
void setup()
{
pixels.begin();
pixels.clear(); // Set all pixel colors to 'off'
for (int i = 0; i < NUMPIXELS; i++) // For each pixel...
pixels.setPixelColor(i, pixels.Color(0, 150, 0)); // some sort of red (GRB)
pixels.show(); // Send the updated pixel colors to the hardware.
}
void loop() {}
are your LEDs in a strip or they are all independent, with each their own control pin ? (which does not show in your code above)
OK - do you want to rewrite the full Adafruit_NeoPixel class or do you want to just have a class abstracting one pixel in a Adafruit_NeoPixel strip, identified for example by its index in the strip ?
if you want to shoot for the latter, then A "Pix" would need a reference to the Strip it belongs to and its index in the strip as instance variables.
you could define a number of methods in your Pix class that basically just issue a similar call to pixel at the index portion in the strip (setPixelColor, show, ...)
I would not really call that "the next level" though as I don't see any win in that abstraction.
I need to know how to change a variable within the class and that that variable can fit in the loop execution time.
I have understood the blink example but it has no variable that can change.
robertoxyz20:
Like led_.publiccolor(colorchanger())=variable?_ but that variable must be like variable too... [/quote] why wouldn't variable be just as you say, a variable reference? all your questions seems to show you are somewhat confused with some basic considerations. Are you at ease with what's a class, a method, a member variable, public or private attributes, what's an instance, what's a variable and associated scope, memory allocation, etc ?