I agree with jremmington. It is not clear what you want this code to do, it is not clear what it is doing now. And it is not clear why you want what it is doing.
For example the if-statement based on the counter i makes no sense. If i starts as 1000 then for the most part this works as a way to sequentially divide i by 2 until it is less than 4, each time waiting for an ultrasonic pulse. Maybe you had something special in mind for this with those statements jremmington made note of. After the halving of i it waits for 2 seconds at the end of the loop. Once i becomes less than 4, each run through the loop it does it’s ultrasonic ping first, then starts switching the relay output with delays of a few miliseconds. And halving i again each time. Which means it eventually becomes 0 because it is an integere. Then it is followed by the 2 second delay at the end of each loop pass. If this is a mechanical relay then this short milisecond pulse switching of the relay may not be sufficient to move it. Definitely not if i=0. How this is supposed to be dependent on the ultrasonic echo distance is anybodies guess.