Home > Net >  returning multiple value in timer Interrupt
returning multiple value in timer Interrupt

Time:02-23

In my program I try to return two values after performing operations in the "Durchfluss" method. These values should then be displayed in the loop. but when I display them I get 0 for f_ml1 and 170 for f_ml. why do I get 0 for the variable f_ml1? where is the error? This Method has been calling in a loop.

class Sensor {

    float _flowRateTrinkwasser;
    unsigned int _f_ml;
    unsigned long _wasserMengeTrinkwasser;
    float _flowRateReinwasser;
    unsigned int _f_ml1;
    unsigned long _wasserMengeReinwasser;

  public:
    Sensor( float flowRateTrinkwasser, unsigned int f_ml, unsigned long wasserMengeTrinkwasser,  float flowRateReinwasser, unsigned int f_ml1, unsigned long wasserMengeReinwasser) {           // Konstruktor

      _flowRateTrinkwasser = flowRateTrinkwasser;
      _f_ml = f_ml;
      _wasserMengeTrinkwasser = wasserMengeTrinkwasser;

      _flowRateReinwasser = flowRateReinwasser;
      _f_ml1 = f_ml1;
      _wasserMengeReinwasser = wasserMengeReinwasser;

    }

    Durchfluss() { 

      _flowRateTrinkwasser = 120;    // Formel zur Berechnung von Flow Rate
      _wasserMengeTrinkwasser = 50;
      _f_ml = _flowRateTrinkwasser    _wasserMengeTrinkwasser ;

      _flowRateReinwasser = 70;
      _wasserMengeReinwasser = 40;
      _f_ml1 = _flowRateReinwasser   _wasserMengeReinwasser;

      return (_f_ml1, _f_ml);

    }

};

#include "test.h"

//Durchflussmessung Variable Trinkwasser//
float flowRateTrinkwasser;
unsigned int f_ml;
unsigned long wasserMengeTrinkwasser;

//Durchflussmessung Variable Trinkwasser//

//Durchflussmessung Variable Reinwasser//

float flowRateReinwasser;
unsigned int f_ml1;
unsigned long wasserMengeReinwasser;

//Durchflussmessung Variable Reinwasser//

Sensor sensorOne(  flowRateTrinkwasser, f_ml, wasserMengeTrinkwasser, flowRateReinwasser, f_ml1, wasserMengeReinwasser );  // Objekt vom Konstruktor Sensor1

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

void loop() {

  (f_ml1, f_ml) = sensorOne.Durchfluss();
  Serial.println(f_ml1);
  Serial.println(f_ml);
  delay(1000);
}

CodePudding user response:

Frankly, I would add a pair of getter methods and read f_ml1 and f_ml directly from your sensor object:

class Sensor {
   ...
public :
    int get_f_ml1 () { return f_ml1; }
    int get_f_ml () { return f_ml; }
    ...
}

void loop() {
  sensorOne.Durchfluss();
  Serial.println(sensorOne.get_f_ml1());
  Serial.println(sensorOne.get_f_ml());
  delay(1000);
}

CodePudding user response:

You need to declare what type Durchfluss() is supposed to return and initialize that return value properly.

Example:

// define a return type to use with `Durchfluss`
struct dfret {
    unsigned int ml1;
    unsigned int ml;
};

dfret Durchfluss() { // declare the function to return a `dfret`
    // calculate ...

    return {_f_ml1, _f_ml};  // note how to initialize `dfret`
}

I recommend removing the global variables f_ml1 and f_ml and declare them in loop().

Then in loop() if you've enabled C 17 you can use structured bindings to bind f_ml1 and f_ml to the returned dfrets member variables:

void loop() {
    auto[f_ml1, f_ml] = sensorOne.Durchfluss();
    // or if you don't need to change `f_ml1` or `f_ml`:
    //const auto&[f_ml1, f_ml] = sensorOne.Durchfluss();
}

If you haven't enabled C 17:

void loop() {
    dfret rv = sensorOne.Durchfluss();
    auto& f_ml1 = rv.ml1;
    auto& f_ml = rv.ml;
}
  • Related