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.

this if loop

It's an if statement. If statements don't loop. For and while will loop. But if doesn't loop. That's important to remember.

Also, format your code. When real coders write and the blocks are all lined up all nice and neat, that isn't just to make it pretty. It will really help you later to follow your code. If you press Control-T then the IDE will do it for you so there really is no excuse for things like two braces together on a line.

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;

Abdelali_:
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

}

You mean if the distance is less than 3 but the buzzer has already sounded then you don't want it to sound again.

Go look at the "State Change Example". It is about reacting only once to a pin changing state. But the same concept of remembering the previous state of something in a variable will apply here.

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