Library not being recognized correctly

Hey there, I'm trying to get an LIS331 accelerometer working but the library, as far as I can tell, isn't being recognized.

The exact error I am getting is ---> 'LIS331' does not name a type

Here's my .ino

#include <LIS331.h>
#include <Wire.h>

LIS331 lis;
int val;

void setup() {
  Serial.begin(9600);
  lis.begin();
}
void loop() {
  val = lis.getXValue();
  Serial.print(val);
  Serial.print("\t");
  val = lis.getYValue();
  Serial.print(val);
  Serial.print("\t");
  val = lis.getZValue();
  Serial.print(val);
  Serial.println();
}

Keywords file
\

LIS331	KEYWORD1
getXValue	KEYWORD2
getYValue	KEYWORD2
getZValue	KEYWORD2

Header file

#ifndef LIS331_h
#define LIS331_h

#include "Arduino.h"
#include "Wire.h"

#define LR_MAX_TRIES 12


#define LR_CTRL_REG1 0x20
#define LR_CTRL_REG2 0x21
#define LR_CTRL_REG3 0x22
#define LR_CTRL_REG4 0x23
#define LR_CTRL_REG5 0x24
#define LR_HP_FILTER_RESET 0x25
#define LR_REFERENCE 0x26
#define LR_STATUS_REG 0x27
#define LR_OUT_X_L 0x28
#define LR_OUT_X_H 0x29
#define LR_OUT_Y_L 0x2A
#define LR_OUT_Y_H 0x2B
#define LR_OUT_Z_L 0x2C
#define LR_OUT_Z_H 0x2D
#define LR_INT1_CFG 0x30
#define LR_INT1_SOURCE 0x31
#define LR_INT1_THS 0x32
#define LR_INT1_DURATION 0x33
#define LR_INT2_CFG 0x34
#define LR_INT2_SOURCE 0x35
#define LR_INT2_THS 0x36
#define LR_INT2_DURATION 0x37


// Power Modes

#define LR_POWER_OFF B00000000
#define LR_POWER_NORM B00100000 
#define LR_POWER_LOW1 B01000000 
#define LR_POWER_LOW2 B01100000 
#define LR_POWER_LOW3 B10000000 

// Data Rates

#define LR_DATA_RATE_50 B00000000
#define LR_DATA_RATE_100 B00001000
#define LR_DATA_RATE_400 B00010000
#define LR_DATA_RATE_1000 B00011000


// Enable and disable channel.

#define LR_Z_ENABLE B00000100
#define LR_Z_DISABLE B00000000
#define LR_Y_ENABLE B00000010
#define LR_Y_DISABLE B00000000
#define LR_X_ENABLE B00000001
#define LR_X_DISABLE B00000000

class LIS331
{
	bool writeReg(byte addr, byte val);
	bool readReg(byte addr, byte *val);
	bool getBit(byte b, byte bit);
	public:
		LIS331();
		void begin();
		int i2cAddress;
		bool statusHasOverrun();
		bool statusHasZOverrun();
		bool statusHasYOverrun();
		bool statusHasXOverrun();
		bool statusHasDataAvailable();
		bool statusHasZDataAvailable();
		bool statusHasYDataAvailable();
		bool statusHasXDataAvailable();
		byte getPowerStatus();
		bool setPowerStatus(byte status);
		byte getDataRate();
		bool setDataRate(byte rate);
		byte getZEnable();
		byte getYEnable();
		byte getXEnable();
		bool setXEnable(bool state);
		bool setYEnable(bool state);
		bool setZEnable(bool state);
		int getXValue();
		int getZValue();
		int getYValue();
};

#endif

And CPP

#include "LIS331.h"
#include "Arduino.h"
#include "Wire.h"

LIS331::LIS331(){
	i2cAddress=25;
	}

void LIS331::begin(){
	Wire.begin();
	byte state=getPowerStatus();
	if (state==LR_POWER_OFF){
		setPowerStatus(LR_POWER_NORM);
	}
	if (!getXEnable()){
		setXEnable(true);
	}	
	if (!getYEnable()){
		setYEnable(true);
	}	
	if (!getZEnable()){
		setZEnable(true);
	}	
}

bool LIS331::readReg(byte addr, byte *val){
  Wire.beginTransmission(i2cAddress);
  Wire.write(addr);
  Wire.endTransmission();

  Wire.requestFrom(i2cAddress,1);
  int timeouts=0;
  while(!Wire.available() && timeouts++<=LR_MAX_TRIES){
    delay(10);
  }
  if (Wire.available()){
		*val=Wire.read();
		return true;
  }else{
	Serial.println("FAIL");
    return false;
  }
}
bool LIS331::writeReg(byte addr, byte val){
  Wire.beginTransmission(i2cAddress);
  Wire.write(addr);
  Wire.write(val);
  Wire.endTransmission();
  return true;
}

  bool LIS331::getBit(byte b,byte bit){
	b<<=(bit);
	if (b>=127){
		return true;
	}
	return false;
  }


  bool LIS331::statusHasOverrun(){
	byte status;
	if (readReg(LR_STATUS_REG, &status)){
		return getBit(status,0);
	}
	return false;
  }

  
  bool LIS331::statusHasZOverrun(){
	byte status;
	if (readReg(LR_STATUS_REG, &status)){
		return getBit(status,1);
	}
	return false;
  }
  bool LIS331::statusHasYOverrun(){
	byte status;
	if (readReg(LR_STATUS_REG, &status)){
		return getBit(status,2);
	}
	return false;
  }
  bool LIS331::statusHasXOverrun(){
	byte status;
	if (readReg(LR_STATUS_REG, &status)){
		return getBit(status,3);
	}
	return false;
  }


  bool LIS331::statusHasDataAvailable(){
	byte status;
	if (readReg(LR_STATUS_REG, &status)){
		return getBit(status,4);
	}
	return false;
  }

  bool LIS331::statusHasZDataAvailable(){
	byte status;
	if (readReg(LR_STATUS_REG, &status)){
		return getBit(status,5);
	}
	return false;
  }
  bool LIS331::statusHasYDataAvailable(){
	byte status;
	if (readReg(LR_STATUS_REG, &status)){
		return getBit(status,6);
	}
	return false;
  }
  bool LIS331::statusHasXDataAvailable(){
	byte status;
	if (readReg(LR_STATUS_REG, &status)){
		return getBit(status,7);
	}
	return false;
  }

	byte LIS331::getPowerStatus(){
		byte state;
		if (readReg(LR_CTRL_REG1, &state)){
			// shift 5 bits to the right
			state=state>>5;
			// shift 5 bits to the left
			state=state<<5;
			// the result is that the right 5 bits are now zero
			// this means that you can compare the output with the constants defined above.
			return state;
	    }
		return 0;
	}

	bool LIS331::setPowerStatus(byte power){
		byte setting;
		if (readReg(LR_CTRL_REG1, &setting)){
			// drop the power bits by leftshifting by 3
			setting=setting<<3;
			// move the rest of the settings back to normal
			setting=setting>>3;
			setting=setting|power;
			return writeReg(LR_CTRL_REG1,setting);
		}
		return false;
	}
	byte LIS331::getDataRate(){
		byte rate;
		if (readReg(LR_CTRL_REG1, &rate)){
			rate=rate<<3;
			rate=rate>>6;
			rate=rate<<3;
			return rate;
	    }
		return 0;
	}
	bool LIS331::setDataRate(byte rate){
		byte setting;
		if (readReg(LR_CTRL_REG1, &setting)){
			// drop the power bits by leftshifting by 3
			setting=setting<<6;
			// move the rest of the settings back to normal
			setting=setting>>6;
			setting=setting|rate;

			byte power;
			power=getPowerStatus();
			setting=setting|power;

			return writeReg(LR_CTRL_REG1,setting);
		}
		return false;
		}


	byte LIS331::getZEnable(){
		byte reg;
		if (readReg(LR_CTRL_REG1, &reg)){
			if (getBit(reg,5)){
				return LR_Z_ENABLE;
			}else{
				return LR_Z_DISABLE;
			}
		}
		return 0;
	}

	byte LIS331::getYEnable(){
		byte reg;
		if (readReg(LR_CTRL_REG1, &reg)){
			if (getBit(reg,6)){
				return LR_Z_ENABLE;
			}else{
				return LR_Z_DISABLE;
			}
		}
		return 0;
	}

	byte LIS331::getXEnable(){
		byte reg;
		if (readReg(LR_CTRL_REG1, &reg)){
			if (getBit(reg,7)){
				return LR_Z_ENABLE;
			}else{
				return LR_Z_DISABLE;
			}
		}
		return 0;
	}


