Need help for multiple ultrasonic sensor code

I'm new to working with Arduino, and I want to get data seperately from 4 ultrasonic sensors for an art project in TouchDesigner, but my code doesn't work properly. I don't know if my code is a shitshow because it's my first time using the Arduino, and I changed some stuff in the code myself. Does anyone maybe knows how to make the code better, so it works properly?

int TriggerPIN1 = 9;
int EchoPIN1 = 10;
int TriggerPIN2 = 5;
int EchoPIN2 = 4;
int TriggerPIN3 = 12;
int EchoPIN3 = 13;
int TriggerPIN4 = 7;
int EchoPIN4 = 6;
int DURATION;
int DISTANCE;

void setup(){
  pinMode(TriggerPIN1,OUTPUT);
  pinMode(EchoPIN2,INPUT);
  pinMode(TriggerPIN2,OUTPUT);
  pinMode(EchoPIN2,INPUT);
  pinMode(TriggerPIN3, OUTPUT);
  pinMode(EchoPIN3, INPUT);
  pinMode(TriggerPIN4, OUTPUT);
  pinMode(EchoPIN4, INPUT);
  Serial.begin(9600); 
  }

void loop() {

  digitalWrite(TriggerPIN1, HIGH);
  delay(1);
  digitalWrite(TriggerPIN1, LOW);
  DURATION = pulseIn(EchoPIN1, HIGH);
  DISTANCE = DURATION / 58.2;

  Serial.print("Sensor 1 : ");

  if(DISTANCE > 0 && DISTANCE < 50){
    Serial.println(DISTANCE);
    delay(100);
  }

  digitalWrite(TriggerPIN2, HIGH);
  delay(1);
  digitalWrite(TriggerPIN2, LOW);
  DURATION = pulseIn(EchoPIN2, HIGH);
  DISTANCE = DURATION / 58.2;

  Serial.print("Sensor 2 : ");

  if(DISTANCE > 0 && DISTANCE < 50){
    Serial.println(DISTANCE);
    delay(100);
  }

  digitalWrite(TriggerPIN3, HIGH);
  delay(1);
  digitalWrite(TriggerPIN3, LOW);
  DURATION = pulseIn(EchoPIN3, HIGH);
  DISTANCE = DURATION / 58.2;

  Serial.print("Sensor 3 : ");

  if(DISTANCE > 0 && DISTANCE < 50){
    Serial.println(DISTANCE);
    delay(100);
  }
  
  digitalWrite(TriggerPIN4, HIGH);
  delay(1);
  digitalWrite(TriggerPIN4, LOW);
  DURATION = pulseIn(EchoPIN4, HIGH);
  DISTANCE = DURATION / 58.2;

  Serial.print("Sensor 4 : ");

  if(DISTANCE > 0 && DISTANCE < 50){
    Serial.println(DISTANCE);
    delay(100);
  }
  
}

This is what the serial monitor shows

Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : Sensor 2 : Sensor 3 : Sensor 4 : Sensor 1 : 

I need to get the distances from each sensor seperately underneath eachother between 0 and 50 in the serial monitor. For example:

Sensor 1: 23
Sensor 2: 9
Sensor 3: 12
Sensor 4: 43
Sensor 2: 34
Sensor 1: 49

You'll have to talk us through that phrase.

My guess is that you're pinging too frequently.

Yes, your code could be shorter and neater.

I've changed the description a little, hopefully you'll understand it better like this.
Thank you for your reply.

OK, but those headers are going to scroll off the top of the serial monitor.
Is it important they remain visible, or are they just there for debug?

(Thanks for using code tags on your first post too! Yay!)

For testing, I suggest that you comment out each if statement and leave the println in there. Then at least you'll see what distance you're getting.

Compiled, but untested.

// trigger/echo pin pairs.
const byte rangers [][2] { {9, 10}, {5,4}, {12, 13}, {7, 6}};

void setup()
{
  for (auto& ranger : rangers) {
    pinMode(ranger [0], OUTPUT);
    digitalWrite (ranger [0], LOW);
    pinMode(ranger [1], INPUT);
  }
  Serial.begin(9600); 
}

int range (const byte ranger [])
{
  digitalWrite(ranger [0], HIGH);
  delayMicroseconds (10);
  digitalWrite(ranger [0], LOW);
  unsigned long duration = pulseIn(ranger [1], HIGH);
  return duration / 58.2;
}

void loop() 
{
  for (int i = 0; i < 4; i++) {
    int distance = range (rangers [i]);
    Serial.print(F("Sensor "));
    Serial.print (i + 1);
    Serial.print(F(" : "));
    Serial.println(distance);
    
    if(distance > 0 && distance < 50){
      // some action here?
    }
    delay (50); // Ping no more frequently than 20 Hz
  }  
}

1 Like

I use the headers to know which sensor is working in TouchDesigner, because each sensor has to do something different to a visual 3d object. If I dont use the headers I'll only get all the distances in TouchDesigner without knowing from which sensor it is.

I'll check it out, thanks!

Sorry, no idea what TouchDesigner is, but originally, I thought you wanted the results in vertical columns, with headings.

The code is almost like how I want it to be, thank you! The only thing is that I want the distances to be between 0 and 50. But I could see if I could change that myself.
Thanks again for you help.

    Serial.print(F("Sensor "));
    Serial.print (i + 1);
    Serial.print(F(" : "));

    
    if(distance > 0 && distance < 50){
      Serial.print(distance);
    }
    Serial.println ();
    delay (50); // Ping no more frequently than 20 Hz

Like that?
(this time uncompiled, untested)

1 Like

Yes, that's perfect!
Thank you so much :hugs: very much appreciated!