Ultrasonic sensor sense very slowly and also gives the garbage value

how to reduce the delay of ultrasonic sensor

#define Grove_Water_Sensor 12 // Attach Water sensor to Arduino Digital Pin 8
 #include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};
int trigPin1 = 6;
int echoPin1 = 7;

int trigPin2 = 8;
int echoPin2 = 9;

int trigPin3 = 10;
int echoPin3 = 11;
int speakerPin1 = 3;
int speakerPin2 =4;
int speakerPin3 =5;
int led_pin = 13;


struct MusicStruct {
  int A = 550;
  int As = 582;
  int B = 617;
  int C = 654;
  int Cs = 693;
  int D = 734;
  int Ds = 777;
  int E = 824;
  int F = 873;
  int Fs = 925;
  int G = 980;
  int Gs = 1003;
  int A2 = 1100;
  int A2s = 1165;
  int B2 = 1234;
  int C3 = 1308;
  int C3s = 1385;
  int D3 = 1555;
}Music;

struct LengthStruct {
  float half = 0.5;
  float one = 1.0;
  float one_half = 1.5;
  float two = 2.0;
  float two_half = 2.5;
}Length;

int tempo = 400;
void setup() {
  pinMode(Grove_Water_Sensor, INPUT); // The Water Sensor is an Input
   pinMode(A0, OUTPUT); 
  Serial.begin (9600);
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  
  pinMode(speakerPin1, OUTPUT);
  pinMode(speakerPin2, OUTPUT);
  pinMode(speakerPin3, OUTPUT);
  pinMode(led_pin, OUTPUT);
  
   // iterate over the notes of the melody:
 
  
}
void setTone(int pin, int note, int duration) {
  tone(pin, note, duration);
  delay(duration);
  noTone(pin);
}
void firstsensor(){ // This function is for first sensor.
  int duration1, distance1;
  digitalWrite (trigPin1, HIGH);
  delayMicroseconds (100);
  digitalWrite (trigPin1, LOW);
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;

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

  if (distance1 < 30) { 
     for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(3, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(3);
     }// Change the number for long or short distances.
    
    
  } else {
    digitalWrite (speakerPin1, LOW);
  }    
}
void secondsensor(){ // This function is for second sensor.
    int duration2, distance2;
    digitalWrite (trigPin2, HIGH);
    delayMicroseconds (10);
    digitalWrite (trigPin2, LOW);
    duration2 = pulseIn (echoPin2, HIGH);
    distance2 = (duration2/2) / 29.1;
  
      Serial.print("2nd Sensor: "); 
      Serial.print(distance2);  
      Serial.print("cm    ");
   
    if (distance2 < 30) { 
       tone(speakerPin2, 500,1000);
       
    }
 else {
      digitalWrite (speakerPin2, 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 < 30) {
       

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 ;
    
// Change the number for long or short distances.
  setTone(speakerPin3, Music.B, tempo * Length.one);
  setTone(speakerPin3, Music.E, tempo * Length.one_half);
  setTone(speakerPin3, Music.G, tempo * Length.half);
  setTone(speakerPin3, Music.F, tempo * Length.one);
  setTone(speakerPin3, Music.E, tempo * Length.two);
  setTone(speakerPin3, Music.B2, tempo * Length.one);
  setTone(speakerPin3, Music.A2, tempo * Length.two_half);
  setTone(speakerPin3, Music.Fs, tempo * Length.two_half);
  
  setTone(speakerPin3, Music.E, tempo * Length.one_half);
 setTone(speakerPin3, Music.G, tempo * Length.half);
 //setTone(speakerPin3, Music.F, tempo * Length.one);
 // setTone(speakerPin3, Music.Ds, tempo * Length.two);
//  setTone(speakerPin3, Music.F, tempo * Length.one);
 // setTone(speakerPin3, Music.B, tempo * Length.two_half);
 int pauseBetweenNotes = noteDuration * 1.30;
 delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(3);
  
    }
 else {
      digitalWrite (speakerPin3, LOW);
    }   
}

void loop() {
Serial.println("\n");
firstsensor();
secondsensor();
thirdsensor();
delay(100);
if( digitalRead(Grove_Water_Sensor) == HIGH)
   {
      analogWrite(A0,153);
   }else {
      analogWrite(A0,0);
   }
}

I’ve edited your post to add backticks to the code so that it displays correctly.

You should start by reading this:

Pete.