How to make data visible?

Hi, everyone.
I need help from anyone.
I try to get a data

*a = ave.mean();
*b = ave.stddev();

and try to call in in void loop, but the data cannot be visible in the serial monitor.

Here is my code.

#include <Average.h>

#define TEST 5
int samples[NUMSAMPLES];
Average<float> ave(5);

void setup() {
    Serial.begin(9600);
    analogReference(EXTERNAL);
    while (!Serial){}
}

void loop()
{
  int i;
  float a, b;
  statistic_reading_stats( i, &a, &b);
  Serial.print(a); Serial.print(", "); Serial.println(b);

}

void statistic_reading_stats(int i, float *a, float *b) {
    
  int stopGettingData=0;
  int s;
  while(stopGettingData<TEST)
  {
    ave.push(analogRead(A0));
    Serial.println();
    Serial.println("Start Putting data...");
    //delay(5000);
     
    for (int s = 0; s < TEST; s++) 
    {
        Serial.println(ave.get(s));
        Serial.print(" ");
        //delay(1000);
    }
    Serial.println();
    // And show some interesting results.
    *a = ave.mean(); 
    *b = ave.stddev();
    
    delay(1000);
    stopGettingData++;
Serial.println("Data........");    
  }  
  exit(0);
}

I know that ‘stopGettingData++’ will stop the process, but I don’t think that the data cannot appear when it is called.

Please,
someone, help me with retrieving the data.
Thank you.

test_average.PNG

What's the deal with exit(0)? Indent your code (control-T) - why do you print data you did not collect yet in the function?

exit(0) is to exit the loop... if I don't write 'exit(0)' the process of void 'statistic_reading_stats' will go on, although the data needed is enough.

do you have any suggestion...?

By the way, what do you mean ?

Indent your code (control-T) - why do you print data you did not collect yet in the function?

Simply press Ctrl + T in the IDE and have a look. Looks great, doesn't it?

I suggest you remove exit(0); See the documentation for what it does... pretty excessive :)

Don't confuse exit with return and since you told the compiler your function returns nothing (void) you don't even need a return.

J-M-L ... Sorry, but this is what I actually want it to do.

At first, I want it to get ONLY five data from analogRead... For that, I cannot do it in a void loop because it will still be running the process.

Then, I try to make it as another function, so I can get the reading by returning the value (though I now that return cannot be with function... So I try to set the function as float). But, the problem is, I want the function to return more than one data.

So, I search and find that there is a way to return more than one value from function to another function by using 'pointers'.

Now the value can be returned using pointers, but it's keep running the process. I know that I can just simply 'not code the process in the void loop', in order to stop the process. But maybe you can suggest me other methods? I mean...the right one.

Sorry, but this is what I actually want it to do.

You need to explain what you think that exit() does. It COMPLETELY stops the Arduino. Is that really what you want?

At first, I want it to get ONLY five data from analogRead... For that, I cannot do it in a void loop because it will still be running the process.

"a void loop" does NOT make sense. There is ONLY one loop() function. If you can't take 5 readings in loop(), why ARE you doing that?

But, the problem is, I want the function to return more than one data.

Never going to happen. Functions return at most one value. What they return can be a struct that contains multiple values, or you can pass the function an address where it stores the data of interest. That is what the & operator is for, and what pass-by-reference is for.

Paul…
exit(0) is what I need the process in that function to stop the process.

Yes, your right there is only one loop() function, and that is why I create another function to do the process to stop looping.

So I guess now, I just have to un-using the void loop().
Just like this. Because this is worked.

#include <Average.h>
#define THERMISTORPIN A0

Average<float> ave(5);

void setup() {
  Serial.begin(9600);
  analogReference(EXTERNAL);
  while (!Serial) {}
  L();
}
void loop(){}

void L()
{
  int i;
  float a, b, c, d;
  statistic_reading_stats( i, &a, &b, &c, &d);
  Serial.print("Average  =  ");
  Serial.println(a); 
  Serial.print("Standard Deviation = "); 
  Serial.println(b);
  Serial.print("Maximum = "); 
  Serial.println(c);
  Serial.print("Minimum = "); 
  Serial.println(d);

}

void statistic_reading_stats(int i, float *a, float *b, float *c, float *d) {

  int stopGettingData = 0;
  int s;
  while (stopGettingData < TEST)
  {
    ave.push(analogRead(A0));
    Serial.println();
    Serial.println("Start Putting data...");
    //delay(5000);

    for (int s = 0; s < TEST; s++)
    {
      Serial.println(ave.get(s));
      Serial.print(" ");
      //delay(1000);
    }
    Serial.println();
    // And show some interesting results.
    *a = ave.mean();
    *b = ave.stddev();
    *c = ave.maximum();
    *d = ave.minimum();

    delay(1000);
    stopGettingData++;
    Serial.println("Data........");
  }
  //exit(0);
}

Here is my code. This one works well for me.
^^

SyukriY: J-M-L ... Sorry, but this is what I actually want it to do.

I don't think so. when I read your loop() code I see

 statistic_reading_stats( i, &a, &b);
  Serial.print(a); Serial.print(", "); Serial.println(b);

so I think you want to go collect 5 samples, then come back to the main loop and print the average data you calculated.

isn't it?

at the moment you call statistic_reading_stats() and at the end of this method you hang your arduino. is that really what you want to do?

study this code and run it

int a;

void setup() {
  Serial.begin(9600);
  a = 0;
  Serial.print("in the setup a is = "); Serial.println(a);

  delay(500);
}

void selfdestruct()
{
  a++;
  exit(0); // this is what your code does today
}

void notselfdestruct()
{
    a++; // this is what I suggest, as your function is void, no need for return
}

void loop() {
  selfdestruct(); // run first time with this line not commented
  // notselfdestruct(); // then comment the line above and uncomment that one.
  Serial.print("in the loop() a is = "); Serial.println(a);
  delay(500);
}

You'll see 2 lines in the loop

 selfdestruct(); // run first time with this line not commented
  // notselfdestruct(); // then comment the line above and uncomment that one.

so run once with calling selfdestruct(); and see what's happening the console --> nothing after the setup, your program dies at the exit() point

then comment the first line and uncomment the second one and try again --> isn't that what you want?

I really can not see why you need to pass the member fields of a global variable to another function. But, hey, I don’t need to maintain oddball code like that, so go right ahead.

OK you were typing as I'm typing

have you noticed someone clever puts some comments when you open a new sketch to explain what the 2 function do?

void setup() {
[color=red]  // put your setup code here, to run once:
[/color]
}

void loop() {
[color=red]  // put your main code here, to run repeatedly:
[/color]
}

so - if you want to run the average only once, then indeed, put it in the setup()

exit(0) is used in C programs running under Unix to terminate the process and to return an error code of zero (no error). It won't do anything unless you are using something like those Arduino Yun cards. It shouldn't even compile.

PaulMurrayCbr: exit(0) is used in C programs running under Unix to terminate the process and to return an error code of zero (no error). It won't do anything unless you are using something like those Arduino Yun cards. It shouldn't even compile.

Paul - it's totally legit in C++ and performs as expected.

check my code above... it does do something pretty radical :)