Go Down

Topic: Help libraries (Read 1 time) previous topic - next topic

IgnorantCarb0n

Hi im trying to run a simple example code and it seems i misunderstand how to import libraries

i did Sketch>Import Library> Add Library ( pointed to folder named ADXL345 within which is ADXL345.h)
then sketch>import library>ADXL345

here is my code:

Quote

#include <ADXL345.h>

#include <Wire.h>


const float alpha = 0.5;

double fXg = 0;
double fYg = 0;
double fZg = 0;

ADXL345 acc;

void setup()
{
   acc.begin();
   Serial.begin(9600);
   delay(100);
}

void loop()
{
   double pitch, roll, Xg, Yg, Zg;
   acc.read(&Xg, &Yg, &Zg);

   //Low Pass Filter to smooth out data
   fXg = Xg * alpha + (fXg * (1.0 - alpha));
   fYg = Yg * alpha + (fYg * (1.0 - alpha));
   fZg = Zg * alpha + (fZg * (1.0 - alpha));

   //Roll and Pitch Equations
   roll  = (atan2(-fYg, fZg)*180.0)/M_PI;
   pitch = (atan2(fXg, sqrt(fYg*fYg + fZg*fZg))*180.0)/M_PI;

   Serial.print(pitch);
   Serial.print("   ");
   Serial.println(roll);

   delay(50);
}




and error message:

Quote
sketch_nov20a.cpp.o: In function `__static_initialization_and_destruction_0':
C:\Program Files (x86)\Arduino/sketch_nov20a.ino:12: undefined reference to `ADXL345::ADXL345()'
sketch_nov20a.cpp.o: In function `loop':
C:\Program Files (x86)\Arduino/sketch_nov20a.ino:24: undefined reference to `ADXL345::read(double*, double*, double*)'
sketch_nov20a.cpp.o: In function `setup':
C:\Program Files (x86)\Arduino/sketch_nov20a.ino:16: undefined reference to `ADXL345::begin()'

majenko

A library should consist of (at minimum) a header file and a .cpp file, both named the same (yes, you get some badly written libraries that are pure headers ... urgh).

The files should be in a folder also named the same, and that folder should be in the "libraries" folder within your sketchbook.

So you should have
libraries\ADXL345\ADXL345.h
libraries\ADXL345\ADXL345.cpp
... etc

There may be other files too.

Once the files are in the right place the IDE should find them ok.  It sounds like it's not finding the .cpp file.

IgnorantCarb0n

Hi, thanks for your reply, i copied the library files (.cpp and .h) in text form from the net, i have now placed them in the libraries folder and no change, still the same error

majenko

Then you may have copied them wrong.  Also, you may need to restart the IDE after putting the files in.

Where did you copy the files from?

IgnorantCarb0n

#4
Nov 20, 2013, 07:53 pm Last Edit: Nov 20, 2013, 08:05 pm by IgnorantCarb0n Reason: 1
https://github.com/Anilm3/ADXL345-Accelerometer/tree/master/Arduino

and yes i restarted the ide, but i still get the same error

thanks for your help btw

majenko

Extract this Zip file into your libraries folder (or use the Add library option on the Zip file itself) and try again.

pito

It compiles fine here (uecide) with the zipped libs..
Code: [Select]
Program Size:
  Flash: 7% (10090 bytes out of 130048 bytes max)
    RAM: 701 bytes
         (text: 9952, data: 138, bss: 563)

majenko


It compiles fine here (uecide) with the zipped libs..
Code: [Select]
Program Size:
  Flash: 7% (10090 bytes out of 130048 bytes max)
    RAM: 701 bytes
         (text: 9952, data: 138, bss: 563)


That's because UECIDE works :P

pito

It even runs with my adxl345, not sure the output data shows the reality.
PS: alpha should be something like 0.01-0.05 to get some filtering effect..

IgnorantCarb0n

#9
Nov 20, 2013, 08:47 pm Last Edit: Nov 20, 2013, 08:51 pm by IgnorantCarb0n Reason: 1
Hi thanks the library problem is solved now, thank you.

@pito do you have experience with this accelerometer? i cant seem to get it to output anything meaningful to the serial monitor
edit, i have modified alpha but getting constant readings of 36.02 -45.44 however it is oriented....hardware fault?

holmes4

Quote
sketch_nov20a.cpp.o: In function `__static_initialization_and_destruction_0':


The error messages erffer to the dot cpp dot o file so these are linker errors. When you installed the lib did you shut down all copies of the IDE as the instructions say and then reopen?

Mark

Go Up