[Corrected] Problem with class

Hello everyone,
I’m currently trying to develop a library for using an accelerometer.
I’ve a problem with my class “accelerometer”; when I try to use, for exemple Accelerometer.getX(), an error message appear, saying that

"request for memeber ‘getX’ in 'accelerometer, which is of non-class type ‘Accelerometer()()’ "

Are you able to say me what the problem is?
Thank you

Valentin

Here are my codes:

Main.ino:

#include <Accelerometer.h>

Accelerometer accelerometer();

void setup()
{
  Serial.begin(9600);
 
  accelerometer.init();
  accelerometer.calibrate();
}

void loop()
{

 Serial.print("X: ");
 Serial.println(accelerometer.getX());
 
 Serial.print("Y ");
 Serial.println(accelerometer.getY());
 
 Serial.print("Z: ");
 Serial.println(accelerometer.getZ());
 
 delay (100);
}

Accelerometer.cpp:

/*
	Accelerometer.cpp - Library for using a BMA180 accelerometer.
	Created by Valentin Py
	Last edition: februar 10, 2012.
	Released in the public domain
	the values returned by getX,Y,Z are in mg
	The range is +-2g
*/

#include "WProgram.h"
#include "Accelerometer.h"

Accelerometer::Accelerometer()
{
  offsetX=0;
  offsetY=0;
  offsetZ=0;
}

void Accelerometer::init()
{
	wire.begin();
	/*Set EEPROM image to write mode so we can change configuration*/
	delay(20);
	Wire.beginTransmission(BMA180);
	Wire.write(0x0D);
	if(Wire.endTransmission()){return(1);}
	if(Wire.requestFrom(BMA180,1) != 1){return(2);}
	byte ee_w = Wire.read();
	ee_w |= ee_w_MASK;
	Wire.beginTransmission(BMA180);
	Wire.write(0x0D);
	Wire.write(ee_w);
	if(Wire.endTransmission()){return(1);}
	delay(20);
	/*Set mode configuration register to Mode 00*/
	Wire.beginTransmission(BMA180);
	Wire.write(0x30);
	if(Wire.endTransmission()){return(1);}
	if(Wire.requestFrom(BMA180,1) != 1){return(2);}
	byte mode_config = Wire.read();
	mode_config &= ~(mode_config_MASK);
	Wire.beginTransmission(BMA180);
	Wire.write(0x30);
	Wire.write(mode_config);
	if(Wire.endTransmission()){return(1);}
	delay(20);
	/*Set bandwidth to 10Hz*/
	Wire.beginTransmission(BMA180);
	Wire.write(0x20);
	if(Wire.endTransmission()){return(1);}
	if(Wire.requestFrom(BMA180,1) != 1){return(2);}
	byte bw = Wire.read();
	bw &= ~(bw_MASK);
	bw |= 0x00 << 4;
	Wire.beginTransmission(BMA180);
	Wire.write(0x20);
	Wire.write(bw);
	if(Wire.endTransmission()){return(1);}
	delay(20);
	/*Set acceleration range to 2g*/
	Wire.beginTransmission(BMA180);
	Wire.write(0x35);
	if(Wire.endTransmission()){return(1);}
	if(Wire.requestFrom(BMA180,1) != 1){return(2);}
	byte range = Wire.read();
	range &= ~(range_MASK);
	range |= 0x02 << 1 ;
	Wire.beginTransmission(BMA180);
	Wire.write(0x35);
	Wire.write(range);
	if(Wire.endTransmission()){return(1);}
	delay(20);
	/*Set interrupt latch state to non latching*/
	Wire.beginTransmission(BMA180);
	Wire.write(0x21);
	if(Wire.endTransmission()){return(1);}
	if(Wire.requestFrom(BMA180,1) != 1){return(2);}
	byte latch_int = Wire.read();
	latch_int &= ~(0x01);
	Wire.beginTransmission(BMA180);
	Wire.write(0x21);
	Wire.write(latch_int);
	if(Wire.endTransmission()){return(1);}
	delay(20); 
	/*Set interrupt type to new data*/
	Wire.beginTransmission(BMA180);
	Wire.write(0x21);
	if(Wire.endTransmission()){return(1);}
	if(Wire.requestFrom(BMA180,1) != 1){return(2);}
	byte int_type = Wire.read();
	int_type |= 0x02;
	Wire.beginTransmission(BMA180);
	Wire.write(0x21);
	Wire.write(int_type);
	if(Wire.endTransmission()){return(1);}
	delay(20);
	return(0);
}

