Pages: [1]   Go Down
Author Topic: Wire library includes, but error says "Not Defined"  (Read 1271 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm building a library to get pressure readings from a BMP085 pressure sensor using the Wire library.

Code:
/*
  BMPSensor.h - Library for reading pressure values from the BMP085
  Created by Jacob Talley, September 20, 2012.
*/
#ifndef BMPSensor_h
#define BMPSensor_h

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

class BMPSensor
{
  public:
    BMPSensor();
    long ReadPressure();
  private:
    void bmp085Calibration();
    short bmp085GetTemperature(unsigned int ut);
    long bmp085GetPressure(unsigned long up);
    char bmp085Read(unsigned char address);
    int bmp085ReadInt(unsigned char address);
    unsigned int bmp085ReadUT();
    unsigned long bmp085ReadUP();
    int ac1;
    int ac2;
    int ac3;
    unsigned int ac4;
    unsigned int ac5;
    unsigned int ac6;
    int b1;
    int b2;
    int mb;
    int mc;
    int md;
    long b5;
};

#endif

Code:
/*
  BMPSensor.cpp - Library for reading pressure values from the BMP085
  Created by Jacob Talley, September 20, 2012.
*/
#include "BMPSensor.h"

#define BMP085_ADDRESS 0x77  //I2C address of BMP085
#define OSS 0 //Oversampling setting


BMPSensor::BMPSensor()
{
  Wire.begin();
  bmp085Calibration();
}

long BMPSensor::ReadPressure()
{
  short temperature;
  long pressure;
 
  temperature = bmp085GetTemperature(bmp085ReadUT());
  pressure = bmp085GetPressure(bmp085ReadUP());

  return pressure;
}

void BMPSensor::bmp085Calibration()
{
  ac1 = bmp085ReadInt(0xAA);
  ac2 = bmp085ReadInt(0xAC);
  ac3 = bmp085ReadInt(0xAE);
  ac4 = bmp085ReadInt(0xB0);
  ac5 = bmp085ReadInt(0xB2);
  ac6 = bmp085ReadInt(0xB4);
  b1 = bmp085ReadInt(0xB6);
  b2 = bmp085ReadInt(0xB8);
  mb = bmp085ReadInt(0xBA);
  mc = bmp085ReadInt(0xBC);
  md = bmp085ReadInt(0xBE);
}

short BMPSensor::bmp085GetTemperature(unsigned int ut)
{
  long x1, x2;
 
  x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
  x2 = ((long)mc << 11)/(x1 + md);
  b5 = x1 + x2;
 
  return ((b5 + 8)>>4);
}

long BMPSensor::bmp085GetPressure(unsigned long up)
{
  long x1, x2, x3, b3, b6, p;
  unsigned long b4, b7;
 
  b6 = b5 - 4000;
 
  x1 = (b2 * (b6 * b6)>>12)>>11;
  x2 = (ac2 * b6)>>11;
  x3 = x1 + x2;
  b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
 
  x1 = (ac3 * b6)>>13;
  x2 = (b1 * ((b6* b6)>>12))>>16;
  x3 = ((x1 + x2) + 2)>>2;
  b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
 
  b7 = ((unsigned long)(up - b3) * (50000>>OSS));
  if (b7 < 0x80000000)
    p = (b7<<1)/b4;
  else
    p = (b7/b4)<<1;
   
  x1 = (p>>8) * (p>>8);
  x1 = (x1 * 3038)>>16;
  x2 = (-7357 * p)>>16;
  p += (x1 + x2 + 3791)>>4;
 
  return p;
}

char BMPSensor::bmp085Read(unsigned char address)
{
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();
 
  Wire.requestFrom(BMP085_ADDRESS,1);
  while(!Wire.available())
    ;
   
  return Wire.read();
}

int BMPSensor::bmp085ReadInt(unsigned char address)
{
  unsigned char msb, lsb;
 
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(address);
  Wire.endTransmission();
 
  Wire.requestFrom(BMP085_ADDRESS, 2);
  while(Wire.available()<2)
    ;
  msb = Wire.read();
  lsb = Wire.read();
 
  return (int) msb<<8 | lsb;
}

unsigned int BMPSensor::bmp085ReadUT()
{
  unsigned int ut;
 
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF4); //register
  Wire.write(0x2E); //value, requests uncompensated temp
  Wire.endTransmission();
 
  delay(5); //Wait for at least 4.5ms
 
  ut = bmp085ReadInt(0xF6);
  return ut;
}

