IR sensor activates ultrasonic sensor for distance.

Hi dear friends!
I need you help, but I suppose you will escape from here after reading my question.

I need following:

When an IR sensor (HC-SR501) detect for 3 times (in 20 seconds) a movement, it has to activate an ultrasonic distance sensor (HC-SR04).
This ultrasonic sensor must measure distance: if distance is more than X centimeters, it has to make nothing, but if distance decrease under X centimeters, it has to activate a 5V DC relais/relé until distance grove above X centimeters again.

I know this is not a simple question, and probably it requires a lot of work.

Thank you very much indeed in advance to all people would like to help me.

Sincerely,
Roberto.

What have you written so far ?
Can you print a message when the IR sensor detects an object for instance ?

Thank you, UKHelibob for your answer.
It is not important IR write a message, but probably it is good to have a feedback about its task.

The "3 times detection" is made in order to avoid a "false detection".
After 3 times IR detects a person, I can be sure that probably there is a real person in the area.
Then, I would like to measure the distance of this person from ultrasonic detector.
If this distance is too low (less then X centimeters), than the relais will be activated, otherwise not.

Thank you for your help.

Roberto.

It is not important IR write a message, but probably it is good to have a feedback about its task.

Exactly