Error: expected primary-expression before '.' token for user-defined library

This is my first time coding a library and I am running into a little difficulty.

I am attempting to use a library to allow me to gather data from the Parallax gyroscope module (L3G4200D). I am currently attaching the module to an Arduino Mega 2560 board. I have connected the SDA pin from the gyroscope to pin 20 (labled SDA on the communication segment of the board) on the arduino and the SCL pin from the gyroscope to pin 21 on the arduino. I am also connecting the Vin and GND pins of the gyroscope to a 5V power supply.

I looked at a few forums for code to use and ended up with the .cpp file and .h file that are attached. I put these files, along with the keywords.txt file, into a folder titled GSCOPE and placed it in the libraries folder.

I then began programming a sketch (attached) to see if I could call the libraries. When I tried to compile the sketch, I received the following error:

sketch_jan20a.ino: In function ‘void setup()’:
sketch_jan20a:12: error: expected primary-expression before ‘.’ token
sketch_jan20a:13: error: expected unqualified-id before ‘.’ token

I was not able to find another post to use to fix either the sketch or the library. Any help is appreciated. Thanks!

GSCOPE.ccp (2.52 KB)

GSCOPE.h (668 Bytes)

keywords.txt (165 Bytes)

gscope_trial.ino (290 Bytes)

Normally I would commend you for attaching the source files. Unfortunately, the forum server is not feeling well…

Error 503 Service Unavailable
Service Unavailable
Guru Meditation:
XID: 1626241463
Varnish cache server

If the code fits, I suggest posting it (please use
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags).

.h file

#include <Arduino.h>

#include <avr/pgmspace.h>

#include <Wire.h>
#include <I2C.h>
#include <HardwareSerial.h>
#include "GSCOPE.h"

//I2C Scanner Derived from code by Nick Gammon
//http://www.gammon.com.au/forum/?id=10896&reply=6#reply6
int GSCOPE::findAddress()
{
  int serial_address;
  Serial.begin (115200);
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
    {
        serial_address=int(i);
    }
  }
  return serial_address;
}

int GSCOPE::getGyroX(int L3G4200D_Address)
{
  int x;
  byte xMSB = readRegister(L3G4200D_Address, 0x29);
  byte xLSB = readRegister(L3G4200D_Address, 0x28);
  x = ((xMSB << 8) | xLSB);
  return x;
}

int GSCOPE::getGyroY(int L3G4200D_Address)
{
  int y;
  byte yMSB = readRegister(L3G4200D_Address, 0x2B);
  byte yLSB = readRegister(L3G4200D_Address, 0x2A);
  y = ((yMSB << 8) | yLSB);
  return y;
}

int GSCOPE::getGyroZ(int L3G4200D_Address)
{
  int z;
  byte zMSB = readRegister(L3G4200D_Address, 0x2D);
  byte zLSB = readRegister(L3G4200D_Address, 0x2C);
  z = ((zMSB << 8) | zLSB);
  return z;
}

void GSCOPE::setupL3G4200D(int L3G4200D_Address, int scale)
{
  writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);  // Enable x, y, z and 

turn off power down:
  writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);  // If you'd like to 

adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
  writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);
  if(scale == 250)  writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
  if(scale == 500)  writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
  if(scale == 2000) writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
  writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}

void GSCOPE::writeRegister(int deviceAddress, byte address, byte val)
{
    Wire.beginTransmission(deviceAddress); // start transmission to device 
    Wire.write(address);       // send register address
    Wire.write(val);         // send value to write
    Wire.endTransmission();     // end transmission
}

int GSCOPE::readRegister(int deviceAddress, byte address){
    //int v;
    Wire.beginTransmission(deviceAddress);
    Wire.write(address); // register to read
    Wire.endTransmission();
    //Wire.requestFrom(deviceAddress, 1); // read a byte
    //while(!Wire.available()) {} // waiting
    //v = Wire.read();
    //return v;
    if(Wire.requestFrom(deviceAddress, 1) !=1)
	return -1;
    return Wire.read();
}

.ccp file

#include <Arduino.h>

#include <avr/pgmspace.h>

#include <Wire.h>
#include <I2C.h>
#include <HardwareSerial.h>
#include "GSCOPE.h"

//I2C Scanner Derived from code by Nick Gammon
//http://www.gammon.com.au/forum/?id=10896&reply=6#reply6
int GSCOPE::findAddress()
{
  int serial_address;
  Serial.begin (115200);
  Wire.begin();
  for (byte i = 1; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
    {
        serial_address=int(i);
    }
  }
  return serial_address;
}

int GSCOPE::getGyroX(int L3G4200D_Address)
{
  int x;
  byte xMSB = readRegister(L3G4200D_Address, 0x29);
  byte xLSB = readRegister(L3G4200D_Address, 0x28);
  x = ((xMSB << 8) | xLSB);
  return x;
}

