How to find mean value of Arrays

Hi, everyone… :slight_smile:

I’m here to give my solution to my problem on this topic.

#include <EEPROM.h>
#include <Process.h>

double SUB2_cy1[] = {35.92, 35.73, 35.79, 36.15, 35.73, 35.91, 36.14, 35.68, 35.87, 35.53, 35.16, 36.24, 35.7, 35.65, 35.55, 35.53, 36.03, 35.65, 36.22, 35.47, 35.65, 35.74, 35.31, 35.88, 35.36, 35.08, 35.51};
double SUB2_cy2[] = {35.89, 35.59, 35.51, 35.71, 35.48, 35.89, 36.17, 35.73, 35.43, 35.74, 35.65, 35.78, 35.81, 35.21, 35.18, 34.99, 35.84, 35.46, 35.77, 35.85, 36.12, 35.84, 35.74, 35.98, 35.74, 36.3, 36.39};
double SUB2_cy3[] = {35.86, 35.72, 35.84, 35.84, 35.84, 36.16, 36.02, 35.88, 35.93, 35.83, 35.67, 35.55, 35.84, 35.14, 35.67, 35.62, 35.85, 35.84, 35.55, 35.55, 36.02, 35.86, 35.8};

double samples[100];
double arrBBT1, arrBBT2, arrBBT3;
double ReadEEPROM;  //Variable to store in EEPROM.
int eeAddress = 0;   //Location we want the data to be put.
int eAddress = 1;
int BBTno;

void setup() {
  // put your setup code here, to run once:
  arrBBT1 = sizeof( SUB2_cy1 ) / sizeof( double );
  arrBBT2 = sizeof( SUB2_cy2 ) / sizeof( double );
  arrBBT3 = sizeof( SUB2_cy3 ) / sizeof( double );
  Serial.begin( 9600 );
  while (!Serial);
  delay(1000);
  Serial.println( arrBBT1 );
  Serial.println( arrBBT2 );
  Serial.println( arrBBT3 );

}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println("Key in your BBT INPUT number ...");
  while (Serial.available() == 0) { // Wait for User to Input Data
  }
  BBTno = Serial.parseInt(); //Read the data the user has input

  if (BBTno = 1)
  {
    BBT1();
  }
  else if (BBTno = 2)
  {
    BBT2();
  }
  else if (BBTno = 3)
  {
    BBT3();
  }
  else
  {  }

}


void BBT1()
{
  int i;
  double limit;

  // take N samples in a row, with a slight delay
  for (i = 0; i < arrBBT1; i++) {
    samples[i] = SUB2_cy1[i];
    delay(50);  //50 5 bacaan
  }

  // limit all the samples out
  limit = 0;
  for (i = 0; i < arrBBT1; i++) {
    limit += samples[i];
    Serial.print("Data plus = ");
    Serial.println( limit );
  }
  limit /= arrBBT1;
  Serial.print("Data limit = ");
  Serial.println( limit );
  //EEPROM.put(eeAddress, tempValue3);
}


void BBT2()
{
  int i;
  double limit;

  // take N samples in a row, with a slight delay
  for (i = 0; i < arrBBT2; i++) {
    samples[i] = SUB2_cy1[i];
    delay(50);
  }

  // limit all the samples out
  limit = 0;
  for (i = 0; i < arrBBT2; i++) {
    limit += samples[i];
    Serial.print("Data plus = ");
    Serial.println( limit );
  }
  limit /= arrBBT2;
  Serial.print("Data limit = ");
  Serial.println( limit );
}


void BBT3()
{
  int i;
  double limit;

  // take N samples in a row, with a slight delay
  for (i = 0; i < arrBBT3; i++) {
    samples[i] = SUB2_cy1[i];
    delay(50);  
  }

  // limit all the samples out
  limit = 0;
  for (i = 0; i < arrBBT3; i++) {
    limit += samples[i];
    Serial.print("Data plus = ");
    Serial.println( limit );
  }
  limit /= arrBBT3;
  Serial.print("Data limit = ");
  Serial.println( limit );
}

For the above code, I can now find the mean value of each array.
Feel free to comment my code for any improvement.

Tq. :slight_smile:

arrBBT1 = sizeof( SUB2_cy1 ) / sizeof( double );

Why is arrBBT1 defined as a double if it is going to hold the number of elements in the array? Do you expect that value to ever be a fraction? How do you make an array with 3.45 elements?

Hi, Delta_G ... :)

Why is arrBBT1 defined as a double if it is going to hold the number of elements in the array? Do you expect that value to ever be a fraction? How do you make an array with 3.45 elements?

It is because when I change it to int... the code cannot be compiled because of this error.

Maybe if you can prove to me, I'll try again. :wink:

  if (BBTno = 1)I'm so glad you found a solution that works, even if the code is way too long.

I'm so glad you found a solution that works, even if the code is way too long.

Thanks AWOL... :) Maybe you can help me to make it short and more compatible. :wink:

Maybe you can make it correct first. ;)

Have you noticed how BBT1() looks a lot like BBT2() and even BBT3()? (Hint: you very nearly had the answer in your original post - I may have already mentioned this)

Maybe you can help me to make it short and more compatible.

More compatible with what?

Your code is STILL seriously flawed, with mistakes that you refuse to fix. You claim, without a shred of evidence, that making the proper changes causes failure to compile. All we can do is ignore you, unless you provide some proof. Things like code where the variables are properly ints, error messages, etc. would be considered proof. Baseless claims are stupid.

Why not calculate it using excel or a calculator app, and put the result in your program?

It is because when I change it to int... the code cannot be compiled because of this error.

Maybe if you can prove to me, I'll try again. :wink:

What error? There are all sorts of really stupid things that you can do that will make an error go away. But just because the error goes away doesn't mean you've done anything right.