unsigned long BMPSensor::bmp085ReadUP()
{
  unsigned char msb, lsb, xlsb;
  unsigned long up = 0;
 
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF4);
  Wire.write(0x34 + (OSS<<6));
  Wire.endTransmission();
 
  delay(2 + (3<<OSS)); //Wait for conversion, delay time dependant on OSS
 
  Wire.beginTransmission(BMP085_ADDRESS);
  Wire.write(0xF6);
  Wire.endTransmission();
  Wire.requestFrom(BMP085_ADDRESS, 3);
 
  while(Wire.available() < 3)
    ;
  msb = Wire.read();
  lsb = Wire.read();
  xlsb = Wire.read();
 
  up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-OSS);
 
  return up;
}

My test program is
Code:
#include <BMPSensor.h>

BMPSensor sensor;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println(sensor.ReadPressure());
}

But I'm getting this error:
Code:
C:\Users\Big Bang\Documents\Arduino\libraries\BMPSensor\BMPSensor.cpp: In constructor 'BMPSensor::BMPSensor()':
C:\Users\Big Bang\Documents\Arduino\libraries\BMPSensor\BMPSensor.cpp:15: error: 'Wire' was not declared in this scope
C:\Users\Big Bang\Documents\Arduino\libraries\BMPSensor\BMPSensor.cpp: In member function 'char BMPSensor::bmp085Read(unsigned char)':
C:\Users\Big Bang\Documents\Arduino\libraries\BMPSensor\BMPSensor.cpp:89: error: 'Wire' was not declared in this scope
C:\Users\Big Bang\Documents\Arduino\libraries\BMPSensor\BMPSensor.cpp: In member function 'int BMPSensor::bmp085ReadInt(unsigned char)':
C:\Users\Big Bang\Documents\Arduino\libraries\BMPSensor\BMPSensor.cpp:104: error: 'Wire' was not declared in this scope
C:\Users\Big Bang\Documents\Arduino\libraries\BMPSensor\BMPSensor.cpp: In member function 'unsigned int BMPSensor::bmp085ReadUT()':
C:\Users\Big Bang\Documents\Arduino\libraries\BMPSensor\BMPSensor.cpp:121: error: 'Wire' was not declared in this scope
C:\Users\Big Bang\Documents\Arduino\libraries\BMPSensor\BMPSensor.cpp: In member function 'long unsigned int BMPSensor::bmp085ReadUP()':
C:\Users\Big Bang\Documents\Arduino\libraries\BMPSensor\BMPSensor.cpp:137: error: 'Wire' was not declared in this scope

Since Wire doesn't need a variable of type Wire, I'm not sure what this could be talking about.
Any ideas?
Logged

Offline Offline
Edison Member
*
Karma: 17
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You must include Wire.h in the main sketch because of some quirks in the IDE that you can probably google if you care about.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Awesome, that fixed it! Thanks a ton!
Logged

Porto Alegre, RS - Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could someone help me, because I believe I have a similar problem. When I try to compile the code in Arduino version 1.0.1 the following message appears: 'BMP085' has not been declared.

Thanks





I'm building a library to get pressure readings from a BMP085 pressure sensor using the Wire library.

....
Since Wire doesn't need a variable of type Wire, I'm not sure what this could be talking about.
Any ideas?
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 71
Posts: 1641
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you put a semicolon at the end of the class definition in BMP085.h?

(Post your code! Otherwise it is impossible to help beyond chance guesses)
Logged

~Tom~

Porto Alegre, RS - Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Tom,
 I think there is  a semicolon at the end of the class BPM085.h


