Average of maximum & minimum value from the sensor

I want to display in the serial monitor the following
1.Number of peaks in 10s.
2.average of max values in 10s
3.average of min values in 10s

I have done an example program by giving input from function generator with 1.5dc offset,2vpp and 1 hz frequency.

I am getting the no.of peaks correct.But max and mini wrong answers.Please advice.

long measurementStartTime = 0;
int count = 0;
byte sensorPin = A0;
int currentSensorValue;
boolean counted = false;
int counts;
int i;
unsigned long int totalA=0;
unsigned long int totalb=0;
int c,a=0,b=1023;
float maxvoltage,minvoltage;
float mindig,Maxdig;
void setup(){
void loop(){
  if ((millis() - measurementStartTime > 10000) && (count > 0))  //time is up
   counts =count*6;// no.of peaks in one minute
   Serial.print("no.of peaks:");
    measurementStartTime = millis();
    count= 0;
  currentSensorValue = analogRead(A0);
  if (currentSensorValue > 511.5&& counted == false)
    counted = true;  
  else if (currentSensorValue < 511.5)
    counted = false; 
  totalA=totalA+a;//sum of max values
  else if(c<b){
   totalb=totalb+b;//sum of min values
    mindig=totalb/100;//average of min values
    Maxdig=totalA/100; //average of max values
    minvoltage=b*5/1023;//corresponding analog values

Here is my output

no.of peaks:60

BTW long measurementStartTime = 0; Should be unsigned long measurementStartTime = 0;

:sleeping: time.


Thank you.I changed that.Any idea about the problem with the output?

I have solved the problem by changing the type of a & b from int to float.Now I need to find the average of max values & min values.Please advise

I have solved the problem

You mispelled "fudged" there. The sum of a number of integers is also an integer. Try "long" instead.

I mean I am getting the max and minimum values correct when I changed the type of 'a ’ and ‘b’

It’s still a fudge.

Hint: in C, what do you think the value of 5 / 1023 is? (Even if you ignore the fact that it should be 1024, not 1023)

Check my statistics class - http://playground.arduino.cc/Main/Statistics -

you can either use it or learn from the code :)