Using 3 ultrasound sensors without libraries and one LED


     const byte numSensors = 3; // const means constant variables, byte means small space in memory, numSensors is my variable name 
const byte ECHO_PIN[numSensors] = {2, 5, 8}; // We create an array to be used in a for loop 
const byte TRIG_PIN[numSensors] = {3, 4, 7};

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  for (byte s=0; s < numSensors; s++) { /* Due to for loop we reduce pinMode instructions from six to only two 
                                            pinMode(3, OUTPUT);
                                            pinMode(2, INPUT);
                                            pinMode(4, OUTPUT);
                                            pinMode(5, INPUT);
                                            pinMode(7, OUTPUT);
                                            pinMode(8, INPUT); */
                                            
    pinMode(TRIG_PIN[s], OUTPUT);
    pinMode(ECHO_PIN[s], INPUT);
  }
}

float readDistanceCM(byte s) { // This is a function with a parameter (s) and return a value that depends on the duration of a pulseIn function and a mathematical calculus with the velocity of the sound */
  digitalWrite(TRIG_PIN[s], LOW); // We need to have the trigger pin controlled to a low value in order to masure the duration of the high value that is proportional to the distance of the sensor */ 
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN[s], HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN[s], LOW);
  int duration = pulseIn(ECHO_PIN[s], HIGH);
  return duration * 0.034 / 2; // Divided by 2 because the sound goes and returns back
}

void loop() { 
  bool isNearby = false;
  Serial.print("Measured distance: ");
  for (byte s=0; s < numSensors; s++) {
    float distance = readDistanceCM(s);
    isNearby |= distance < 100;
    Serial.print(distance);
    Serial.print(" ");
  }
  Serial.println();
  digitalWrite(LED_BUILTIN, isNearby);
  delay(100);
}
     
     
     
     

int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;

int trigPin1 = 6;  // trigPin és el emisor del ultrasonido
int echoPin1 = 7;  // echoPin és la señal que recibimos   

int trigPin2 = 8;
int echoPin2 = 9;

int trigPin3 = 10;
int echoPin3 = 11;

void setup() {
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
 
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
 
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
 
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  digitalWrite(ledPin3, HIGH);
}

void firstsensor(){ // This function is for first sensor.
  int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (10);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;  // 29.1 es la velocidad del sonido

      Serial.print("1st Sensor: ");
      Serial.print(distance1); 
      Serial.print("cm    ");

  if (distance1 < 50) {  // Change the number for long or short distances.
    digitalWrite (ledPin1, HIGH);
  } else {
    digitalWrite (ledPin1, LOW);
  }   
}
void secondsensor(){ // This function is for second sensor.
    int duration2, distance2; // I created 2 local variables to have an space in memory in my microcoontroler memory
    digitalWrite (trigPin2, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin2, LOW); // Stop the emition of ultrasound after 10 microseconds
    duration2 = pulseIn (echoPin2, HIGH); //Activates echoPin in HIGH to recive a ultrasound backwards
   // duration2 keeps the value obtained by echoPin2 
    distance2 = (duration2/2) / 29.1; 
   // It converts value of the ultrasound duration like a time of flight (TOF) into a distance with using a mathematic 
      Serial.print("2nd Sensor: ");
      Serial.print(distance2); 
      Serial.print("cm    ");
  
    if (distance2 < 50) {  // Change the number for long or short distances.
      digitalWrite (ledPin2, HIGH);
    }
 else {
      digitalWrite (ledPin2, LOW);
    }   
}
void thirdsensor(){ // This function is for third sensor.
    int duration3, distance3;
    digitalWrite (trigPin3, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin3, LOW);
    duration3 = pulseIn (echoPin3, HIGH);
    distance3 = (duration3/2) / 29.1;

      Serial.print("3rd Sensor: ");  
      Serial.print(distance3); 
      Serial.print("cm    ");
  
    if (distance3 < 50) {  // Change the number for long or short distances.
      digitalWrite (ledPin3, HIGH);
    }
 else {
      digitalWrite (ledPin3, LOW);
    }  
}

