Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Instantiating and using NeoPixel object within a class

  1. #1
    Junior Member
    Join Date
    Mar 2015
    Posts
    9

    Instantiating and using NeoPixel object within a class

    I am turning my LED clock code into a library so that I can easily drop it into a project and keep the main sketch uncluttered, just exposing the methods that create the clock functionality.

    I am not new to programming or OOP programming, but I am new to C-based programming with it's pointers and strong typing. I am having trouble figuring out how to instantiate an Adafruit_NeoPixel object inside of my class and then use it in other methods. One example:

    MyClock.h
    Code:
    class MyClock
    {
    public:
    	MyClock(byte Outer_Ring_LEDs, byte Outer_Ring_Pin, byte Inner_Ring_LEDs, byte Inner_Ring_Pin);
    	
    private:
    	Adafruit_NeoPixel* _Seconds;
    	Adafruit_NeoPixel* _Minutes;
    	Adafruit_NeoPixel* _Hours;
    };
    MyClock.ccp
    Code:
    GClock::GClock(byte Outer_Ring_LEDs, byte Outer_Ring_Pin, byte Inner_Ring_LEDs, byte Inner_Ring_Pin) {
    	_Seconds = new Adafruit_NeoPixel(Outer_Ring_LEDs, Outer_Ring_Pin, NEO_GRB + NEO_KHZ800);
    	_Minutes = new Adafruit_NeoPixel(Outer_Ring_LEDs, Outer_Ring_Pin, NEO_GRB + NEO_KHZ800);
    	_Hours = new Adafruit_NeoPixel(Inner_Ring_LEDs, Inner_Ring_Pin, NEO_GRB + NEO_KHZ800);
    
    	// initialize the LED rings
    	_Seconds->begin();
    	_Seconds->show();
            // _Minutes->begin();
    	// _Minutes->show();
    	_Hours->begin();
    	_Hours->show();
    }
    When I try to call _Seconds->begin() for example, this error presents:
    Code:
    GClock.cpp:26: undefined reference to `Adafruit_NeoPixel::begin()
    Any suggestions would be appreciated!

    Also, may be a separate issue, may be related, but you can see that I would like to declare two objects for one strip for semantics (Seconds and Minutes). Is this good practice? Would a cloned object/pointer be better? Suggestions?

  2. #2
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,962
    In the current 1.21 you need to include Adafruit_Neopixel.h in your .ino/.pde file. Paul is working on fixes to intuit the libraries needed by other libraries, but for now you need to #include all of the libraries used by your program in the main file.

  3. #3
    Junior Member
    Join Date
    Mar 2015
    Posts
    9
    Quote Originally Posted by MichaelMeissner View Post
    In the current 1.21 you need to include Adafruit_Neopixel.h in your .ino/.pde file. Paul is working on fixes to intuit the libraries needed by other libraries, but for now you need to #include all of the libraries used by your program in the main file.
    I have the neopixel library included as you mention, just didn't include it in the code fragment above.

  4. #4
    Junior Member
    Join Date
    Mar 2015
    Posts
    9
    Quote Originally Posted by garlinto View Post
    I have the neopixel library included as you mention, just didn't include it in the code fragment above.
    Wow, I need to pay better attention! I was on the road earlier when I read the post from MichaelMeissner and I didn't really *get it* until reading it over more carefully just now. I will include the NeoPixel lib in the main sketch and build my class around passing the already instantiated NeoPixel object into it. I'm assuming that this can be done with success? If I have any further questions I'll post them, thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •