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;
}
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