Data processing

Hello everyone!

I want to make some sort of data processing which includes making a average from analogRead of a pin within a period of 1 second. I want to know how can i take as many values of analogRead of that pin and after 1 second, arduino makes a mean value of all the values he got, store that mean value or displays it on serial monitor and reset all the variables and starts all over again so he can show me a mean value of what happend to that analog pin in the next second. I tried multiple variants but i cannot reset the time so it can go back to 0 or reset the variables (after he does the mean value, the output stays 0).

Anyone can give me some tips how can i make it work? Thank you in advance!

Show us your code attempts. Maybe then, we can help.

Jacques

I tried multiple variants but i cannot reset the time so it can go back to 0

There is no need to reset the time to 0

Save the value of millis() before you start reading the input. Read the input then subtract the start value from the value of millis() now. If it equals or exceeds 1000 milliseconds then one second has passed.

See Using millis() for timing. A beginners guide and look at the BlinkWithoutDelay example in the IDE.

Set start time (millis ())
2)
Take sample and add; increment sample counter
3)
Check time (millis()), subtract start time
4 )
If less than 1 second passed, back to (2)
5)
Calculate average and do something with it
6)
Reset sample counter
7)
Back to (1)

Depending on mathematical processing, you might need an array to store the samples. Note that you csn roughly do 10000 ssmples per second and most Arduinos dont have enough memory to store that many.

const int pin = A0;

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

void loop() {
  unsigned long chrono = millis(); 
  int average = analogRead(pin);
  while (millis() - chrono < 1000) {
    average = (analogRead(pin) + average ) >> 2;
  }
Serial.println(average);
}

This what I meant by code.

I want to make some sort of data processing which includes making a average from analogRead of a pin within a period of 1 second. I want to know how can i take as many values of analogRead of that pin and after 1 second, arduino makes a mean value of all the values he got, store that mean value or displays it on serial monitor and reset all the variables and starts all over again so he can show me a mean value of what happend to that analog pin in the next second. I tried multiple variants but i cannot reset the time so it can go back to 0 or reset the variables (after he does the mean value, the output stays 0).

This is an assignment

jbellavance:

...

...



This what I meant by code.


> ...


This is an assignment

That's why we usually don't provide complete code :slight_smile:

Seems to have a bug, so it's ok :wink:

void setup() {
  Serial.begin(9600);
}
int sum = 0;
int increment = 0;
int average = 0;
void loop() {
  unsigned long now = millis();
  int sensorValue = analogRead(A0);
  if (millis() - now < 1000) {
    sum = sum + sensorValue;
    increment++;
  }
  else {
    average = sum / increment;
    sum = 0;
    increment = 0;
  }
  Serial.print("Analog value is:");
  Serial.println(sensorValue);
  Serial.print("Sum=");
  Serial.println(sum);
  Serial.print("Increment=");
  Serial.println(increment);
  Serial.print("Average=");
  Serial.println(average);
  delay(100);
}

Sorry for not posting the code, the problem with this code is that it shows me the analog value of the pin, the sum, increment and average. What i get from serial monitor is:

Analog value is:323
Sum=323
Increment=1
Average=0
Analog value is:324
Sum=647
Increment=2
Average=0
Analog value is:323
Sum=970
Increment=3
Average=0
Analog value is:323
Sum=1293
Increment=4
Average=0
Analog value is:323
Sum=1616
Increment=5
Average=0
Analog value is:324
Sum=1940
Increment=6
Average=0
Analog value is:323
Sum=2263
Increment=7
Average=0
Analog value is:323
Sum=2586
Increment=8
Average=0
Analog value is:323
Sum=2909
Increment=9
Average=0
Analog value is:323
Sum=3232
Increment=10
Average=0
Analog value is:324
Sum=3556
Increment=11
Average=0
Analog value is:324
Sum=3880
Increment=12
Average=0
Analog value is:323
Sum=4203
Increment=13
Average=0
Analog value is:323
Sum=4526
Increment=14
Average=0
Analog value is:323
Sum=4849
Increment=15
Average=0

The sum is corectly calculated, increment but when the time hits 1 second, it does not calculate the average and reset the variables (sum, increment). This is the closest try i had on this application.

In loop() you keep updating the now variable and comparing it with millis(). Instead of that save the start time of the period once and compare it with millis()

unsigned long startTime;
unsigned long currentTime;
const unsigned long period = 1000; 
unsigned long sum;
unsigned long increment;
unsigned long average;
void setup() {
  Serial.begin(9600);
  startTime = millis();
}

void loop() {
  int sensorValue = analogRead(A0);
  if (currentTime - startTime < period) {
    sum = sum + sensorValue;
    increment++;
  }
  else {
    average = sum / increment;
    sum = 0;
    increment = 0;
  }
  Serial.print("Analog value is:");
  Serial.println(sensorValue);
  Serial.print("Sum=");
  Serial.println(sum);
  Serial.print("Increment=");
  Serial.println(increment);
  Serial.print("Average=");
  Serial.println(average);
  delay(100);
}

I changed the code a bit but the average remains 0 even after 1 second passed.

You never set currentTime, so it will always be 0.

In loop(), you need to assign the value of millis() to currentTime.

void loop()
{
  currentTime = millis();

  ...
  ...
}

If you want your measurements to repeat correctly, you also need to 'reset' startTime once the one second has lapsed.

  ...
  ...
  else
  {
    startTime = currentTime;

    ...
    ...
  }

  ...
  ...
}

i added the 2 instructions and now it works like a charm. Thank you everyone for the support! :slight_smile: