trouble with HC-SR04 and 5V DC motor

Back at a computer now.

Try this - I haven't compiled it.

const byte trigPin = 2;
const byte echoPin = 4;
const byte motorPin = 3;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode (trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  pinMode (echoPin, INPUT);
  pinMode (motorPin, OUTPUT);
}

void loop()
{
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
 
  unsigned long duration = pulseIn(echoPin, HIGH);
  unsigned long  inches = microsecondsToInches(duration);
  unsigned cm = microsecondsToCentimeters(duration);
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.println("cm");
 
  delay(100);
  if (cm > 20) {
     digitalWrite(motorPin, HIGH);
  } else {
    digitalWrite(motorPin, LOW);
  }
}

unsigned long microsecondsToInches(unsigned long microseconds)
{
  return microseconds / 74 / 2;
}

unsigned long microsecondsToCentimeters(unsigned long microseconds)
{
   return microseconds / 29 / 2;
}