Error by return multiple value

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?

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);
}


Your error is that a return returns a single value.
Use references instead.

You can only return a single value from a function.

    Durchfluss()   // function which will be called when an interrupt occure at timer 1

Can you even return a single value from an ISR ?

Consider using global variables or returning a struct containing multiple variables

do means references for table other vector where I can save multiple value?

I missed the fact this was an interrupt , and as @UKHeliBob pointed out, a bare ISR cannot return even a single value.

...and now you've edited the code and removed the comment, and Bob and I look like idiots.

I'm out.

sorry it was a mistake. I have to remove it

You can return multiple values like that from a function in Python.

Since an ISR cannot return a value, you place the return value in a global variable and build a mechanism (locks) around it to know when to read that variable by polling the lock.

The below shows a rudimentary example - not tested

volatile int isr_result = 0; // ISR result
volatile int lock = 0; // Lock variable

//This is the interrupt handler
void isr()
{
    lock = 1; // Set lock

    isr_result = 10; // Update value

    lock = 0; // Release lock
}

void loop()
{
    if (lock == 0) //Check if lock is released before reading the ISR result
    {
        int tmp = isr_result;
        printf("ISR VALUE = %d", isr_result);
    }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.