int GSCOPE::getGyroY(int L3G4200D_Address)
{
  int y;
  byte yMSB = readRegister(L3G4200D_Address, 0x2B);
  byte yLSB = readRegister(L3G4200D_Address, 0x2A);
  y = ((yMSB << 8) | yLSB);
  return y;
}

int GSCOPE::getGyroZ(int L3G4200D_Address)
{
  int z;
  byte zMSB = readRegister(L3G4200D_Address, 0x2D);
  byte zLSB = readRegister(L3G4200D_Address, 0x2C);
  z = ((zMSB << 8) | zLSB);
  return z;
}

void GSCOPE::setupL3G4200D(int L3G4200D_Address, int scale)
{
  writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);  // Enable x, y, z and 

turn off power down:
  writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);  // If you'd like to 

adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
  writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);
  if(scale == 250)  writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
  if(scale == 500)  writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
  if(scale == 2000) writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
  writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}

void GSCOPE::writeRegister(int deviceAddress, byte address, byte val)
{
    Wire.beginTransmission(deviceAddress); // start transmission to device 
    Wire.write(address);       // send register address
    Wire.write(val);         // send value to write
    Wire.endTransmission();     // end transmission
}

int GSCOPE::readRegister(int deviceAddress, byte address){
    //int v;
    Wire.beginTransmission(deviceAddress);
    Wire.write(address); // register to read
    Wire.endTransmission();
    //Wire.requestFrom(deviceAddress, 1); // read a byte
    //while(!Wire.available()) {} // waiting
    //v = Wire.read();
    //return v;
    if(Wire.requestFrom(deviceAddress, 1) !=1)
	return -1;
    return Wire.read();
}

keywords

GSCOPE	KEYWORD1
findAddress	KEYWORD2
getGyroX	KEYWORD2
getGyroY	KEYWORD2
getGyroZ	KEYWORD2
setupL3G4200D	KEYWORD2
writeRegister	KEYWORD2
readRegister	KEYWORD2

sketch

#include <stdint.h>
#include <GSCOPE.h>
#include <Wire.h>

int serial_address;

void setup()
{
  //Initialize gyroscope
  Wire.begin();
  Serial.begin(9600);
  serial_address = GSCOPE.findAddress();
  GSCOPE.setupL3G4200D(/*serial_address*/105, 2000);
  delay(1500);
}

void loop()
{
  
}

GSCOPE isn’t in ‘scope’

You need to initialise a GSCOPE object

GSCOPE g_Scope;

void setup()
{
  //Initialize gyroscope
  Wire.begin();
  Serial.begin(9600);
  serial_address = g_Scope.findAddress();
  g_Scope.setupL3G4200D(/*serial_address*/105, 2000);
  delay(1500);
}

I added that into my sketch just prior to the setup function and then received a new error message:

gscope_trial.cpp.o: In function setup': C:\Arduino\arduino-1.0.3-windows\arduino-1.0.3/gscope_trial.ino:13: undefined reference to GSCOPE::findAddress()’
C:\Arduino\arduino-1.0.3-windows\arduino-1.0.3/gscope_trial.ino:14: undefined reference to `GSCOPE::setupL3G4200D(int, int)’

I did not change my .cpp or .h file, which I thought adequately defined the functions.

The .h you posted here was just a copy of the cpp file it seems, post the actual .h as I can only make guesses now.

Sorry about that. Here it is.

#ifndef GSCOPE_h
#define GSCOPE_h
#include <Arduino.h>
#include <avr/pgmspace.h>
#include <Wire.h>
#include <I2C.h>
#include <HardwareSerial.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24

class GSCOPE
{
	public:
	int findAddress();
	int getGyroX(int L3G4200D_Address);
	int getGyroY(int L3G4200D_Address);
	int getGyroZ(int L3G4200D_Address);
	void setupL3G4200D(int L3G4200D_Address, int scale);

	private:
	void writeRegister(int deviceAddress, byte address, byte val);
	int readRegister(int deviceAddress, byte address);
};


//extern GSCOPE Gscope;

#endif

I’ll copy the code over in a sec and give it a try, but you also need to include the libraries your library includes into your sketch.

So add:

#include <I2C.h>
#include <HardwareSerial.h>

to the list of includes in your sketch.

Made the change but still getting the same error message.

I copied the .cpp code, the corrected .h code, and the corrected .ino file into (tabs in) Arduino 1.0.2. I changed the GSCOPE include statement to:

#include "GSCOPE.h"

I got just one “error” message:

Binary sketch size: 4,062 bytes (of a 32,256 byte maximum)

I’ll leave it to you to resolve or ignore that one.

It runs! Thanks!