void loop() {
Serial.println("\n");
firstsensor();
secondsensor();
thirdsensor();
  delay(100);
}

Souliton with LiquidCrystal library

Here

#include < Adafruit_LiquidCrystal.h >

#include < Adafruit_LiquidCrystal.h>

#include "LiquidCrystal.h"  
 LiquidCrystal lcd(A0, A1, A2, A3, A4, A5);  
 const int trigPin1 = 9;  
 const int echoPin1 = 10;  
 const int trigPin2 = 5;  
 const int echoPin2 = 6;  
 const int trigPin3 = 7;  
 const int echoPin3 = 8;  
 long duration1;  
 long duration2;  
 long duration3;  
 int distanceCm1, distanceInch1;  
 int distanceCm2, distanceInch2;  
 int distanceCm3, distanceInch3;  
 void setup() {  
 Serial.begin(9600);   
  lcd.begin(16,2);  
 pinMode(trigPin1, OUTPUT);  
 pinMode(echoPin1, INPUT);  
 pinMode(trigPin2, OUTPUT);  
 pinMode(echoPin2, INPUT);  
 pinMode(trigPin3, OUTPUT);  
 pinMode(echoPin3, INPUT);  
 pinMode(12,OUTPUT);  
 pinMode(11,OUTPUT);  
 pinMode(3,OUTPUT);  
 }  
 void loop()  
 {  
 digitalWrite(trigPin1, LOW);  
 delayMicroseconds(2);  
 digitalWrite(trigPin1, HIGH);  
 delayMicroseconds(10);  
 digitalWrite(trigPin1, LOW);  
 duration1 = pulseIn(echoPin1, HIGH);  
 digitalWrite(trigPin2, LOW);  
 delayMicroseconds(2);  
 digitalWrite(trigPin2, HIGH);  
 delayMicroseconds(10);  
 digitalWrite(trigPin2, LOW);  
 duration2 = pulseIn(echoPin2, HIGH);  
 digitalWrite(trigPin3, LOW);  
 delayMicroseconds(2);  
 digitalWrite(trigPin3, HIGH);  
 delayMicroseconds(10);  
 digitalWrite(trigPin3, LOW);  
 duration3 = pulseIn(echoPin3, HIGH);  
 distanceCm1= duration1*0.034/2;  
 distanceInch1 = duration1*0.0133/2;  
 distanceCm2= duration2*0.034/2;  
 distanceInch2 = duration2*0.0133/2;  
 distanceCm3= duration3*0.034/2;  
 distanceInch3 = duration3*0.0133/2;  
 Serial.print("Distance1: ");  
 Serial.println(distanceCm1);  
 lcd.clear();  
  lcd.setCursor(0,0);  
  lcd.print("Ultrasonic1 ");  
  lcd.setCursor(12,0);  
  lcd.print(distanceCm1);  
 //delay(500);  
 Serial.print("Distance2: ");  
 Serial.println(distanceCm2);  
  lcd.setCursor(0,1);  
  lcd.print("U2 ");  
  lcd.setCursor(3,1);  
  lcd.print(distanceCm2);  
 Serial.print("Distance3: ");  
 Serial.println(distanceCm3);  
  lcd.setCursor(9,1);  
  lcd.print("U3 ");  
  lcd.setCursor(12,1);  
  lcd.print(distanceCm3);  
 if(distanceCm1 < 150)  
 {  
   digitalWrite(3,HIGH); // LED ON  
 }  
 if(distanceCm1 >150)  
 {  
   digitalWrite(3,LOW);  // LED OFF  
 }  
 if(distanceCm2 < 150)  
 {  
   digitalWrite(12,HIGH); // LED ON  
 }  
 if(distanceCm2 > 150)  
 {  
   digitalWrite(12,LOW); // LED OFF  
 }  
 if(distanceCm3 < 150)  
 {  
   digitalWrite(11,HIGH); // LED ON  
 }  
 if(distanceCm3 > 150)   
 {  
   digitalWrite(11,LOW); // LED OFF  
 }  
 delay(1000);  
 }