Need Help! Ultrasonic Sensor HC-SR04

I got this HC-SR04 to use on my project, I want the HC-SR04 to trigger my 5v 2CH relay, but I can't find a code on the internet to be my guide. I found some but they doesn't work, or doesn't do what I want. If you can help me with the coding i would be grateful. What I want is when the ultrasonic sensor has sensed something, the relay will active for 10 secs. Thanks in advance!

Lots of code on how to read distances with the HC-SR04.

Finding code on how to keep a relay on for some time should also be quite easy to find.

After that, just combine the two.

For detailed suggestions - post what you got.

I currently have this working code, but it only powers up the LED, now, I only need the code for the relay, help. :

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 7;

// defines variables
long duration;
int distance;
int safetyDistance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}

void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delay(1000);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(500);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 10){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}