Search code examples
classarduino-ideesp32

Function was not declared in the scope


I do not know why my program throws me error that I do not have defined functions even though I have them in the program. This happened after adding the code INA219 monitor;

My code:

#include "BNO055_ESP32.h"
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <INA219.h>



#define SHUNT_MAX_V 0.02  /* Rated max for our shunt is 75mv for 50 A current: 
                             we will mesure only up to 20A so max is about 75mV*20/50 lets put some more*/
#define BUS_MAX_V   5  /* with 12v lead acid battery this should be enough*/
#define MAX_CURRENT 5    /* In our case this is enaugh even shunt is capable to 50 A*/
#define SHUNT_R   0.1   /* Shunt resistor in ohm */

#define SERVICE_UUID            "6E40180D-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID     "6E402A37-B5A3-F393-E0A9-E50E24DCCA9E"


// BLE variables
bool deviceConnected = false;

BLECharacteristic *pCharacteristic;

INA219 monitor;

void setup() {

  Wire.begin(21, 22, 400000);
  Serial.begin(115200); //opens serial terminal
  monitor.begin();
  monitor.configure(INA219::RANGE_16V, INA219::GAIN_1_40MV, INA219::ADC_16SAMP , INA219::ADC_16SAMP , INA219::CONT_SH_BUS);
  monitor.calibrate(SHUNT_R, SHUNT_MAX_V, BUS_MAX_V, MAX_CURRENT);

  BLEDevice::init("SMART HELMET");

  //----------BLE INITIALIZATION-------------

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
}

void loop() {

  readQuatData(BNO055_quatCount);
  // Calculate the quaternion values

  BNO055_quat[0] = (float)(BNO055_quatCount[0]) / 16384.;
  BNO055_quat[1] = (float)(BNO055_quatCount[1]) / 16384.;
  BNO055_quat[2] = (float)(BNO055_quatCount[2]) / 16384.;
  BNO055_quat[3] = (float)(BNO055_quatCount[3]) / 16384.;

  pCharacteristic->setValue((uint8_t*)BNO055_quat, 16);


  pCharacteristic->notify(); // Send the value to the app!

}

void readQuatData(int16_t * destination)
{
  uint8_t rawData[8];  // x/y/z gyro register data stored here
  readBytes(BNO055_ADDRESS, BNO055_QUA_DATA_W_LSB, 8, &rawData[0]);  // Read the six raw data registers sequentially into data array
  destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ;       // Turn the MSB and LSB into a signed 16-bit value
  destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
  destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
  destination[3] = ((int16_t)rawData[7] << 8) | rawData[6] ;
}

Error I am getting:

C:\Users\Boris\Documents\Arduino\pokus_BNO055_ESP32\BNO_jedna_charka\BNO_jedna_charka.ino: In function 'void loop()':

BNO_jedna_charka:216:36: error: 'readQuatData' was not declared in this scope

       readQuatData(BNO055_quatCount);

                                    ^

C:\Users\Boris\Documents\Arduino\pokus_BNO055_ESP32\BNO_jedna_charka\BNO_jedna_charka.ino: In function 'void readQuatData(int16_t*)':

BNO_jedna_charka:282:66: error: 'readBytes' was not declared in this scope

   readBytes(BNO055_ADDRESS, BNO055_QUA_DATA_W_LSB, 8, &rawData[0]);  // Read the six raw data registers sequentially into data array

                                                                  ^

C:\Users\Boris\Documents\Arduino\pokus_BNO055_ESP32\BNO_jedna_charka\BNO_jedna_charka.ino: In function 'bool initBNO055()':

BNO_jedna_charka:346:64: error: 'writeByte' was not declared in this scope

   writeByte(BNO055_ADDRESS, BNO055_OPR_MODE, BNO055_CONFIGMODE );

                                                                ^

C:\Users\Boris\Documents\Arduino\pokus_BNO055_ESP32\BNO_jedna_charka\BNO_jedna_charka.ino: In function 'void accelgyroCalBNO055(float*, float*)':

BNO_jedna_charka:390:49: error: 'writeByte' was not declared in this scope

   writeByte(BNO055_ADDRESS, BNO055_PAGE_ID, 0x00);

                                                 ^

BNO_jedna_charka:401:65: error: 'readBytes' was not declared in this scope

     readBytes(BNO055_ADDRESS, BNO055_ACC_DATA_X_LSB, 6, &data[0]);  // Read the six raw data registers into data array

                                                                 ^

BNO_jedna_charka:430:65: error: 'readBytes' was not declared in this scope

     readBytes(BNO055_ADDRESS, BNO055_GYR_DATA_X_LSB, 6, &data[0]);  // Read the six raw data registers into data array

                                                                 ^

C:\Users\Boris\Documents\Arduino\pokus_BNO055_ESP32\BNO_jedna_charka\BNO_jedna_charka.ino: In function 'void magCalBNO055(float*)':

BNO_jedna_charka:530:64: error: 'writeByte' was not declared in this scope

   writeByte(BNO055_ADDRESS, BNO055_OPR_MODE, BNO055_CONFIGMODE );

                                                                ^

exit status 1
'readQuatData' was not declared in this scope

C:\Users\Boris\Documents\Arduino\ina219_pokus_stackoverflow\ina219_pokus_stackoverflow.ino: In function 'void loop()':

ina219_pokus_stackoverflow:45:32: error: 'readQuatData' was not declared in this scope

   readQuatData(BNO055_quatCount);

                                ^

C:\Users\Boris\Documents\Arduino\ina219_pokus_stackoverflow\ina219_pokus_stackoverflow.ino: In function 'void readQuatData(int16_t*)':

ina219_pokus_stackoverflow:63:66: error: 'readBytes' was not declared in this scope

   readBytes(BNO055_ADDRESS, BNO055_QUA_DATA_W_LSB, 8, &rawData[0]);  // Read the six raw data registers sequentially into data array

                                                                  ^

exit status 1
'readQuatData' was not declared in this scope

Thanks for any help.

Boris


Solution

  • In C and C++ you have to declare functions before you call them.

    You can either do that by defining the function like you did here:

    void readQuatData(int16_t * destination)
    {
      uint8_t rawData[8];  // x/y/z gyro register data stored here
      readBytes(BNO055_ADDRESS, BNO055_QUA_DATA_W_LSB, 8, &rawData[0]);  // Read the six raw data registers sequentially into data array
      destination[0] = ((int16_t)rawData[1] << 8) | rawData[0] ;       // Turn the MSB and LSB into a signed 16-bit value
      destination[1] = ((int16_t)rawData[3] << 8) | rawData[2] ;
      destination[2] = ((int16_t)rawData[5] << 8) | rawData[4] ;
      destination[3] = ((int16_t)rawData[7] << 8) | rawData[6] ;
    }
    

    Or you can do it by using a function declaration like this:

    void readQuatData(int16_t * destination);
    

    You're calling readQuatData() before you declare it, so you're getting this error.

    You can either move your definition of the function before the places that you use it, or you can declare it before you use it, like so:

    INA219 monitor;
    
    void readQuatData(int16_t * destination);
    
    void setup() {
    

    I placed it before the call to setup() as declaring everything you need to declare at the start of the file, before the code, is usually cleaner and more maintainable than scattering declarations throughout the code.

    You'll need to do this for writeByte() and readBytes(). These seem to be missing from the code you shared, so you're on your own with them, but you'd do the same sort of thing - either define them before you use them, or add a declaration for them before you use them.