trouble with HC-SR04 and 5V DC motor

So, my son has a project for school and I'm helping him because it's very hard. i said that i would do the coding but, i'm jsut don't really know how this arduine works, i took classes and tried but i can't understand it. the thing that are being used are the HC-SR04 infrared sensor and a normal, small 5V DC motor. When the Infrared sensor sees something within 30cm of it than the Dc motor should go backwards but i can't seem to find a code for this, can anyone help me?

Post your code, please.

Perhaps if you understand that it's an ultrasound ranger, not IR, things might be a little clearer.

this is my code at the moment

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

void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop()
{

long duration, inches, cm;

pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
if (Serial.print > 20cm) {
digitalWrite(pinMode, HIGH);
} else {
digitalWrite(pinMode, LOW);
}
}

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

long microsecondsToCentimeters(long microseconds)
{

return microseconds / 29 / 2;
}

That code doesn't compile, and there is nothing in that code that hints at motor control.

There are many worked examples for this device.

you're saying the code is totally wrong? Where can i find a good code?

No, the compiler already told you the code was wrong.

I'm just confirming it.
Where did you find that code?

(deleted)

Work out how to read the distance (The example code you posted does this)

No, it doesn't - read it.

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;
}