Returning max value from array

Hi all, as with most posts I’m new to the Arduino and haven’t used c/c++ since college. I’ve usually been coding in Labview of VB.

Currently I’m sweeping a Sharp GP2D12 on a servo and getting readings every 90 degs, and loading the values into an array.

Is there a function to return the maximum value from that array?

If not how would you go about determining the maximum value?

This is my code so far:

#include <ServoTimer1.h>

ServoTimer1 servo1;
ServoTimer1 servo2;
int IR_Sensor = 2; // select the input pin for the ir sensor
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
int distance[4] = {0};
int servo_angle = {0,90,180,90};

void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Servo test!”);
servo1.attach(10);
}

void loop()
{

for(int x = 1; x <= 4; x++)
{
servo1.write(servo_angle);
distance = ReadSens_and_Condition();
Serial.println(servo_angle);
Serial.println(distance);
Serial.println();
delay(500);
}

}

int ReadSens_and_Condition()

{
int i;
int sval;

for (i = 0; i < 5; i++)
{
sval = sval + analogRead(IR_Sensor); // sensor on analog pin 0
}

sval = sval / 5; // average
return sval;
}

I’m not aware of any library function that will do what you want, but such a function can’t be more than about seven or eight lines, though the value itself is probably of less importance than its index (i.e. which direction that value came from).

Besides, you don’t need to save it to an array (which saves valuable RAM), you can do it as you go along. Again, the index may be of some importance.

 for(int x = 1; x <= 4; x++)

Indices run from zero in C/C++, so this is generating an illegal access

I just finnished the start for an Array wrapper library.

[Under Construction] http://www.arduino.cc/playground/Code/Array

Example Skecth:

#include <Array.h>

const byte size = 10;
int rawArray[size] = {1,2,3,4,5,6,7,8,9,10};
Array array = Array(rawArray,size);

void setup(){
Serial.begin(9600);
Serial.println(“The contents of the array:”);
for (byte i=0; i<array.size(); i++){
Serial.print(array*);*

  • Serial.print(", ");*
  • }*
  • Serial.println("\nSpecial functionality:");*
  • Serial.print("\tMinimum value:");*
  • Serial.print(array.getMin());*
  • Serial.println();*
  • Serial.print("\tMaximum value:");*
  • Serial.print(array.getMax());*
  • Serial.println();*
  • Serial.print("\tAverage value:");*
  • Serial.print(array.getAverage());*
  • Serial.println();*
    }
    void loop(){/no loop/}[/quote]
    Serial Output:
    * *The contents of the array: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, Special functionality:      Minimum value:1      Maximum value:10      Average value:5* *
    This library also implements a simple bound check.
    Any attempted index below 0, will return the first element of the array. Any attempted index above arraySize will return the last element of the array.

But, like I said, in this application, it is the index of the maximum/minimum, not the value that is probably of more interest.

To be fair he said:

Is there a function to return the maximum value from that array?

If not how would you go about determining the maximum value?

I have provided a library that implements this, and he should feel free to be inspired by it and write his own function if the index is the variable of interest.

A matter of fact, I can implement the 11 lines needed right now, hopefully it’ll work :slight_smile:

int getIndexOfMaximumValue(int* array, int size){
  int maxIndex = 0;
  int max = array[maxIndex];
  for (int i=1; i<size; i++){
    if (max<array[i]){
      max = array[i];
      maxIndex = i;
    }
  }
  return maxIndex;
}

Thanks for the responces, and the function AlphaBeta. It works great, I had to change my thinking. With the index number I have the direction and the distance value.

#include <ServoTimer1.h>

ServoTimer1 servo1;
ServoTimer1 servo2;
int IR_Sensor = 2; // select the input pin for the
int ledPin = 13; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
int distance[18] = {0};
int servo_angle= {0,90,180,90};
int a;

void setup()
{
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println(“Servo test!”);
servo1.attach(10);
}

void loop()
{

for(int x = 0; x <= 3; x++)
{
servo1.write(servo_angle);
distance = ReadSens_and_Condition();
Serial.println(servo_angle);
Serial.println(distance);
Serial.println();
delay(500);
}
a=getIndexOfMaximumValue(distance,4);
Serial.print(“Max index is------”);
Serial.print(a);
Serial.print("------");
Serial.println(distance[a]);

}

int ReadSens_and_Condition()
{
int i;
int sval;

for (i = 0; i < 5; i++)
{
sval = sval + analogRead(IR_Sensor); // sensor on analog pin 0
}

sval = sval / 5; // average
return sval;
}

int getIndexOfMaximumValue(int* array, int size){
int maxIndex = 0;
int max = array[maxIndex];
for (int i=1; i<size; i++){
if (max<array*){*
_ max = array*;_
_
maxIndex = i;_
_
}_
_
}_
_
return maxIndex;_
_
}_
_
[/quote]*_

With the index number I have the direction and the distance value.

:sunglasses: