How to create LEDs objects and method.

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 {
    int i = 100, r = 1;
    int _npix, _e;
    long _colorn;
    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);;
    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() {
  blockWidth = 10;
  for (int i = 0; i < numleds; i++) {
    pixs[i] = new pix();
void loop() {
  if (Serial.available() == 18)   
    xs = ( - 48) * 100;
    xs = xs + ( - 48) * 10;
    xs = xs + - 48;
    ys = ( - 48) * 100;
    ys = ys + ( - 48) * 10;
    ys = ys + - 48;

    r = ( - 48) * 100 ;
    r = r + ( - 48) * 10;
    r = r + - 48;
    g = ( - 48) * 100 ;
    g = g + ( - 48) * 10;
    g = g + - 48;
    b = ( - 48) * 100 ;
    b = b + ( - 48) * 10;
    b = b + - 48;
    //State changer
    e = ( - 48) * 100 ;
    e = e + ( - 48) * 10;
    e = e + - 48;
    int d =;
    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));

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.

Video snip

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** **
** **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 ?

is pix just an individual pixel?

R: Yes, is a WS2812b addressable led strip.

how does it interact with the rest of the pixels ?

R: not yet, they ares individuals.

does it need to exit in itself or is the matrix a better idea ?

R. Cant’t understand it. :S

I saw this in an example:

class cubo{
  int colorCubo;
  void colorFade();
  void colorCambio();
  void colorFixed();
  void init();
void cubo::xychange(){}
void cubo::moveContinously(){}
void cubo::init(){}
cubo* cubos=new cubo[10];
void setup() {
   for (int i=0;i<10;i++){
void loop{


How do I make each of the cubes, for example, move continuously independently of the others?

I've no clue what a "cube" is....

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.

pix class let show every led, only color change can do for now.

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.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);   // 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)

this is what it does...

seems you have a working program...

it's working. As i said, i want to go to the next programming level

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.

you can hand over a reference on your strip object and use it in the class.

The current BlinkLed class takes a reference onto your strip object, and one pixel within the strip handeled by the instance.

Usecase might be a model car, with one long string of Neopixel and different blinking lights (or other effects).

compiles, works and blinks:

  Use pixels in a strip as separate objects
  by noiasca



const byte ledPin = 12;                // Which pin on the Arduino is connected to the NeoPixels?
const uint16_t ledCount = 30;

#include <Adafruit_NeoPixel.h>

Adafruit_NeoPixel strip(ledCount, ledPin, NEO_GRB + NEO_KHZ800);

class BlinkLed {
    byte state = 1;       // 0 off, 1 blink high , 2 blink low
    unsigned long previousMillis;
    uint16_t on = 180;
    uint16_t off = 320;   // 180/320 is according ECE
    uint32_t colorOn = 0xAA0000;
    uint32_t colorOff = 0x000000;
    Adafruit_NeoPixel& obj;
    const byte id;

    BlinkLed(Adafruit_NeoPixel& obj, byte id):

    void set(uint16_t _on, uint16_t _off)  // modify on/off times during runtime
      on = _on;
      off = _off;

    void setState(byte _state)            // 0 switch off blinking; 1 Switch on blinking;
      state = _state;

    void setColor(uint32_t newColorOn, uint32_t newColorOff)
      colorOn = newColorOn;
      colorOff = newColorOff;

    void tick()
      if (state)
        uint32_t currentMillis = millis();
        if (state == 1 && currentMillis - previousMillis >= on) {
          // save the last time you blinked the LED
          obj.setPixelColor(id, colorOn);
          state = 2;
          previousMillis = currentMillis;
        if (state == 2 && currentMillis - previousMillis >= off) {
          // save the last time you blinked the LED
          obj.setPixelColor(id, colorOff);
          state = 1;
          previousMillis = currentMillis;

BlinkLed blinkLedFront(strip, 0);
BlinkLed blinkLedRear(strip, 5);

void setup() {

  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED);            // Turn OFF all pixels ASAP
  strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)

  blinkLedRear.set(900, 800);
  blinkLedRear.setColor(0x0000AA, 0xFF0000);

void loop() {

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.

this is exactly what the setters are doing.

blinkLedRear.setColor(0x0000AA, 0xFF0000);

changes the the two colors during the runtime.
But if you would use a public variable, you could simple assign it by object.publicVariable=value;

Like led*.publiccolor(colorchanger())=variable?*
but that variable must be like variable too...

Like led_.publiccolor(colorchanger())=variable?_
but that variable must be like variable too…
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 ?

I think this is the way...

That’s ONE way (which still requires coding correctly)