Trouble with library scope

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:


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

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.

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

Try adding this to the top of your code

#pragma SPARK_NO_PREPROCESSOR
#include "application.h"

and function prototypes if needed.

Sometimes the preprocessor plays up.

No joy…

Now it thinks there is an error in the library…

screenshot: https://goo.gl/photos/AcP2J8ska8va6uwFAw

@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:

#define PIXEL_TYPE WS2812B

:wink:

1 Like

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.

@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);

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

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…

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

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);
1 Like

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.

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.

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:

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

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)

2 Likes

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.

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);
}

@marmageek, I believe the issue is with your placement of the ‘*’ ! Adafruit_NeoPixel *unit should be Adafruit_NeoPixel* unit I believe :wink: