Return struct from a function

Hey!

I wrote/complied some code to get some data from sensors and now Im trying to split some of it up into libraries to make the code look “cleaner”. I have one function that returns 3 values, so Im using a struct to do so, but Im having a little trouble with it.
Here is the main code

#include <SPI.h>//SPI communcation library
#include <Wire.h>//I2C communcation library

//These libraries came with Compass
#include <FreeSixIMU.h>//Reading data from Acc anf Gyro
#include <FIMU_ADXL345.h>//Digital Accelerometer
#include <FIMU_ITG3200.h>// triple-axis MEMS gyroscope and temperature 
#include <HMC5883L.h>// 3-Axis Digital Compass
#include <BMP085.h>//Digital Pressure Sensor
#include <Sonar.h>
#include <Pressure.h>



const int clock = 9;// generate a MCKL signal on pin 9 for use with SPI

float angles[3]; // yaw pitch roll
float heading;
//float tempReal,tempComp;
long internalPressure;
//long externalPressure;
long tempDOF;//internal temperature measured by DOF compass
int humidityCurrent, humidityPrevious, humidityTemp,deltaHumidity,tempPhidget,relativeHumidity;
word range;//range of Sonar

struct pressureData{
	float tempReal;
	float tempComp;
	long externalPressure;
};
pressureData pres;


// Set the FreeSixIMU object
FreeSixIMU sixDOF = FreeSixIMU();
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;


void setup(){
  
      Serial.begin(9600);//9600 baud rate for TX0, RX0
      Serial1.begin(9600);//9600 baud rate for TX1, RX1
    
      Wire.begin();//start I2C communcation
     
 
      //pressure
      SPI.begin(); //start SPI communcation
      SPI.setBitOrder(MSBFIRST);
      SPI.setClockDivider(SPI_CLOCK_DIV32); //divide 16 MHz to communicate on 500 kHz
      pinMode(clock, OUTPUT);
      delay(100);
}

void loop(){
   
      
      //sonar
     takeRangeReading(); //Tell the sensor to perform a ranging cycle
     delay(100); //Wait for sensor to finish
     range = requestRange(); //Get the range from the sensor
    
    
     getHeading();
     getPhiget();
     resetPressureSensor();//You may have to go into Pressure.h and mess around with variable location
     pres = getPressure();
     

     printData();
     
    
     delay(300); 
}



/*
printData

This function takes all the data from the sensors and sends it to the  TX1 pin. It also adds in the 
tags for each sensors data and comma delimiters
*/
void printData(){


     Serial1.print("PRESS");
     Serial1.print(",");
     Serial1.print(pres.tempReal);
     Serial1.print(",");
     Serial1.print(pres.externalPressure);
     Serial1.print(",");
     Serial1.print(pres.tempComp); 
     Serial1.println(",");
    
//}


}

Here is the header file getPressure (the one giving me errors)

#include <Arduino.h>
#include <SPI.h>//SPI communcation library

void resetPressureSensor();
struct pressureData getPressure();

Here is the library file getPressure (the one giving me errors)

#include "Pressure.h"

const int clock = 9;// generate a MCKL signal on pin 9 for use with SPI


/*
resetPressureSesnor

this resets the pressure sensor. BUT WHY AMND HOW
*/
struct pressureData{
	float tempReal;
	float tempComp;
	long externalPressure;
};

pressureData pres;


struct pressureData getPressure(){
  
	
      TCCR1B = (TCCR1B & 0xF8) | 1 ; //generates the MCKL signal
      analogWrite (clock, 128) ; 
    
      resetPressureSensor();//resets the sensor - caution: afterwards mode = SPI_MODE0!
    
      //Calibration word 1
      unsigned int result1 = 0;
      unsigned int inbyte1 = 0;
      SPI.transfer(0x1D); //send first byte of command to get calibration word 1
      SPI.transfer(0x50); //send second byte of command to get calibration word 1
      SPI.setDataMode(SPI_MODE1); //change mode in order to listen
      result1 = SPI.transfer(0x00); //send dummy byte to read first byte of word
      result1 = result1 << 8; //shift returned byte 
      inbyte1 = SPI.transfer(0x00); //send dummy byte to read second byte of word
      result1 = result1 | inbyte1; //combine first and second byte of word
    
    
      resetPressureSensor();//resets the sensor
    
      //Calibration word 2; see comments on calibration word 1
      unsigned int result2 = 0;
      byte inbyte2 = 0; 
      SPI.transfer(0x1D);
      SPI.transfer(0x60);
      SPI.setDataMode(SPI_MODE1); 
      result2 = SPI.transfer(0x00);
      result2 = result2 <<8;
      inbyte2 = SPI.transfer(0x00);
      result2 = result2 | inbyte2;
     
    
      resetPressureSensor();//resets the sensor
    
      //Calibration word 3; see comments on calibration word 1
      unsigned int result3 = 0;
      byte inbyte3 = 0;
      SPI.transfer(0x1D);
      SPI.transfer(0x90); 
      SPI.setDataMode(SPI_MODE1); 
      result3 = SPI.transfer(0x00);
      result3 = result3 <<8;
      inbyte3 = SPI.transfer(0x00);
      result3 = result3 | inbyte3;
      
    
      resetPressureSensor();//resets the sensor
    
      //Calibration word 4; see comments on calibration word 1
      unsigned int result4 = 0;
      byte inbyte4 = 0;
      SPI.transfer(0x1D);
      SPI.transfer(0xA0);
      SPI.setDataMode(SPI_MODE1); 
      result4 = SPI.transfer(0x00);
      result4 = result4 <<8;
      inbyte4 = SPI.transfer(0x00);
      result4 = result4 | inbyte4;
      
      //now we do some bitshifting to extract the calibration factors 
      //out of the calibration words; read datasheet AN510 for better understanding
      long c1 = result1 >> 3 & 0x1FFF;
      long c2 = ((result1 & 0x07) << 10) | ((result2 >> 6) & 0x03FF);
      long c3 = (result3 >> 6) & 0x03FF;
      long c4 = (result4 >> 7) & 0x07FF;
      long c5 = ((result2 & 0x003F) << 6) | (result3 & 0x003F);
      long c6 = result4 & 0x007F;
    
      resetPressureSensor();//resets the sensor
    
      //Temperature:
      unsigned int tempMSB = 0; //first byte of value
      unsigned int tempLSB = 0; //last byte of value
      unsigned int D2 = 0;
      SPI.transfer(0x0F); //send first byte of command to get temperature value
      SPI.transfer(0x20); //send second byte of command to get temperature value
      delay(35); //wait for conversion end
      SPI.setDataMode(SPI_MODE1); //change mode in order to listen
      tempMSB = SPI.transfer(0x00); //send dummy byte to read first byte of value
      tempMSB = tempMSB << 8; //shift first byte
      tempLSB = SPI.transfer(0x00); //send dummy byte to read second byte of value
      D2 = tempMSB | tempLSB; //combine first and second byte of value
     
      resetPressureSensor();//resets the sensor
    
      //Pressure:
      unsigned int presMSB = 0; //first byte of value
      unsigned int presLSB =0; //last byte of value
      unsigned int D1 = 0;
      SPI.transfer(0x0F); //send first byte of command to get pressure value
      SPI.transfer(0x40); //send second byte of command to get pressure value
      delay(35); //wait for conversion end
      SPI.setDataMode(SPI_MODE1); //change mode in order to listen
      presMSB = SPI.transfer(0x00); //send dummy byte to read first byte of value
      presMSB = presMSB << 8; //shift first byte
      presLSB = SPI.transfer(0x00); //send dummy byte to read second byte of value
      D1 = presMSB | presLSB; //combine first and second byte of value
      
      //calculation of the real values by means of the calibration factors and the maths
      //in the datasheet. const MUST be long
      const long UT1 = (c5 << 3) + 10000;
      const long dT = D2 - UT1;
      const long TEMP = 200 + ((dT * (c6 + 100)) >> 11);
      const long OFF  = c2 + (((c4 - 250) * dT) >> 12) + 10000;
      const long SENS = (c1/2) + (((c3 + 200) * dT) >> 13) + 3000;
      pressureData.externalPressure = (SENS * (D1 - OFF) >> 12) + 1000;
      pressureData.tempReal = TEMP/10;
      
      //2nd order compensation only for T > 0°C
      const long dT2 = dT - ((dT >> 7 * dT >> 7) >> 3);
      pressureData.tempComp = (200 + (dT2*(c6+100) >>11))/10;
   
       //delay(5000); 
	   
	  return pressureData;
	   
}

void resetPressureSensor(){
      SPI.setDataMode(SPI_MODE0); 
      SPI.transfer(0x15);
      SPI.transfer(0x55);
      SPI.transfer(0x40);
}

I figured it out. I hadnt named the struct properly in the library

Instead of using a struct, you can just make a function that can do this for you.

Maybe a little overkill tho.

class myValues
{
  public:
    int a, b, c;

    myValues() // initial constructor
    {
      a = b = c = 0;
    }

    myValues(int A, int B, int C) // second constructor that gets data and stores it
    {
      a = A;
      b = B;
      c = C;
    }

    myValues data() // a function to add in values, return type must be the class name
    {
      return myValues(45, 200, 35);
    }
};
//---------------END_OF_CLASS----------------

myValues mVal; // constructor
void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(115200);

  myValues D = mVal.data(); // call the data function which puts some values into the second constructor, then
                            // using another constructor, you collect that data.
  Serial.println(D.a);
  Serial.println(D.b);
  Serial.println(D.c);
}

void loop() {
  // put your main code here, to run repeatedly:

}

output:

45 200 35

It's generally easier to use reference variables.

void someFun(int iOne, int iTwo, int &oSum)
{
   oSum = iOne + iTwo;
}

Then call the function:

   int a = 14;
   int b = 2;
   int c;

   someFun(a, b, c);
   Serial.print("c = ");
   Serial.println(c);

This will print c = 16.