Request for member 'ping_median' in 'sonar', which is of non-class type 'NewPing [3]'

Hi,
a question about NewPing again, the example of NewPing3Sensors_ls from NewPing defined NewPing sonar[SONAR_NUM] and NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); in NewPingTimerMedian_M.
How can I use them the way below?
Please let me know If need I'll upload the examples.
Thanks
Adam

// ---------------------------------------------------------------------------
// Example NewPing library sketch that pings 3 sensors 20 times a second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define SONAR_NUM 3      // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. 
  NewPing(6, 7, MAX_DISTANCE), 
  NewPing(8, 9, MAX_DISTANCE)
};

float duration, distance;

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() { 
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    Serial.print(i);
    Serial.print("=");
    Serial.print(sonar[i].ping_cm());
    Serial.print("cm ");
  }
  Serial.println();
  duration = sonar.ping_median(iterations);
  
}

ERROR:

Arduino: 1.8.3 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\HUA.DELLV-PC\Documents\Arduino\NewPing3Sensors_ls\NewPing3Sensors_ls.ino: In function 'void loop()':

NewPing3Sensors_ls:32: error: request for member 'ping_median' in 'sonar', which is of non-class type 'NewPing [3]'

   duration = sonar.ping_median(iterations);

                    ^

NewPing3Sensors_ls:32: error: 'iterations' was not declared in this scope

   duration = sonar.ping_median(iterations);

                                ^

exit status 1
request for member 'ping_median' in 'sonar', which is of non-class type 'NewPing [3]'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

For which element of the array sonar did you intend the operation?

1 Like

Thanks.
I modified as:

void loop() { 
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    Serial.print(i);
    Serial.print("=");
    Serial.print(sonar[i].ping_cm());
    Serial.print("cm ");

    duration[i] = sonar.ping_median(iterations);
  }
  Serial.println();
  ///// duration[i] = sonar.ping_median(iterations);
  
}

still got error:

Arduino: 1.8.3 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\HUA.DELLV-PC\Documents\Arduino\NewPing3Sensors_ls\NewPing3Sensors_ls.ino: In function 'void loop()':

NewPing3Sensors_ls:34: error: request for member 'ping_median' in 'sonar', which is of non-class type 'NewPing [3]'

     duration[i] = sonar.ping_median(iterations);

                         ^

NewPing3Sensors_ls:34: error: 'iterations' was not declared in this scope

     duration[i] = sonar.ping_median(iterations);

                                     ^

exit status 1
request for member 'ping_median' in 'sonar', which is of non-class type 'NewPing [3]'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I guess the ping_median need : NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
I just don't want to use like trig1/trig2...,
any easy way?

sonar is an array.
Arrays need subscripts.

1 Like

Great!!!
Works now, thank you.
Edit and see if got some clue?
The code run well, but the results is not right, why?

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