Function returning struct can't be found

Hi Everyone,

I have a project that recently compiled fine but today when I went to compile it - I received compilation errors. The error is:

error: 'getSensorReadings' was not declared in this scope

My function definition looks like this:

//populate struct with sensor readings
struct SensorReadings getSensorReadings()
{
    struct SensorReadings sensorReadings;
    sensorReadings.accelX = computeForce(accelGetX());
    sensorReadings.accelY = computeForce(accelGetY());
    sensorReadings.accelZ = computeForce(accelGetZ());
    sensorReadings.millis = millis();
    return sensorReadings;
}

My struct definition looks like this and is at the top of my program:

//structure holding data from sensors and history
struct SensorReadings {
    float accelX;
    float accelY;
    float accelZ;
    unsigned long millis;
};

The call to this function (actually in two places inside the loop() function) looks like this:

//get sensor readings
struct SensorReadings sensorReadings = getSensorReadings();

Both times where I make the above call - I receive the compilation error. This used to compile a month ago - not sure if something changed in the Particle world or if I accidentally made a change and can’t find it now…

Any help would be greatly appreciated - thanks!!!

I’ve had trouble with the latest firmware versions trying to get complex functions to build thier own forward declarations. So, you may want to add a function prototype at the beginning of the .ino file.

Example:

/***************************************************************************
  This is a library for the BME280 humidity, temperature & pressure sensor

  Designed specifically to work with the Adafruit BME280 Breakout
  ----> http://www.adafruit.com/products/2650

  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution

  This file was modified by Markus Haack (https://github.com/mhaack)
  in order to work with Particle Photon & Core.
 ***************************************************************************/

#include "Adafruit_Sensor.h"
#include "Adafruit_BME280.h"
#include "Particle.h"

#define BME_SCK D4
#define BME_MISO D3
#define BME_MOSI D2
#define BME_CS D5

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);

struct Conditions{
  double Temperature;
  double RelativeHumidity;
  double Pressure;
  double Altitude;
};

char conditionsJSON[128] = "unavailable";

void getInstrumentData(Conditions* cond);
void updateConditionsJSON(Conditions* cond);

void setup()
{
  Serial.begin(9600);
  Particle.variable("conditions", conditionsJSON);
  Serial.println(F("BME280 test"));
  if (!bme.begin())
  {
    while (1)
    {
      Particle.process();
      Serial.println("Could not find a valid BME280 sensor, check wiring!");
      delay(1000);
    }
  }
}

void loop()
{
  static uint32_t lastSerialUpdateMillis = 0;
  if(millis() - lastSerialUpdateMillis > 15 * 1000)
  {
    Conditions conditions;
    getInstrumentData(&conditions);
    updateConditionsJSON(&conditions);
    serialPrintData();
    lastSerialUpdateMillis = millis();
  }
}

void getInstrumentData(Conditions* cond)
{
  cond->RelativeHumidity = bme.readHumidity();
  cond->Temperature = bme.readTemperature() * 9.0F / 5.0F + 32.0F;
  cond->Pressure = bme.readPressure() / 100.0F;
  cond->Altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
}

void updateConditionsJSON(Conditions* cond)
{
  snprintf(conditionsJSON, sizeof(conditionsJSON), "{\"temperature\":%f,\"humidity\":%f,\"pressure\":%f,\"altitude\":%f}", cond->Temperature, cond->RelativeHumidity, cond->Pressure, cond->Altitude);
}

void serialPrintData(void)
{
  Serial.println(conditionsJSON);
}

2 Likes

BulldogLowell - you’re a rock star - that completely solved the problem! Ok - this is good to know moving forward - thanks so much for the help!!!

@BulldogLowell, it’s important to note that instead of passing around structs, you pass around pointers to structs. To me, this is the most important take-away here :wink:

1 Like

Hi Peekay123 - hmmm, this might be the bigger deal - this is one of the the problems with being a Java program that later in life starts developing in C. :smile:

So in the bigger picture, I have a rolling history of these structs - amazingly enough I sample at 50hz and save the data for 10 seconds. It’s somewhat resource hungry but seems to work great.

My approach is to create a new struct each time I sample my accelerometer and then push that struct onto my array of historical data (500 element array)…

If I was to use a pointer instead - would my array just be an array of pointers to the structs that are defined and populated inside my function that gets the sensor readings (struct SensorReadings getSensorReadings())?

I hope I asked that in a clear way…

@pscmpf, you could create a static or global array of structs like:

Conditions conditions[500]; //(50Hz * 10 secs)

The pointer to each struct element would then be: &conditions[index] and struct members are accessed via conditions[index].member :smile: