[Coding] How to ignore first 10 data and use next 20 data to calculate?

Hi ! I want to ignore the first 10 data from sensor and use the next 20 data and average those data on Arduino. Do you have any suggestions?

Thank you so much.

Read 10 points, then read 20 and average those.

Using code tags, post your working code to read one point.

1 Like

Read and ignore the first 10 points. Then sum the next 20 and divide by 20 and you have your average.

1 Like

Assuming in your code you have a function to get a data point. Then you make a for loop that goes round 10 times and call this function.

Then you make another for loop that runs 20 times. In this loop call the function and add it to a variable that you set to zero before going into this second loop.
At the end of this loop you divide this variable by 20 to get your average.

Just a point, how big will 20 data values add up to if they are all at their maximum. Make sure the data type you choose for the variable is big enough to take the maximum number. Look up how big a number you can get in an int, long int and long long int data type.

Add 10 to the pointer to your data

const byte *data…

const byte *dataFrom10 = data + 10;

Thank you everyone.

I try this code yesterday and it works

For skip first 10 data, I let them show on serial monitor but not let them in sum.

For next 20 data, I sum the next 20 data using

 //Sum data
 senSum += blue;
 Serial.print("Sum: "); Serial.println(senSum,DEC);

P.S. blue is my sensor output.

Then, average

     int senAve = senSum / numReads;
      Serial.print("Ave: "); Serial.println(senAve,DEC);

For my whole code about this

#include <Wire.h>
#include "SparkFunISL29125.h"

//Declare sensor object
SFE_ISL29125 RGB_sensor;
const byte led_gpio = 32;

void setup()
{
  //Initialize serial communication
  Serial.begin(115200);

  //Initialize the ISL29125
  RGB_sensor.init();

  //LEDs
  pinMode(led_gpio, OUTPUT);
  
}

void loop()
{
  //Trun on Blue LED
  digitalWrite(led_gpio, HIGH);
  delay(1000);
  
  int numReads = 20;
  static int count = 0;
  static int senSum = 0;
  {
    
    //Read sensor values
    unsigned int blue = RGB_sensor.readBlue();
    count++;
    Serial.print("Light intensity: "); Serial.println(blue,DEC);

    //Sum the next 20 values (skip first 5 values)
    if (count > 5)
    {
      //Sum data
      senSum += blue;
      Serial.print("Sum: "); Serial.println(senSum,DEC);
    }

    //total values is 25 
    if (count > 24)
    
    {
      
      //avaerage the value
      int senAve = senSum / numReads;
      Serial.print("Ave: "); Serial.println(senAve,DEC);
      //Calculate concentration (calibration curve)
      int conc = (senAve-56096)/(-1626);
      Serial.print("Concentration: "); Serial.println(conc,DEC);
      //Stop working if n = 25
      while(1);
    
    }
  }
  delay(10);
}

Hope this will help people who has the same problem with me before.