Finding the biggest value from sensor data

My Arduino Mega 2560 arrived in the post this morning!

I was hoping that somebody would be able to help me with the code to find the HC-SR04 sensor outputting the biggest value.
I am using the example code provided on the 'NewPing' page but I have changed the number of sensors to 3 rather than 15.

#include <NewPing.h>
 
#define SONAR_NUM     3 // Number or sensors.
#define MAX_DISTANCE 200 // Max distance in cm.
#define PING_INTERVAL 33 // Milliseconds between pings.
 
unsigned long pingTimer[SONAR_NUM]; // When each pings.
unsigned int cm[SONAR_NUM]; // Store ping distances.
uint8_t currentSensor = 0; // Which sensor is active.
 
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(40, 41, MAX_DISTANCE),
NewPing(42, 43, MAX_DISTANCE),
NewPing(44, 45, MAX_DISTANCE),
};
 
void setup() {
  Serial.begin(115200);
  pingTimer[0] = millis() + 75; // First ping start in ms.
  for (uint8_t i = 1; i < SONAR_NUM; i++)
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}
 
void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    if (millis() >= pingTimer[i]) {
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;
      if (i == 0 && currentSensor == SONAR_NUM - 1)
        oneSensorCycle(); // Do something with results.
      sonar[currentSensor].timer_stop();
      currentSensor = i;
      cm[currentSensor] = 0;
      sonar[currentSensor].ping_timer(echoCheck);
    }
  }
  // The rest of your code would go here.
}
 
void echoCheck() { // If ping echo, set distance to array.
  if (sonar[currentSensor].check_timer())
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}
 
void oneSensorCycle() { // Do something with the results.
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    Serial.print(i);
    Serial.print("=");
    Serial.print(cm[i]);
    Serial.print("cm ");
  }
  Serial.println();

I assume that I would put it after the "void oneSensorCycle()" bit of code if somebody would please be able to help me work out how to get the Arduino to feedback which sensor is giving out the highest distance value.
Thank you.

You need to store the sensor values in 3 different variables (e.g. sensorVal1 .. sensorVal3).
Then you can go on with this Arduino command.

  1. compare sensorVal1 with sensorVal2
  2. take the sensorVal with the biggest value and compare it with sensorVal3.
  3. The result of that comparison is what you were looking for.

I have not yet dealt with that command, but maybe the following may work (one line of code):

maxValue = max(max(sensorVal1, sensorVal2), sensorVal3);

@rpt007 The link was useful thank you! Does that mean that I cannot use what appears to be an array?

NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(40, 41, MAX_DISTANCE),
NewPing(42, 43, MAX_DISTANCE),
NewPing(44, 45, MAX_DISTANCE),

I wouldn't know how to assign the sensor values from those to a variable.

You have to select each array item by calling its index (in your case: SONAR_NUM) and then assign its values to the variables.
You will have to study how to read a single item of an array.

There are tons of examples around Arduino. The forum search or Aunt Google will help you.

void oneSensorCycle() { // Do something with the results.
    int max = cm[0];
    int sensor = 0;
   for (uint8_t i = 1; i < SONAR_NUM; i++) {
      if (cm[i] > max) {
         max = cm[i];
         sensor = i;
       }
    }
    Serial.print(F("Max distance is "));
    Serial.print(max);
    Serial.print(F(" cm from sensor "));
    Serial.println(sensor);
}