bool LIS331::setZEnable(bool state){
	byte setting;
	if (readReg(LR_CTRL_REG1, &setting)){
		setting &= ~(1<<2);
		if (state){
			setting |= 1<<2;
		}
		return writeReg(LR_CTRL_REG1,setting);
	}
	return false;
}
	


bool LIS331::setYEnable(bool state){
	byte setting;
	if (readReg(LR_CTRL_REG1, &setting)){
		setting &= ~(1<<1);
		if (state){
			setting |= 1<<1;
		}
		return writeReg(LR_CTRL_REG1,setting);
	}
	return false;
}
	


bool LIS331::setXEnable(bool state){
	byte setting;
	if (readReg(LR_CTRL_REG1, &setting)){
		setting &= ~(1<<0);
		if (state){
			setting |= 1<<0;
		}
		return writeReg(LR_CTRL_REG1,setting);
	}
	return false;
}

int LIS331::getZValue(){
	byte high;
	byte low;
	if (!readReg(LR_OUT_Z_L, &low)){
		return false;
	}
	if (!readReg(LR_OUT_Z_H, &high)){
		return false;
	}
	return (low|(high << 8));
}


	
	


int LIS331::getYValue(){
	byte high;
	byte low;
	if (!readReg(LR_OUT_Y_L, &low)){
		return false;
	}
	if (!readReg(LR_OUT_Y_H, &high)){
		return false;
	}
	return (low|(high << 8));
}


	
	


int LIS331::getXValue(){
	byte high;
	byte low;
	if (!readReg(LR_OUT_X_L, &low)){
		return false;
	}
	if (!readReg(LR_OUT_X_H, &high)){
		return false;
	}
	return (low|(high << 8));
}

If there's anything that's glaringly wrong, please let me know!

I've tried reinstalling the library a few times, rewriting the keywords file, scrolling through the code I just can't seem to see what the problem is.
Anyone have any thoughts? much appreciated. Thanks!

Anyone?
:frowning:

Where are the LIS331.* files?

If they are in the same folder with your .ino file, change

#include <LIS331.h>
to
#include "LIS331.h"

It's saved on the desktop, the rest of the library files are in the library folder.

How is the compiler supposed to know your library files are on the desktop?
Copy/move those files in their own folder (libraries/LIS331/), like any other library in your Arduino IDE.

Sorry,
I meant the .ino file is saved to the desktop, the library files are saved in their own folder under the Arduino libraries.

Does the LIS331 library come with an example ino?
If yes, try to create the same setup (folder etc) for your ino file.

Hi,
What IDE are you using?
I have had problems with sketches that run in 1.5.5 but have library problems with 1.6.5.
You need to locate the library for it.
Select
SKETCH from top bar.
Select
Include Library
Select
Add Zip Library
Then in the browser window that pops up, locate and click on the zip or FOLDER with the library name that you are looking for.
It will then drop back to the edit window and have added your library in the sketch.

Don't ask me why the normal keyboard #include does not work all the time, but since I updated I have to do this to most of my older sketches to locate the library files.

Tom.... :slight_smile:

Well Tom, I tried and it still is giving me the same problem.
This is very strange.
Any other suggestions?

When I define the variable,
LIS331 lis;

LIS331 is being recognized by the IDE as it's colored, but still says 'LIS331' does not name a type.

Please provide a screenshot of the IDE, and one of the folder structure.
Try changing your ino to use an instance of some other class, like RTC_DS1307, see if you get similar error.

Chirka:
Well Tom, I tried and it still is giving me the same problem.
This is very strange.
Any other suggestions?