Here goes the code of  BMP085.h
Code:
/****************************************************************************
* BMP085.h - BMP085/I2C (Digital Pressure Sensor) library for Arduino       *
* Copyright 2010-2012 Filipe Vieira & various contributors                  *
*                                                                           *
* This file is part of BMP085 Arduino library.                              *
*                                                                           *
* This library is free software: you can redistribute it and/or modify      *
* it under the terms of the GNU Lesser General Public License as published  *
* by the Free Software Foundation, either version 3 of the License, or      *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU Lesser General Public License for more details.                       *
*                                                                           *
* You should have received a copy of the GNU Lesser General Public License  *
* along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
****************************************************************************/
/****************************************************************************
* Tested on Arduino Mega with BMP085 Breakout                               *
* SDA   -> pin 20   (no pull up resistors)                                  *
* SCL   -> pin 21   (no pull up resistors)                                  *
* XCLR  -> not connected                                                    *
* EOC   -> not connected                                                    *
* GND   -> pin GND                                                          *
* VCC   -> pin 3.3V                                                         *
* NOTE: SCL and SDA needs pull-up resistors for each I2C bus.               *
*  2.2kOhm..10kOhm, typ. 4.7kOhm                                            *
*****************************************************************************/
#ifndef BMP085_h
#define BMP085_h

#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

#define BMP085_ADDR                 0x77     //0x77 default I2C address
#define BUFFER_SIZE                 3

#define AUTO_UPDATE_TEMPERATURE     true    //default is true
        // when true, temperature is measured everytime pressure is measured (Auto).
        // when false, user chooses when to measure temperature (just call calcTrueTemperature()).
        // used for dynamic measurement to increase sample rate (see BMP085 modes below).
      
/* ---- Registers ---- */
#define CAL_AC1           0xAA  // R   Calibration data (16 bits)
#define CAL_AC2           0xAC  // R   Calibration data (16 bits)
#define CAL_AC3           0xAE  // R   Calibration data (16 bits)    
#define CAL_AC4           0xB0  // R   Calibration data (16 bits)
#define CAL_AC5           0xB2  // R   Calibration data (16 bits)
#define CAL_AC6           0xB4  // R   Calibration data (16 bits)
#define CAL_B1            0xB6  // R   Calibration data (16 bits)
#define CAL_B2            0xB8  // R   Calibration data (16 bits)
#define CAL_MB            0xBA  // R   Calibration data (16 bits)
#define CAL_MC            0xBC  // R   Calibration data (16 bits)
#define CAL_MD            0xBE  // R   Calibration data (16 bits)
#define CONTROL           0xF4  // W   Control register
#define CONTROL_OUTPUT    0xF6  // R   Output registers 0xF6=MSB, 0xF7=LSB, 0xF8=XLSB

// unused registers
#define SOFTRESET         0xE0
#define VERSION           0xD1  // ML_VERSION  pos=0 len=4 msk=0F  AL_VERSION pos=4 len=4 msk=f0
#define CHIPID            0xD0  // pos=0 mask=FF len=8
                                // BMP085_CHIP_ID=0x55

/************************************/
/*    REGISTERS PARAMETERS          */
/************************************/
// BMP085 Modes
#define MODE_ULTRA_LOW_POWER    0 //oversampling=0, internalsamples=1, maxconvtimepressure=4.5ms, avgcurrent=3uA, RMSnoise_hPA=0.06, RMSnoise_m=0.5
#define MODE_STANDARD           1 //oversampling=1, internalsamples=2, maxconvtimepressure=7.5ms, avgcurrent=5uA, RMSnoise_hPA=0.05, RMSnoise_m=0.4
#define MODE_HIGHRES            2 //oversampling=2, internalsamples=4, maxconvtimepressure=13.5ms, avgcurrent=7uA, RMSnoise_hPA=0.04, RMSnoise_m=0.3
#define MODE_ULTRA_HIGHRES      3 //oversampling=3, internalsamples=8, maxconvtimepressure=25.5ms, avgcurrent=12uA, RMSnoise_hPA=0.03, RMSnoise_m=0.25
                  // "Sampling rate can be increased to 128 samples per second (standard mode) for
                  // dynamic measurement.In this case it is sufficient to measure temperature only
                  // once per second and to use this value for all pressure measurements during period."
                  // (from BMP085 datasheet Rev1.2 page 10).
                  // To use dynamic measurement set AUTO_UPDATE_TEMPERATURE to false and
                  // call calcTrueTemperature() from your code.
