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, ®)){
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, ®)){
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, ®)){
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!