void Accelerometer::calibrate()
{
  int x, y, z;
  int averageX, averageY, aveageZ;
  for(int i=0; i++; i<10)
  {
	Wire.beginTransmission(BMA180);
	Wire.write(0x02);
	if(Wire.endTransmission()){return(1);}
	if(Wire.requestFrom(BMA180,6) != 6){return(2);}
	x = Wire.read();
	x |= Wire.read() << 8;
	x >>= 2;
	y = Wire.read();
	y |= Wire.read() << 8;
	y >>= 2;
	z = Wire.read();
	z |= Wire.read() << 8;
	z >>= 2;
	averageX+=x;
	averageY+=y;
	averageZ+=z;
  }
  offsetX=(averageX/=10);
  offsetY=(averageY/=10);
  offsetZ=(averageZ/=10)-1;
  
}
int Accelerometer::getX()
{
Wire.beginTransmission(BMA180);
  Wire.write(0x02);
  if(Wire.endTransmission()){return(1);}
  if(Wire.requestFrom(BMA180,2) != 2){return(2);}
  data = Wire.read();
  data |= Wire.read() << 8;
  data >>= 2;
  data = (data*0.25)-offsetX;
  return data;
}
int Accelerometer::getY()
{
Wire.beginTransmission(BMA180);
  Wire.write(0x04);
  if(Wire.endTransmission()){return(1);}
  if(Wire.requestFrom(BMA180,2) != 2){return(2);}
  data = Wire.read();
  data |= Wire.read() << 8;
  data >>= 2;
  data = (data*0.25)-offsetY;
  return data;
}
int Accelerometer::getZ()
{
Wire.beginTransmission(BMA180);
  Wire.write(0x06);
  if(Wire.endTransmission()){return(1);}
  if(Wire.requestFrom(BMA180,2) != 2){return(2);}
  data = Wire.read();
  data |= Wire.read() << 8;
  data >>= 2;
  data = (data*0.25)-offsetZ;
  return data;
}

Accelerometer.h:

/*
	Accelerometer.h - Library for using a BMA180 accelerometer.
	Created by Valentin Py
	Last edition: februar 10, 2012.
	Released in the public domain
*/

#include "WProgram.h"
#include <Wire.h>

#define BMA180              0x40
#define ee_w_MASK           0x10
#define mode_config_MASK    0x03
#define bw_MASK             0xF0
#define range_MASK          0x0E
#define lat_int_MASK        0x01
#define lat_int             0x01


#ifndef Accelerometer_h
#define Accelerometer_h

class Accelerometer
{
  public:
    Accelerometer();
	void init();
	void calibrate();
	int getX();
	int getY();
	int getZ();
  private:
	int data;
	int offsetX;
	int offsetY;
	int offsetZ;
}
#endif

Change:

Accelerometer accelerometer();

to:

Accelerometer accelerometer;

The form you used declares 'accelerometer' to be a parameterless function returning an Accelerometer, not an object of type Accelerometer.

Thank you, I tried that and it solved the problem. :slight_smile:
However I’m having on emore issue with this code, when I try to compile:

_testBMA180:22: error: ‘class HardwareSerial’ has no member named ‘println’

and the same error for ‘Serial.print’
I’ve this problem since I include <Accelerometer.h>

Which version of Arduino are you using? If you are using Arduino 1.0, then in Accelerometer.h and .cpp you should replace #include "Wprogram.h" by "#include "Arduino.h".

I'm currently using Arduino v1.0, and ?ve some few problems to switch :D Your solution was correct, now, it work completely! Thank you for your answers!