make the buzzer beeps for 3 secondes in an if statement

hi , any help please !

i want to active the buzzer one time for 3 seconds in this if loop !

if( distance1 <= 3  )//When the measurement in ultrasonic sensor1 is = or less than 3 cm .
  {
  
  digitalWrite(G_Led,LOW);   // the Green led goes off
  digitalWrite(R_Led,HIGH);  // red led turns on
  myservo.write(0);          // the barrier goes down (SG from 90 to 0 degrees)
 ///////////////
  digitalWrite(Buzzer,HIGH); // active the buzzer
  delay(3000);              // delay of 3000 millisecond = 3s
  digitalWrite(Buzzer,LOW); // deactivat the buzzer
 ///////////////
  }}

Good work, carry on.

what i want to do is how to make The buzzer only active for 3 seconds when the ultrasonic sensor 1 measurement goes below 3cm.

if( distance1 < 3 )//When the measurement in ultrasonic sensor1 is = or less than 3 cm .
  {
  
  digitalWrite(G_Led,LOW);   // the Green led goes off
  digitalWrite(R_Led,HIGH);  // red led turns on
  myservo.write(0);          // the barrier goes down (SG from 90 to 0 degrees)
  digitalWrite(Buzzer,HIGH); // active the buzzer
  delay(3000);              // delay of 3000 millisecond = 3s
  digitalWrite(Buzzer,LOW); // deactivat the buzzer
 
  }

It sounds like the buzzer keeps buzzing as long as the value is under three, maybe something like the following to only buzz when the value has changed to below three instead of always when it is below three.

Still having problems? Post your complete code and describe what you see and hear when you run the sketch.

  distance1 = ???

  if ( (distance1 <= 3) && (oldDistance1 > 3) ) //When the measurement in ultrasonic sensor1 is = or less than 3 cm .
  {

    digitalWrite(G_Led, LOW);  // the Green led goes off
    digitalWrite(R_Led, HIGH); // red led turns on
    myservo.write(0);          // the barrier goes down (SG from 90 to 0 degrees)
    digitalWrite(Buzzer, HIGH); // active the buzzer
    delay(3000);              // delay of 3000 millisecond = 3s
    digitalWrite(Buzzer, LOW); // deactivat the buzzer

  }

  oldDistance1 = distance1;

I'd separate the buzzer from your main code and allow other things to happen while the buzzer is sounding. Delays of that magnitude make baby Jesus cry...

Something like:

.
.
.
    if( distance1 < 3 )
    {
        //When the measurement in ultrasonic sensor1 is = or less than 3 cm .
  
        digitalWrite(G_Led,LOW);   // the Green led goes off
        digitalWrite(R_Led,HIGH);  // red led turns on
        myservo.write(0);          // the barrier goes down (SG from 90 to 0 degrees)

        if( flagBuzzerEnabled )
        {
            flagSoundBuzzer = true;
            flagBuzzerEnabled = false;
        }
 
    }//if

    //always call this in loop()
    //buzzer will sound only when needed and for 3-seconds
    //without blocking loop
    ProcessBuzzer();
    .
    .
    .
    //at some point, you re-enable the 3-second buzzer
    if( some condition met)
        flagBuzzerEnabled = true;

    .
    .
    .
    
}//loop

void ProcessBuzzer( void )
{
    byte
        stateBuzzer = 0;
    unsigned long
        timeNow;
    static unsigned long
        timeBuzzer;
        
    if( flagSoundBuzzer == false )
        return;

    switch( stateBuzzer )
    {
        case    0:
            digitalWrite(Buzzer,HIGH);
            timeBuzzer = millis();
            stateBuzzer = 1;            
        break;
        case    1:
            timeNow = millis();
            if( (timeNow - timeBuzzer) < 3000 )
                return;
            digitalWrite(Buzzer,LOW);
            stateBuzzer = 0;
            flagSoundBuzzer = false;
        break;
        
    }//switch
    
}//ProcessBuzzer