Class in class problem Neopixel

Hi there,
I'm new to programming C but eager to learn and of course trying to make it as nice as possible. So working on an aquarium script with temp, pH, several relais and light

The temp and pH work nicely.
As I want to integrate one light update function with different scenes I created a class:

Light.h
Light.cpp

usinig Neopixel. But then I got stuck.. I have a pointer in Light.h for Adafruit_NeoPixel but I get all the complains about pixels which is not a member of class..

can anyone help me how to solve this?

/* Light.h */
#ifndef LIGHT_H
#define LIGHT_H
#include "Arduino.h"
#include <Adafruit_NeoPixel.h>

class Light {
  public:
    Light(int number, int iopin, String type, int scene, int r, int g, int b);

    void  update(); // Update light

    int number;
    int iopin;
    String type;
    int scene;
    int r;
    int g;
    int b;
    void init_led();
    Adafruit_NeoPixel     *adaneo; //??
  private:
};

#endif /* LIGHT_H*/

And the Light.cpp file

#include "Light.h"
Light::Light(int number, int iopin, String type, int scene, int r, int g, int b) {
  this->number = number;
  this->iopin = iopin;
  this->type = type;
  this->scene = scene;
  this->r = r;
  this->g = g;
  this->b = b;
  this->init_led();
}

void Light::update() {
  String out;
  if (this->scene == SCENE_CUSTOM) {
    out += ("Light params:\n");
    out += ("- Nr LEDS: " + String(this->number) + "\n");
    out += ("- IOpin: " + String(this->iopin) + "\n");
    out += ("- Type: " + String(this->type) + "\n");
    out += ("- Scene: " + String(this->scene) + "\n");
    out += ("- Color r: " + String(this->r) + ":");
    out += ("/ Color g: " + String(this->g) + ":");
    out += ("/ Color b: " + String(this->b) + "\n");
    pixels.setPixelColor(this->number, pixels.Color(this->r, this->g, this->b));
    pixels.show();   // Send the updated pixel colors to the hardware
  }
  Serial.println(out);
}

void Light::init_led() {
// Adafruit_NeoPixel pixels(this->number, this->iopin, NEO_GRB + NEO_KHZ800);
  this->adaneo = new Adafruit_NeoPixel pixels(this->number, this->iopin, NEO_GRB + NEO_KHZ800);
  Serial.print("init led");
  pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  pixels.clear();
}

pixels?

Did you mean adaneo?

(Also means you need to fix those . to be -> )

Well I can't use Adafruit_neoPixel I have to have a pointer right?

But the compiler complains about

this->adaneo = new Adafruit_NeoPixel pixels(this->number, this->iopin, NEO_GRB + NEO_KHZ800);
pixels.begin();
pixels.clear();

all the pixel statements in above
I tried the adaneo pointer as I don't really understand how to solve this and used some examples to see if I could fix it :stuck_out_tongue:

pixels->begin();

gives me the same error that pixels is not a member of class Light
which is true it belongs to Neopixel

@robbert1977, your topic was moved to a more suitable location on the forum.

1 Like

Nope.

:stuck_out_tongue: any idea how to solve it so I can use the Neopixel calls in my Light class?

Clue, right there

Not exactly as the compiler expects a ; before pixels in following line:

this->adaneo = new Adafruit_NeoPixel pixels(this->number, this->iopin, NEO_GRB + NEO_KHZ800);

Light.cpp:52:40: error: expected ';' before 'pixels'
this->adaneo = new Adafruit_NeoPixel pixels(this->number, this->iopin, NEO_GRB + NEO_KHZ800);
exit status 1
expected ';' before 'pixels'

I tried (desperate) removing all pixels and using adaneo instead.. no compile errors.. no action either (if I set a color with the following code)

this->adaneo->setPixelColor(this->number, this->adaneo->Color(this->r, this->g, this->b));
this->adaneo->show();

Can't see your code.

I changed the earlier post with the code

And now nothing makes sense, so I'm out.

thnx anyway maybe someone else can help me with this

Which earlier post?

Why is it so difficult to provide a simple, linear narrative?

just made it readable for you :slight_smile:

Not exactly as the compiler expects a ; before pixels in following line:

this->adaneo = new Adafruit_NeoPixel pixels(this->number, this->iopin, NEO_GRB + NEO_KHZ800);

Light.cpp:52:40: error: expected ';' before 'pixels'
this->adaneo = new Adafruit_NeoPixel pixels(this->number, this->iopin, NEO_GRB + NEO_KHZ800);
exit status 1
expected ';' before 'pixels'

I tried (desperate) removing all pixels and using adaneo instead.. no compile errors.. no action either (if I set a color with the following code)

this->adaneo->setPixelColor(this->number, this->adaneo->Color(this->r, this->g, this->b));
this->adaneo->show();

A quick reminder for you.

Adafruit_NeoPixel     *adaneo; 

adaneo is a pointer to an object of type Adafruit_NeoPixel.

because I'm stupid probably

You point me into a direction of a clue...

But my question is quite simple...

How can I use the Neopixel statements in my own class??

so how can I use :slight_smile:

Adafruit_NeoPixel pixels(this->number, this->iopin, NEO_GRB + NEO_KHZ800);
pixels.begin();
pixels.clear();
this->adaneo->Adafruit_NeoPixel pixels(this->number, this->iopin, NEO_GRB + NEO_KHZ800);
pixels.begin();
pixels.clear();

in my class without the errors of pixel

Have you tried omitting your chosen name "pixels"?

There's nothing inherent in the class called "pixels"

I didn't choose pixels it comes from the Adafruit_Neopixel library.
the following comes straight out of their example

Adafruit_NeoPixel pixels(this->number, this->iopin, NEO_GRB + NEO_KHZ800);
pixels.begin();
pixels.clear();

No, pixels is an object of type Adafruit_NeoPixel, in other words an instance of the class Adafruit_NeoPixel

"new Adafruit_NeoPixel" returns a pointer to an object of type Adafruit_NeoPixel

Welcome,

Try

adaneo = new Adafruit_NeoPixel( number, iopin, NEO_GRB + NEO_KHZ800 );

then you can use

adaneo->begin();
adaneo->clear();

Someone had basically the same request recently, see here for a more complete example : Setting up an OLED display instance in a library - #2 by guix