When I define the variable,
LIS331 lis;

LIS331 is being recognized by the IDE as it's colored, but still says 'LIS331' does not name a type.

You have a choice.

You can spent time hacking known IDE problems or you can get a copy of your library files from a reputable source ( I prefer Github) ,build you own header file and add it to your x.ino as local "library" header AKA "MyLIS.h" using "Add new tab".

Building your own has advantages - you can easy edit it using IDE and you KNOW what you have.

Here's a quick screenshot of the IDE and file structure.

http://postimg.org/image/dbinvkpcn/

I downloaded the cpp and h LIS331 files from here.
into a folder structure like this:

>
Folder PATH listing
Volume serial number is B832-EF92
C:\arduino\arduino-1.0.6\libraries\LIS331
│   LIS331.h
│   LIS331.cpp
│
└───examples
    └───LIS331
            LIS331.ino

It compiles just fine.

I'm compiling at the moment, it's taking a while...

Also I'm on 1.6.4 of the IDE so that might make a difference as Tom mentioned.
I'll update.

Still not compiling,
I'm not sure how you got it to work, I made sure everything was exactly as you wrote and even rolled back to 1.0.6 IDE.

Chirka:
Still not compiling,
I'm not sure how you got it to work, I made sure everything was exactly as you wrote and even rolled back to 1.0.6 IDE.

Could you post your code (AGAIN) and full text ( verbose enabled ) of compiler errors?
I had some problems running your code ( on Due) but it compiled with library I got from github.
PS different IDE versions have problems , but generally minor.

SOLVED:

Thanks guys for your help, now I must confess my shame.
I managed to get this code working GitHub - damellis/lis331: LIS331 Accelerometer (from loguino)

With the following folder structure using 1.6.5 IDE

c->programsx86->Arduino->Libraries->LIS331
------>LIS331.cpp
------>LIS331.h

Did not include the keywords file, used the example code provided in the GitHUB folder and just saved the .ino on the desktop.

Now for what happened, first of all let me just say that I'm a mechanical eng student and my programming skills are limited to say the least, so don't flame me please.

When I downloaded the .cpp and .h files from GitHUB, I just right clicked, save link as. Saving the file like that doesn't actually save the code in the .h and .cpp files and it replaces it with a bunch of HTML ( i think), so I opened up of the .cpp and .h files with a code editor, deleted all the junk in there, and directly copied and pasted the code from GitHUB and saved the files.
Compiled no problem, working great.

Thanks again, hopefully this helps some other noob like me down the road.
:slight_smile:

Chirka:
SOLVED:

Thanks guys for your help, now I must confess my shame.
I managed to get this code working GitHub - damellis/lis331: LIS331 Accelerometer (from loguino)

With the following folder structure using 1.6.5 IDE

c->programsx86->Arduino->Libraries->LIS331
------>LIS331.cpp
------>LIS331.h

Did not include the keywords file, used the example code provided in the GitHUB folder and just saved the .ino on the desktop.

Now for what happened, first of all let me just say that I'm a mechanical eng student and my programming skills are limited to say the least, so don't flame me please.

When I downloaded the .cpp and .h files from GitHUB, I just right clicked, save link as. Saving the file like that doesn't actually save the code in the .h and .cpp files and it replaces it with a bunch of HTML ( i think), so I opened up of the .cpp and .h files with a code editor, deleted all the junk in there, and directly copied and pasted the code from GitHUB and saved the files.
Compiled no problem, working great.

Thanks again, hopefully this helps some other noob like me down the road.
:slight_smile:

Glad you got it working. It is always a good idea to use example code - if provided.
And copying from github is not that apparent. I always open the file as "RAW" and copy it as text.

c->programsx86->Arduino->Libraries->LIS331
------>LIS331.cpp
------>LIS331.h

They will work there, but the official place for the LIS331 folder is in the libraries folder of your sketches folder where the library will not be affected by any future upgrade of the IDE

And copying from github is not that apparent. I always open the file as "RAW" and copy it as text.

Using the "Download ZIP" button is easier (and probably less error prone), you get all files at once.