Output largest number from an analogread array

HI i would like to pick largest number from an array here is a sample code
but instead of taking average i would like to take the highest number .
Thank you

float ave_1(){
float VAL = analogRead(33);

sampleArray[sampleCounter] = VAL;

sampleCounter=0;

for (int i = 0; i < 20; i++){
sampleArray[sampleCounter] = VAL;
sampleCounter++;
}

if (sampleCounter == 20) {
for (unsigned int i=0; i<numReadings; i++){
avrghr=avrghr+sampleArray[i] ;
}
delay(100);
avrghr=avrghr/numReadings;
return avrghr;
}

Loop trough your array take each value and compare it with the largest value you found so far. If the value is larger make that the largest value.

There is even a max() function that returns you the larger of two values.

https://www.arduino.cc/reference/en/language/functions/math/max/

That can simplify your code

maxValue = 0;
for ()
{
  maxValue = max( maxValue, sampleArray[i] );
}

Great thank you ,i am not sure how to loop through my array to hold on to 2 different values.
thank you for your help

your code is confusing.
looks like it reads a sample which is then copied into each array element.
it uses multiple indices (i and sampleCounter) to access an array element
it unnecessarily tests for sampleCounter == 20
numreadings and avrghr are underfined
avrghr is not initialized to zero
why is there a delay?

consider the following written on my laptop

#include <stdio.h>
#include <stdlib.h>

#define N_SAMP  20
int sampleArray [N_SAMP];
int sampleCounter = 0;

int analogRead (
    int  pin )
{
    return random() % 1000;
}

// -----------------------------------------------------------------------------
void
fillArray (void)
{
    for (int i = 0; i < N_SAMP; i++)
        sampleArray [i] = analogRead (33);
}

// ---------------------------------------------------------
void
dispArray (void)
{
    for (int i = 0; i < N_SAMP; i++)  {
        if (! (i % 5))
            printf ("\n");
        printf (" %6d", sampleArray [i]);
    }
    printf ("\n");
}

// ---------------------------------------------------------
float
avgArray (void)
{
    int sum = 0;
    for (int i = 0; i < N_SAMP; i++)
        sum += sampleArray [i];
    return 1.0 * sum / N_SAMP;
}

// ---------------------------------------------------------
int
maxArray (void)
{
    int max = sampleArray [0];
    for (int i = 0; i < N_SAMP; i++)
        if (max < sampleArray [i])
            max = sampleArray [i];
    return max;
}

// -----------------------------------------------------------------------------
void
setup () {
    fillArray ();
    dispArray ();

    char s [80];
    sprintf (s, " %.2f avg, %6d max\n", avgArray (), maxArray ());
    printf (s);         // in place of Serial.print
}

// -----------------------------------------------------------------------------
int main () {
    setup ();
    return 0;
}

seems a mix of trying to read samples while simultaneously trying to calculate an

float ave_1()
{
  int max = 0;

  for (int i = 0; i < 20; i++)
  {
    int val = analogRead(33);
    if (val > max)
      max = val;
    delay(100);
  }

  return max;
}

Just declare another variable.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.