about Array operations

I am testing two Ultrasonic Sensors HC-SR04, it is works good. I just wonder if I can use Array operations to deal with more sensors say 6 of them as follow:
Thanks.

int UlsEcho[] = {UlsBinLE, UlsBinRE, UlsFrontE, UlsBackE, UlsLeftE, UlsRightE};

for (int i = 0; i < 6; i++) // 
    {
      duration[i] = pulseIn(UlsEcho[i], HIGH);
      // Calculating the distance
      distance[i] = duration[i] * 0.034 / 2;

      Val[i] = map(distance[i], 0, 1023, 0, 100);
    }

here is the whole code:

// defines pins numbers
const int trigPinA = 31;
const int echoPinA = 29;

const int trigPinB = 26;
const int echoPinB = 24;

// defines variables
long durationA;
int distanceA;

const int ledA = 8;
const int ledB = 9;
const int BUZZER = 10;

int ValA = 0;
int ValB = 0;
int T0 = 5;

long durationB;
int distanceB;

void setup() {
  pinMode(trigPinA, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPinA, INPUT); // Sets the echoPin as an Input

  pinMode(trigPinB, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPinB, INPUT); // Sets the echoPin as an Input

  pinMode(BUZZER, OUTPUT); // Sets the trigPin as an Output

  Serial.begin(9600); // Starts the serial communication
}
void loop() {

  digitalWrite(BUZZER, LOW);

  // Clears the trigPin
  digitalWrite(trigPinA, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPinA, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinA, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  durationA = pulseIn(echoPinA, HIGH);
  // Calculating the distance
  distanceA = durationA * 0.034 / 2;

  ValA = map(distanceA, 0, 1023, 0, 100);

  // Prints the distance on the Serial Monitor
  Serial.print("DistanceA: ");
  Serial.println(distanceA);

  // Clears the trigPin
  digitalWrite(trigPinB, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPinB, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPinB, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  durationB = pulseIn(echoPinB, HIGH);
  // Calculating the distance
  distanceB = durationB * 0.034 / 2;

  ValB = map(distanceB, 0, 1023, 0, 100);

  // Prints the distance on the Serial Monitor
  Serial.print("DistanceB: ");
  Serial.println(distanceB);

  if (ValA - ValB <= T0) {
    digitalWrite(ledA, HIGH);
    delay(100);
    digitalWrite(ledA, LOW);
  }

  if (ValA - ValB <= T0 && ValA < ValB) {
    digitalWrite(ledB, HIGH);
    delay(100);
    digitalWrite(ledB, LOW);

    digitalWrite(BUZZER, LOW);
    delay(1000);
  }
}

You should take a look at the NewPing library. One of the examples show how to use 15 ultrasonic range sensors.

In your sketch you can use arrays for pin numbers:

const byte SensorCount = 2;


// defines pins numbers
const int trigPins[SensorCount] = {31, 26};
const int echoPins[SensorCount] = {29, 24};


unsigned Vals[SensorCount];

const int ledA = 8;
const int ledB = 9;
const int BUZZER = 10;


const int T0 = 5;


void setup()
{
  Serial.begin(9600); // Starts the serial communication


  for (int i = 0; i < SensorCount; i++)
  {
    pinMode(trigPins[i], OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPins[i], INPUT); // Sets the echoPin as an Input
  }


  pinMode(BUZZER, OUTPUT); // Sets the trigPin as an Output
}


void loop()
{
  digitalWrite(BUZZER, LOW);


  for (int i = 0; i < SensorCount; i++)
  {




    // Clears the trigPin
    digitalWrite(trigPins[i], LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPins[i], HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPins[i], LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    unsigned long duration = pulseIn(echoPins[i], HIGH);
    // Calculating the distance
    unsigned distance = duration * 0.034 / 2;


    Vals[i] = map(distance, 0, 1023, 0, 100);  // ????


    // Prints the distance on the Serial Monitor
    Serial.print("Vals[");
    Serial.print(i);
    Serial.print("]: ");
    Serial.println(Vals[i]);


    delay(25); // Wit for late echoes to subside
  }




  if (Vals[0] - Vals[1] <= T0)
  {
    digitalWrite(ledA, HIGH);
    delay(100);
    digitalWrite(ledA, LOW);
  }


  if (Vals[0] - Vals[1] <= T0 && Vals[0] < Vals[1])
  {
    digitalWrite(ledB, HIGH);
    delay(100);
    digitalWrite(ledB, LOW);


    digitalWrite(BUZZER, LOW);
    delay(1000);
  }
}

johnwasser:
You should take a look at the NewPing library. One of the examples show how to use 15 ultrasonic range sensors.

GitHub - microflo/NewPing: Fork of arduino-new-ping

In your sketch you can use arrays for pin numbers:

const byte SensorCount = 2;

// defines pins numbers
const int trigPins[SensorCount] = {31, 26};
const int echoPins[SensorCount] = {29, 24};

}

Thank you.

I tested your sketch works fine. and I signed each ultrasonic sensor a specific name such as 'front'/'right'/'left'/'right'/'top'/'bottom' , not the just sensor1/2/3..., I have totally six ultrasonic sensors. Can I still use newping?

Best.

I made the array seems compiling is fine, but have only one Ultrasonic Sensor read. the sensors are good, because I exchanged them, still only channel 1 read. what's wrong here?
Best

// defines pins numbers
const int trigPinA = 26;
const int echoPinA = 27;

const int trigPinB = 28;
const int echoPinB = 29;

// defines variables
long durationA;
int distanceA;

long durationB;
int distanceB;

const int ledA = 10;
const int ledB = 9;
const int Buzzer = 8;

int ValA = 0;
int ValB = 0;
int T0 = 5;

int UlsTrig[] = {trigPinA, trigPinB};
int UlsEcho[] = {echoPinA, echoPinB};
long durationRead[] = {0};
long distanceCal[] = {0};
long distanceMap[] = {0};
long cm[] = {0};
long inch[] = {0};
long cmdistanceMap[] = {0};

void setup() {
  Serial.begin(115200);

  pinMode(trigPinA, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPinA, INPUT); // Sets the echoPin as an Input

  pinMode(trigPinB, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPinB, INPUT); // Sets the echoPin as an Input

  pinMode(Buzzer, OUTPUT); // Sets the trigPin as an Output
}
void loop() {

  digitalWrite(Buzzer, LOW);

  ////

  // Ultrasonic Sensor HC-SR04 input pins Clears, all set to LOW.
  for (int i = 0; i < 2; i++) // use for Buttons, limt switches, Uls sensors pin setup
  {
    digitalWrite(UlsTrig[i], LOW);
    delayMicroseconds(2);
  }
  // Sets the trigPin on HIGH state for 10 micro seconds
  for (int i = 0; i < 2; i++) // use for Buttons, limt switches, Uls sensors pin setup
  {
    digitalWrite(UlsTrig[i], HIGH);
    delayMicroseconds(10);
  }

  for (int i = 0; i < 2; i++) // use for Buttons, limt switches, Uls sensors pin setup
  {
    digitalWrite(UlsTrig[i], LOW);
  }

  // Reads the echoPin, returns the sound wave travel time in microseconds
  for (int i = 0; i < 2; i++) // use for Buttons, limt switches, Uls sensors pin setup
  {
    durationRead[i] = pulseIn(UlsEcho[i], HIGH);

  }

  // Calculating the distance
  for (int i = 0; i < 2; i++) // use for Buttons, limt switches, Uls sensors pin setup
  {
    distanceCal[i] = durationRead[i] * 0.034 / 2;
  }

  for (int i = 0; i < 2; i++) // use for Buttons, limt switches, Uls sensors pin setup
  {
    cm[i] = ( distanceCal[i] / 2 ) / 29.1 ;
    inch[i] = ( distanceCal[i] / 2 ) / 74 ;


    // Mapping the distance
    for (int i = 0; i < 2; i++) // use for Buttons, limt switches, Uls sensors pin setup
    {
      //  cmdistanceMap[i] = map(cm[i], 0, 1023, 0, 100);  // the number is too small after map
      Serial.print("cm");
      Serial.println(i);
      Serial.println(cm[i]);
    }

      if (ValA - ValB <= T0) {
      digitalWrite(ledA, HIGH);
      delay(100);
      digitalWrite(ledA, LOW);
    }

    if (ValA - ValB <= T0 && ValA < ValB) {
      digitalWrite(ledB, HIGH);
      delay(100);
      digitalWrite(ledB, LOW);

      digitalWrite(Buzzer, LOW);
      delay(1000);
    }
  }
}

Those arrays only have one element so you memory overflow

 long durationRead[] = {0};
long distanceCal[] = {0};
long distanceMap[] = {0};

Also you can’t fire your ultrasonic sensors’ pings at the same time as waves likely will get mixed up
Trigger them one after another, with a bit of delay in between two successive pings

I think that's a physical problem. You send a ping from all sensors at the same time. That creates interference between the received signals (because each reciever is picking up all the reflected signals).

J-M-L:
Those arrays only have one element so you memory overflow

 long durationRead[] = {0};

long distanceCal[] = {0};
long distanceMap[] = {0};




Also you can’t fire your ultrasonic sensors’ pings at the same time as waves likely will get mixed up
Trigger them one after another, with a bit of delay in between two successive pings

aarg:
I think that's a physical problem. You send a ping from all sensors at the same time. That creates interference between the received signals (because each reciever is picking up all the reflected signals).

Thank you.
It is the point, I did get the result when I operate ultrasonic sensors one by one.

Best

It is the point, I did get the result when I operate ultrasonic sensors one by one.

Make note of the size of your array though and the type of the data you store in there

ideally you would do something like this

const byte UlsTrig[] = {trigPinA, trigPinB};
// count how many we have
const byte numberOfUltrasonicSensors = sizeof(UlsTrig) / sizeof(UlsTrig[0]);

// from then on, ensure we have the right size for the arrays
const byte UlsEcho[numberOfUltrasonicSensors] = {echoPinA, echoPinB};

// global variables are initialized to 0 by the compiler if no default value is provided
long durationRead[numberOfUltrasonicSensors]; 
long distanceCal[numberOfUltrasonicSensors];
long distanceMap[numberOfUltrasonicSensors];
long cm[numberOfUltrasonicSensors];
long inch[numberOfUltrasonicSensors];
long cmDistanceMap[numberOfUltrasonicSensors];

J-M-L:
Make note of the size of your array though and the type of the data you store in there

ideally you would do something like this

const byte UlsTrig[] = {trigPinA, trigPinB};

// count how many we have
const byte numberOfUltrasonicSensors = sizeof(UlsTrig) / sizeof(UlsTrig[0]);

// from then on, ensure we have the right size for the arrays
const byte UlsEcho[numberOfUltrasonicSensors] = {echoPinA, echoPinB};

// global variables are initialized to 0 by the compiler if no default value is provided
long durationRead[numberOfUltrasonicSensors];
long distanceCal[numberOfUltrasonicSensors];
long distanceMap[numberOfUltrasonicSensors];
long cm[numberOfUltrasonicSensors];
long inch[numberOfUltrasonicSensors];
long cmDistanceMap[numberOfUltrasonicSensors];

Thanks
I have a new question.
I modified the NEWPING sketch into 6 sensors, works well, but got error when I added Buzzer into it. got 'not for arduino board' error, I changed Tone.cpp timer2 as timer3, the Buzzer doesn't work, why?
Best

Timer2 is used by the newping library and so does the tone function

What arduino do you use?