DC motor with ultrasonic sensor

So I'm trying to make a device that when the ultrasonic sensor senses something less than 20 cm away, the motor activates. Here is my code:

const int trigPin = 9; const int echoPin = 10;

const int motor = 7;

long duration; int distance;

void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(motor, OUTPUT); }

void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2);

digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = duration*0.034/2;

Serial.println(distance);

digitalWrite(motor, HIGH); }

I'm now trying to wire the whole thign to my Arduino Uno, but I can't get it to work. Does anyone know a way I could wire this ONLY using wires and a breadboard? BTW the motor requires a tiny amount of voltage to run, so it runs when plugged into the Arduino.

Does the distance measurement work?

when the ultrasonic sensor senses something less than 20 cm away, the motor activates.

You need an [u]if-statement[/u].

BTW the motor requires a tiny amount of voltage to run, so it runs when plugged into the Arduino.

What's the current rating on the motor? The Arduino I/O pins are rated for 40mA (0.04A) "absolute maximum". Any more than that and you are over-stressing the Arduino. And, make sure to add a reverse diode across the motor to kill the high voltage [u]back-EMF[/u] when the motor shuts off.

if(distance < 20)
{
digitalWrite(motor, HIGH);
}
else
{
digitalWrite(motor, LOW);
}
}

I’m not sure what rating it is but the motor runs fine off a AAA battery. Also, I’m trying not to use anything besides wires, the arduino, the motor, and the sensor because I don’t want to spend more money on it, as this is just a prototype, so is there any way to wire it just with wires?