// Control register
#define READ_TEMPERATURE        0x2E
#define READ_PRESSURE           0x34
//Other
#define MSLP                    101325          // Mean Sea Level Pressure = 1013.25 hPA (1hPa = 100Pa = 1mbar)



class BMP085 {
public:  
  BMP085();
  
  // BMP initialization
  void init();                                              // sets current elevation above ground level to 0 meters
  void init(byte _BMPMode, int32_t _initVal, bool _centimeters);   // sets a reference datum
                                                            // if _centimeters=false _initVal is Pa
  // Who Am I
  byte getDevAddr();
  
  // BMP mode  
  byte getMode();        
  void setMode(byte _BMPMode);                   // BMP085 mode
  // initialization
  void setLocalPressure(int32_t _Pa);            // set known barometric pressure as reference Ex. QNH
  void setLocalAbsAlt(int32_t _centimeters);     // set known altitude as reference
  void setAltOffset(int32_t _centimeters);       // altitude offset
  void sethPaOffset(int32_t _Pa);                // pressure offset
  void zeroCal(int32_t _Pa, int32_t _centimeters);// zero Calibrate output to a specific Pa/altitude
  // BMP Sensors
  void getPressure(int32_t *_Pa);                // pressure in Pa + offset  
  void getAltitude(int32_t *_centimeters);       // altitude in centimeters + offset  
  void getTemperature(int32_t *_Temperature);    // temperature in Cº  
  void calcTrueTemperature();                    // calc temperature data b5 (only needed if AUTO_UPDATE_TEMPERATURE is false)  
  void calcTruePressure(long *_TruePressure);    // calc Pressure in Pa    
  // dummy stuff
   void dumpCalData();                           // debug only

  void writemem(uint8_t _addr, uint8_t _val);
  void readmem(uint8_t _addr, uint8_t _nbytes, uint8_t __buff[]);
  
  private:
  
  int ac1,ac2,ac3,b1,b2,mb,mc,md;               // cal data  
  unsigned int ac4,ac5,ac6;                     // cal data
  long b5, oldEMA;                                      // temperature data
  
  uint8_t _dev_address;
  byte _buff[BUFFER_SIZE];                      // buffer  MSB LSB XLSB
  int _oss;                                     // OverSamplingSetting
  int _pressure_waittime[4];                    // Max. Conversion Time Pressure is ms for each mode
  
  int32_t _cm_Offset, _Pa_Offset;
  int32_t _param_datum, _param_centimeters;

  void getCalData();        
  

};

#endif




Have you put a semicolon at the end of the class definition in BMP085.h?

(Post your code! Otherwise it is impossible to help beyond chance guesses)
Logged

Leeds, UK
Offline Offline
Edison Member
*
Karma: 71
Posts: 1641
Once the magic blue smoke is released, it won't go back in!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I believe then it is just a case of changing:

#include <BMP085.h>

to:

#include "BMP085.h"

The two are different. The first implies a system file (somewhere buried deep in the /hardware/tools/avr/avr/ folder), the second a local file, such as arduino libraries and files in your sketch folder.
Logged

~Tom~

Porto Alegre, RS - Brazil
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot Mr. Tom,

now it works.



I put the code here: http://code.google.com/p/arduino-diy-variometer-bmp085/

Regards.


I believe then it is just a case of changing:

#include <BMP085.h>

to:

#include "BMP085.h"

The two are different. The first implies a system file (somewhere buried deep in the /hardware/tools/avr/avr/ folder), the second a local file, such as arduino libraries and files in your sketch folder.
Logged

Pages: [1]   Go Up
Jump to: