How to change these lines into an array?

Hi,
How to change these 3 lines into an array sonar[3] ?
Thanks
Adam

NewPing sonar1(trigPin1, echoPin1, MAX_DISTANCE); 
NewPing sonar2(trigPin2, echoPin2, MAX_DISTANCE); 
NewPing sonar3(trigPin3, echoPin3, MAX_DISTANCE); 

..........................
 sonar[3]

Look at the example sketch NewPin3Sensors that comes with the library:

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

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

Thanks.
Can you just simply help on the question please?

It looks like he did it in the code he posted, in the first section here:

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

If your typical array looks like this:

int X[] = { 0, 5, 3, ect};

He simply put each variable on a separate line, like this:

int X[] = {
0,
5,
3,
ect
};

So if you did it the typical way you see arrays, it would look something like this, which is a bit harder to read:

NewPing sonar[SONAR_NUM] = { NewPing(4, 5, MAX_DISTANCE), NewPing(6, 7, MAX_DISTANCE), NewPing(8, 9, MAX_DISTANCE) };

Correct me if I'm wrong @david_2018

1 Like
NewPing sonar[3] =
{
  NewPing (trigPin1, echoPin1, MAX_DISTANCE),
  NewPing (trigPin2, echoPin2, MAX_DISTANCE),
  NewPing (trigPin3, echoPin3, MAX_DISTANCE)
};
1 Like

Thank you.
This one is very close to what I like to have. All my purpose is to use: duration[i] = sonar[i].ping_median(iterations); and get the correct readings for multi-sensor.
The code attached below get there but just for one sensor, would you please help me to combine your sketch and my code to achieve:
duration[i] = sonar[i].ping_median(iterations);
I made one: sonar[3]=(trigPin[3], echoPin[3], MAX_DISTANCE); kind of fool, just no work. ERROR below.
please.
Thank you.

// Include the library:
#include <NewPing.h>

// Define pins and max distance:
const int trigPinL = 2;
const int echoPinL = 3;

const int trigPinC = 4;
const int echoPinC = 5;

const int trigPinR = 7;
const int echoPinR = 8;

const int trigPinB = A0;
const int echoPinB = A1;

int trigPin[4] = {2, 4, 7, A0 };
int echoPin[4] = {3, 5, 8, A1 };

#define SONAR_NUM 4      // Number of sensors.
#define MAX_DISTANCE 350 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
#define iterations 5      // Number of sensors.
/////// NewPing sonar(trigPin, echoPin, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

NewPing sonarL(trigPinL, echoPinL, MAX_DISTANCE);
NewPing sonarC(trigPinC, echoPinC, MAX_DISTANCE);
NewPing sonarR(trigPinR, echoPinR, MAX_DISTANCE);
NewPing sonarB(trigPinB, echoPinB, MAX_DISTANCE);

float durationL, distanceC, durationR, durationB, distanceL, durationC, distanceR,  distanceB;

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

void loop() {
  delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

  durationL = sonarL.ping_median(iterations);
  durationC = sonarC.ping_median(iterations);
  durationR = sonarR.ping_median(iterations);
  durationB = sonarB.ping_median(iterations);

  Serial.print("durationL=");
  Serial.print(durationL);
  Serial.print("    durationC=");
  Serial.print(durationC);
  Serial.print("    durationR=");
  Serial.print(durationR);
  Serial.print("    durationB=");
  Serial.println(durationB);

  distanceL = (durationL / 2) * 0.0343;
  distanceC = (durationC / 2) * 0.0343;
  distanceR = (durationR / 2) * 0.0343;
  distanceB = (durationB / 2) * 0.0343;

  Serial.print("distanceL=");
  Serial.print(distanceL);
  Serial.print("    distanceC=");
  Serial.print(distanceC);
  Serial.print("    distanceR=");
  Serial.print(distanceR);
  Serial.print("    distanceB=");
  Serial.println(distanceB);
}

ERROR:

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

duration_i_sonar_i_M:29: error: conversion from 'uint8_t {aka unsigned char}' to non-scalar type 'NewPing' requested

 NewPing sonar[3]={trigPin[3], echoPin[3], MAX_DISTANCE};

                            ^

duration_i_sonar_i_M:29: error: conversion from 'uint8_t {aka unsigned char}' to non-scalar type 'NewPing' requested

 NewPing sonar[3]={trigPin[3], echoPin[3], MAX_DISTANCE};

                                        ^

duration_i_sonar_i_M:29: error: conversion from 'int' to non-scalar type 'NewPing' requested

 NewPing sonar[3]={trigPin[3], echoPin[3], MAX_DISTANCE};

                                                       ^

exit status 1
conversion from 'uint8_t {aka unsigned char}' to non-scalar type 'NewPing' requested

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

sm.11

I note that the below will verify, does it do something different to the code in #2?

I just happen to be looking at aggregate initialisation recently, can't find it now but the syntax below compiled for me in another circumstance and appeared to do what I wanted.

# 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.
  {4, 5, MAX_DISTANCE}, // Each sensor's trigger pin, echo pin, and max distance to ping. 
  {6, 7, MAX_DISTANCE}, 
  {8, 9, MAX_DISTANCE}
};

void setup{}
void loop{}

TIA

a7

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