I am trying to find the max value in a C++ array (Arduino). Is there a faster way to do this, than how I have tried below?
const int arraySize = 5;
float currentLargest;
void setup() {
Serial.begin(115200);
}
void loop() {
float accelVals[arraySize] = {-1.0,-2.0,-10.0,-10.0,-2.2}; //X, Y, Z
Serial.println(findLargest(accelVals));
}
float findLargest(float arr[arraySize])
{
for (int i = 0; i < arraySize; i++)
{
if (i == 0)
{
currentLargest = max(arr[i], arr[i+1]);
}else{
currentLargest = max(arr[i], currentLargest);
}
}
return currentLargest;
}
The actual array I am trying to solve is much larger, approximately 2000 values, and I need to solve them dynamically as quickly as possible. This method is slow, and I am hoping there is a better way.
Thanks!