# Trouble with library scope

**URL:** <https://community.particle.io/t/trouble-with-library-scope/18081>\
**Category:** Libraries\
**Created:** [December 7, 2015, 4:33pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081 "2015-12-07T16:33:00Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![marmageek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marmageek/32/27325_2.png) [@marmageek](https://community.particle.io/u/marmageek)\
**Post date:** [December 7, 2015, 4:33pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/1 "2015-12-07T16:33:00Z")

</div>

I am using the NEOPIXEL library. I am getting a bunch of errors saying that _Adafuit\_NeoPixel was not declared in this scope._

I have included the first few lines of code here:

```auto

// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"
#include <stdint.h>

// DEFINITIONS
#define PIN D6
#define PIXELS 150
#define TYPE WS2812B
#define FRAME_DELAY 33 // framerate = 30 fps
#define MOODS 5
#define MOOD_TIMER FRAME_DELAY*

// GLOBALS
Adafruit_NeoPixel strip = new Adafruit_NeoPixel(PIXELS, PIN, TYPE);
byte mood; // 0=Jon, 1=Dana, 2=Devin, 3=Kolbe, 4=Kyra
unsigned int tick=0;
unsigned int moodTick=0;

// HELPERS
// Colorwheel implemented 0-255 hue.
uint32_t wheel (Adafruit_NeoPixel* unit, uint8_t pos) {
    if(pos<85) return unit->Color(pos*3, 255-pos*3, 0);
    else if (pos<170) {
        pos -= 85;
        return unit->Color(255-pos*3, 0, pos*3);
    }
    else {
        pos -=170;
        return unit->Color(0, pos*3, 255-pos*3);
    }
}

```

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [December 7, 2015, 4:34pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/2 "2015-12-07T16:34:14Z")

</div>

Have you included the library through the library interface or have you only inserted that code? If you haven’t done the former, please do, since that’s what’s required to get it working. The libraries you’ve included should be on the left pane of the IDE.

---

<div class="post-metadata">

**Author:** ![marmageek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marmageek/32/27325_2.png) [@marmageek](https://community.particle.io/u/marmageek)\
**Post date:** [December 7, 2015, 4:35pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/3 "2015-12-07T16:35:31Z")

</div>

I used the GUI to include the lib.

(although I added the \<stdint.h\> one as I was getting errors on unitX\_t as well.)

screenshot: [https://goo.gl/photos/YXMWKdYADFfMNbWG7](https://goo.gl/photos/YXMWKdYADFfMNbWG7)

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 7, 2015, 4:46pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/4 "2015-12-07T16:46:40Z")

</div>

Try adding this to the top of your code

```cpp
#pragma SPARK_NO_PREPROCESSOR
#include "application.h"

```

and function prototypes if needed.

Sometimes the preprocessor plays up.

---

<div class="post-metadata">

**Author:** ![marmageek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marmageek/32/27325_2.png) [@marmageek](https://community.particle.io/u/marmageek)\
**Post date:** [December 7, 2015, 8:06pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/5 "2015-12-07T20:06:45Z")

</div>

No joy…

Now it thinks there is an error in the library…

screenshot: [https://goo.gl/photos/AcP2J8ska8va6uwFAw](https://goo.gl/photos/AcP2J8ska8va6uwFAw)

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 7, 2015, 8:17pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/6 "2015-12-07T20:17:53Z")

</div>

@marmageek, get rid of the “new” in the Adafruit\_Neopixel constructor call on line 17. Also, look at line 11 where the pixel type is defined. It SHOULD read:

```auto
#define PIXEL_TYPE WS2812B

```

😉

---

<div class="post-metadata">

**Author:** ![marmageek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marmageek/32/27325_2.png) [@marmageek](https://community.particle.io/u/marmageek)\
**Post date:** [December 7, 2015, 8:47pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/7 "2015-12-07T20:47:30Z")

</div>

TYPE is defined as WS2812B already.

Why can’t I call the constructor with ‘new’?

I have dozens of NeoPixle projects and the all work in this manner.

There has to be some typo somewhere screwing up the preprocessor.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 7, 2015, 8:50pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/8 "2015-12-07T20:50:02Z")

</div>

@marmageek, I used the included Neopixel example as a template. You cast the pixel type as “TYPE” instead of PIXEL\_TYPE. Look at the very first error in the list!

---

<div class="post-metadata">

**Author:** ![marmageek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marmageek/32/27325_2.png) [@marmageek](https://community.particle.io/u/marmageek)\
**Post date:** [December 7, 2015, 9:06pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/9 "2015-12-07T21:06:56Z")

</div>

> [@peekay123](#):
>
> @marmageek, I used the included Neopixel example as a template. You cast the pixel type as "TYPE" instead of PIXEL\_TYPE. Look at the very first error in the list!

I do not understand what you are trying to say. I defined (not 'cast') TYPE as WS2812B and I use TYPE in the constructor. The fact that the demo defines PIXEL\_TYPE is irrelevant.

```
// DEFINITIONS
#define PIN D6
#define PIXELS 150
#define TYPE WS2812B
#define FRAME_DELAY 33 // framerate = 30 fps
#define MOODS 5
#define MOOD_TIMER 1980 // 60 s

// GLOBALS
Adafruit_NeoPixel strip = new Adafruit_NeoPixel(PIXELS, PIN, TYPE);

```

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 7, 2015, 9:11pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/10 "2015-12-07T21:11:29Z")

</div>

@marmageek, my brain is in park and I’m misfiring on some cylinders! You are correct. I’ll have to test compile once I get home. Can you compile one of the supplied neopixel examples without errors?

---

<div class="post-metadata">

**Author:** ![marmageek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marmageek/32/27325_2.png) [@marmageek](https://community.particle.io/u/marmageek)\
**Post date:** [December 7, 2015, 9:12pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/11 "2015-12-07T21:12:27Z")

</div>

More information…

If I scroll a bit further down in the error list there is an error in `Adafruit_NeoPixel::Adafruit_NeoPixel()` so it is not getting defined…

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/8/82ce32304a6d0a3c9b5b84afbc96b5463bd70c64.png)

---

<div class="post-metadata">

**Author:** ![marmageek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marmageek/32/27325_2.png) [@marmageek](https://community.particle.io/u/marmageek)\
**Post date:** [December 7, 2015, 9:18pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/12 "2015-12-07T21:18:11Z")

</div>

@peekay123, I just compiled one of my other projects and it ‘validates’ just fine.

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/e/e345a99b6de495d70e4e1644fdefc77a67cf50f2.png)

---

<div class="post-metadata">

**Author:** ![dougal](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dougal/32/20713_2.png) [@dougal](https://community.particle.io/u/dougal)\
**Post date:** [December 7, 2015, 9:45pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/13 "2015-12-07T21:45:37Z")

</div>

> [@marmageek](#):
>
> Why can't I call the constructor with 'new'?

Double-check your declarations. You use `new` to create/instantiate an object which can then be assigned to a pointer variable. But if you just declare an object of the class's type directly, you don't use `new`. For example:

```
Adafruit_NeoPixel *strip1; // strip1 is a pointer, but not initialized yet
strip1 = new Adafruit_NeoPixel(PIXELS, PIN, TYPE);
// ...or... (notice: _not_ an explicit pointer declaration)
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(PIXELS, PIN, TYPE);

```

---

<div class="post-metadata">

**Author:** ![marmageek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marmageek/32/27325_2.png) [@marmageek](https://community.particle.io/u/marmageek)\
**Post date:** [December 7, 2015, 9:55pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/14 "2015-12-07T21:55:15Z")

</div>

Has Java killed that much of my C++ knowledge?

Removing the `new` operator killed the library error (for some reason); however, Still cannot use `Adafruit_NeoPixel*` to define a pointer.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 7, 2015, 10:03pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/15 "2015-12-07T22:03:41Z")

</div>

> [@peekay123](#):
>
> @marmageek, get rid of the "new" in the Adafruit\_Neopixel

This was the reason for what @peekay123 said earlier.

It's not for some reason, but for the reason that the `new` operator returns a pointer so you'd need a pointer type variable to assign it to.

---

<div class="post-metadata">

**Author:** ![dougal](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dougal/32/20713_2.png) [@dougal](https://community.particle.io/u/dougal)\
**Post date:** [December 7, 2015, 10:03pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/16 "2015-12-07T22:03:47Z")

</div>

Pointers are so much fun, right?

Not sure what problem you’re having with declaring one, though… I use them in my own class to pass a strip reference around:

> <https://github.com/dougalcampbell/eyeblink/blob/master/eyeblink.h#L45>

Though, I’m not using `new` in my case, so I can’t say that I’ve actually tried it that way…

---

<div class="post-metadata">

**Author:** ![dougal](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/dougal/32/20713_2.png) [@dougal](https://community.particle.io/u/dougal)\
**Post date:** [December 7, 2015, 10:11pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/17 "2015-12-07T22:11:18Z")

</div>

I just did a quick test. Here’s a minimal example, which seems to compile fine in the Cloud IDE:

```
// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"

// DEFINITIONS
#define PIN D6
#define PIXELS 150
#define TYPE WS2812B

// GLOBALS
Adafruit_NeoPixel* strip1 = new Adafruit_NeoPixel(PIXELS, PIN, TYPE);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(PIXELS, PIN, TYPE);

void setup() {
    strip1->show();
    (*strip1).show(); // dereferenced pointer
    strip2.show();
    (&strip2)->show(); // pointerize reference
}

```

(oops, accidentally replied to @ScruffR instead of @marmageek)

---

<div class="post-metadata">

**Author:** ![marmageek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marmageek/32/27325_2.png) [@marmageek](https://community.particle.io/u/marmageek)\
**Post date:** [December 7, 2015, 10:40pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/18 "2015-12-07T22:40:24Z")

</div>

Went back to my trusty C++ Pocket reference.

According to that one should do as follows:  
`
Adafruit_NeoPixel *strip1 = new Adafruit_NeoPixel(PIXELS, PIN, TYPE);
Adafruit_NeoPixel strip2(PIXELS, PIN, TYPE);
…
Adafruit_NeoPixel *strip3 = &strip2;
…
`

strip1 is allocated off the stack.  
strip2 is allocated out of code\_segment  
strip3 is stack pointer

I have a feeling though that my error lies elsewhere. I will post my entire code (current state) here in a bit.

---

<div class="post-metadata">

**Author:** ![marmageek](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/marmageek/32/27325_2.png) [@marmageek](https://community.particle.io/u/marmageek)\
**Post date:** [December 7, 2015, 10:46pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/19 "2015-12-07T22:46:33Z")

</div>

I am still getting these errors: `xmastree-01.cpp:3:16: error: ‘Adafruit_NeoPixel’ was not declared in this scope uint32_t wheel(Adafruit_NeoPixel *unit, uint8_t pos);
`

```
// This #include statement was automatically added by the Particle IDE.
#include "neopixel/neopixel.h"

// DEFINITIONS
#define PIN D6
#define PIXELS 150
#define PTYPE WS2812B
#define FRAME_DELAY 33 // framerate = 30 fps
#define MOODS 5
#define MOOD_TIMER 1980 // 60 s

// GLOBALS
//Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN, PTYPE);
Adafruit_NeoPixel strip(PIXELS, PIN, PTYPE);
uint8_t mood; // 0=Jon, 1=Dana, 2=Devin, 3=Kolbe, 4=Kyra
unsigned int tick=0;
unsigned int moodTick=0;

// HELPERS
// Colorwheel implemented 0-255 hue.
uint32_t wheel(Adafruit_NeoPixel *unit, uint8_t pos) {
    if(pos<85) return unit->Color(pos*3, 255-pos*3, 0);
    else if (pos<170) {
        pos -= 85;
        return unit->Color(255-pos*3, 0, pos*3);
    }
    else {
        pos -=170;
        return unit->Color(0, pos*3, 255-pos*3);
    }
}
// Fill the dots one after the other with a color
void wipe(Adafruit_NeoPixel *unit, uint32_t c, uint8_t wait){
    for (int i=0; i<unit->numPixels(); i++){
        unit->setPixelColor(i,c);
        unit->show();
        delay(wait);
    }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(Adafruit_NeoPixel *unit, uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< unit->numPixels(); i++) {
      unit->setPixelColor(i, wheel(unit, ((i * 256 / unit->numPixels()) + j) & 255));
    }
    unit->show();
    delay(wait);
  }
}

// Mood Funcitons We will run a mood for MOOD_TIMER ticks (i.e., frames).
// Mood Inicitalizers
void (*initMoods[])() ={
    initJonsMood,
    initDanasMood,
    initDevinsMood,
    initKolbesMood,
    initKyrasMood };
    
// Moods[] contains an array of function pointers to each mood.
void (*Moods[])() = {
    jonsMood,
    danasMood,
    devinsMood,
    kolbesMood,
    kyrasMood };
    
// Jon's Mood
void initJonsMood(){
    
}
void jonsMood() {
    rainbow();
}
// Dana's Mood
void initDanasMood(){
    
}
void danasMood() {
    rainbow();
}

// Devin's Mood
void initDevinsMood(){
    
}
void devinsMood() {
    rainbow();
}

// Kolbe's Mood
void initKolbesMood(){
    
}
void kolbesMood() {
    rainbow();
}

// Kyra's Mood
void initKyrasMood(){
    
}
void kyrasMood() {
    rainbow();
}

// Rainbow mood
void initRainbow(){
    
}
void rainbow() {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, wheel(&strip, (i+j) & 255));
    }
    strip.show();
    delay(FRAME_DELAY);
  }
}

// MAIN

void setup() {
    strip.begin();
    strip.show();
}

void loop() {
  if (++tick%MOOD_TIMER == 0) (*initMoods[++moodTick%MOODS])();
  
  // disptach mood
  (*Moods[moodTick%MOODS])();
  delay(FRAME_DELAY);
}
```

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 7, 2015, 11:44pm UTC](https://community.particle.io/t/trouble-with-library-scope/18081/20 "2015-12-07T23:44:09Z")

</div>

> [@marmageek](#):
>
> I am still getting these errors: xmastree-01.cpp:3:16: error: 'Adafruit\_NeoPixel' was not declared in this scope uint32\_t wheel(Adafruit\_NeoPixel \*unit, uint8\_t pos);

@marmageek, I believe the issue is with your placement of the '\*' ! `Adafruit_NeoPixel *unit` should be `Adafruit_NeoPixel* unit` I believe 😉

[Next page](https://community.particle.io/t/trouble-with-library-scope/18081.md?page=2)
