reading A0 and storing in an array of 10 values and getting max of the first 10

Ok I am reading values at pin A0, and i am storing them in an array. I want to know how can i get the code for getting the maximum value for the first 10 values and then use serial.print to display that value. And then get the next 10 values in the array and then display the maximum value of that 10 elements.I need to continue this indefinitely.

I would also like the code to be flexible so that i can change the array of 10 to say array of 5.

currently i have this code. And i want to modify this code to display the max values and min values as well. Anyone could help that would be great. Please insert ur code in the code below with explanations.

Thanks guys
Have a lovely day!

const int numReadings = 5;

float readings[numReadings];      // the readings from the analog input
int index = 0;                  // the index of the current reading
int total = 0;                  // the running total
int average = 0;       // the average
int maxval=0;
int inputPin = A0;

void setup()
{
  // initialize serial communication with computer:
  Serial.begin(9600);                   
  // initialize all the readings to 0: 
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;    
//Serial.println( maxval );
    
}

void loop() {
  // subtract the last reading:
  total= total - readings[index];         
  // read from the sensor:  
  readings[index] = analogRead(inputPin); 
  // add the reading to the total:
  total= total + readings[index];       
  // advance to the next position in the array:  
  index = index + 1;                    

  // if we're at the end of the array...
  if (index >= numReadings)              
    // ...wrap around to the beginning: 
    index = 0;                           

  // calculate the average:
  average = total / numReadings;  
 
 
  Serial.print(index);
  Serial.print("\t");
  Serial.print(average); 
  Serial.print("\t\t"); 
  Serial.println();
  delay(1000);        // delay in between reads for stability  
 
}

z8_average_with_max_and_min.ino (1.23 KB)

Unless you need each of the 10 values for something don't bother with the array.

Set a min variable to 1024 Set a max variable to 0 Set a total variable to 0

After each reading If the reading is smaller than min set min to the reading If the reading is larger than max set max to the reading Add the reading to the total

Output the results

Ummm...I dont think thats gona work for example i get 1. 200 2. 300 3. 100 4. 600 5. 900

the above are readings from A0 pin displaying

I want to know how can i get the max value displayed which is 900 for the above case.

My array will reset and then it will store again 1. 100 2. 300 3. 300 4. 500 5. 600

and now i want to display 600 as my max in serial monitor.

Is that possible? if so then how? May be someone can help me edit the code to do that.

Maybe you will post your code that got those results.

Read this before posting a programming question

max starts at 0

Read the input using a for loop

  1. 200 Is 200 larger than 0 ? Yes. Set max to 200

  2. 300 Is 300 larger than 200 ? Yes. Set max to 300

  3. 100 Is 100 larger than 300 No. Do nothing

  4. 600 Is 600 larger than 300 ? Yes. Set max to 600

  5. 900 Is 900 larger than 600 ? Yes. Set max to 900

Print max

Reset variables and do it again

Yes that is a possibility, but cant things be more simple, like bubble sort or something more easier. Those loops are gona chew a lot of time i believe. I guess this code will be more like a peak detector as well.

Yes that is a possibility, but cant things be more simple, like bubble sort or something more easier.

It's hard to think of a simpler solution.

hardikfiji:
Yes that is a possibility, but cant things be more simple, like bubble sort or something more easier.

A bubble sort? Whoa. They chew up time.

Bubble sort has worst-case and average complexity both ?(n2), where n is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of O(n log n). Even other ?(n2) sorting algorithms, such as insertion sort, tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when n is large.

hardikfiji: Yes that is a possibility, but cant things be more simple, like bubble sort or something more easier. Those loops are gona chew a lot of time i believe. I guess this code will be more like a peak detector as well.

I think that you have misunderstood my suggestion. You don't do the max/min/total derivation after taking n readings, you do it on the fly after [u]each[/u] reading. You can easily do it without a for loop if that troubles you. Just use the loop() function and increment a counter as you do now. When the counter reaches the required number output the values and reset them for next time.

As to a for loop chewing up a lot of time, your current solution already has a 1 second delay between sensor readings. Enough said I think.

Is a peak detector not what you want ?

Hi UKHeliBob

Thanks man, and my bad I didnt understand ur suggestion in the first place. I got what u mean to say now, and I am trying to implement that. Hope its gona work out.

Nick! Thanks to you as well. I think i am gona try both ways and see which one is more easier to implement.

I am doing sound processing at the moment for my Final Year Project and this is the base foundation i am trying to establish.

Cheers Guys, You all have been very supportive to me. Really appreciate it, and i will also make sure that i contribute to this forum as much as you guys do. :)