1

I'm trying to use Adafruit's LED Backpack library within a custom class. When I use the Adafruit library directly within a sketch, it compiles fine. When I use an example sketch provided with the library, it compiles fine. However when I create an instance of my custom class with the Adafruit object within it, I get the following error:

In file included from sketch_feb18a.ino:6: /Users/lawnmowerlatte/Documents/Arduino/libraries/Bargraph/bargraph.h:18: error: 'Adafruit_24bargraph' does not name a type

All the reasons I've seen for this error stem from the library being missing or otherwise incorrectly installed. However, since I can use it directly, I think something else must be wrong.

Working

#include <Adafruit_LEDBackpack.h>
#include <Adafruit_GFX.h>
Adafruit_24bargraph bar = Adafruit_24bargraph();

void setup() { }
void loop() { }

Not Working

#include <bargraph.h>
Bargraph bargraph = Bargraph("Test", "api", 0);

void setup() { }
void loop() { }

bargraph.h

#ifndef Bargraph_h
#define Bargraph_h

#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

class Bargraph {
  public :
    String name;
    String api;
    Adafruit_24bargraph bar;

    Bargraph(String _name, String _api, int _device,
        String _type="Default");

    void set(String _value);
    void setMax(String _max);
    void update();
    void print();

  private :
    int display[24];

    long max;
    long value;
    int device;
    int size;
    String type;

    void format();
    void write();
};

#endif

bargraph.cpp

#define OFF 0
#define RED 1
#define YELLOW 2
#define GREEN 3

#include <bargraph.h>

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

Bargraph::Bargraph(String _name, String _api, int _device, String _type) : bar() {
    name        = _name;
    api         = _api;
    max         = _max.toLong();
    device      = _device;
    type        = _type;
    value       = 0;

    for (int i=0; i<24; i++) {
        display[i] = OFF;
    }

    bar.begin(0x70+device);
}

**Methods Omitted for Brevity**

Can anyone explain what's going on here? I'm pretty certain my class syntax is correct as I have other object-in-object relationships working. I can post code to GitHub if anyone would like to see more context.

3

I think these lines still need to be in your main sketch file (in addition to being in your custom class file):

#include <Adafruit_LEDBackpack.h>
#include <Adafruit_GFX.h>

If I remember the Arduino IDE quirks correctly, it uses the #include directives in the main sketch file to determine which libraries to actually link. It's one of those annoying features that was intended to be beginner-friendly but sometimes just makes things more confusing.

  • Thanks for the answer. I tried this, but then I get a ton of redefinition and previous definition errors. If I wrap the Adafruit library in ifndef blocks, it works! Do you have any ideas why they wouldn't have put that in? It seems like good practice to do so. – lawnmowerlatte Feb 19 '15 at 20:09
  • @lawnmowerlatte - That's surprising. I can't see any reason that it wouldn't be there already. – Peter Bloomfield Feb 19 '15 at 21:06
0

If you used the serial port, Arduino sometimes causes confusion. Close Arduino IDE and delete files in folder C:/users/[your name]/AppData/Local/Temp/ Its working. Regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.