Arduino Coding Issue
So what you're asking is:

If distance is greater than 10:

Turn on


If distance is less than 5:

Turn off



That's not hard to code, since you already have an &quot;if&quot; there that compares the distance.
 if(distance&gt;5){
   digitalWrite(motor,HIGH);
   delay(30000);
   digitalWrite(motor,LOW);
 } else {
   digitalWrite(motor,LOW);
 } 

becomes:
if (distance &gt; 10) {
    digitalWrite(motor, HIGH);
}

if (distance &lt; 5) {
    digitalWrite(motor, LOW);
}

See how there's a direct 1:1 correlation between the English I wrote at the top and the code below it? That's often a good way to understand code: write it out in plain English step by step what you want and what decisions you need to make - then there's usually a simple conversion from English to code.
The hardest part of learning to program is understanding the logic. Do that in English first and the writing of the code itself becomes so much easier.