Devantech SRF04 ultrasonic motor control

Hello all,
I am a newbie here and am trying to have a Devantech SRF04 sensor turn a 12V motor on and off.

I have both the Devantech SRF04 and the 12v motor working independently.

The motor is interfaced with an H-bridge.

Any help greatly appreciated :slight_smile:

code that I am working with is:

//int switchPin = 2; // switch input
#define echoPin 2 // the SRF04โ€™s echo pin
#define initPin 3 // the SRF04โ€™s init pin
unsigned long pulseTime = 0; // variable for reading the pulse
int motor1Pin = 5; // H-bridge leg 1
int motor2Pin = 4; // H-bridge leg 2
int speedPin = 9; // H-bridge enable pin
int ledPin = 13; //LED

void setup() {
// set the switch as an input:
pinMode(echoPin, INPUT);
pinMode(initPin, OUTPUT);
// initialize the serial port:
Serial.begin(9600);

// set all the other pins youโ€™re using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(speedPin, OUTPUT);
pinMode(ledPin, OUTPUT);

// set speedPin high so that motor can turn on:
digitalWrite(speedPin, HIGH);

// blink the LED 3 times. This should happen only once.
// if you see the LED blink three times, it means that the module
// reset itself,. probably because the motor caused a brownout
// or a short.

}

void loop() {

// send the sensor a 10microsecond pulse:
digitalWrite(initPin, HIGH);
delayMicroseconds(10);
digitalWrite(initPin, LOW);

// wait for the pulse to return. The pulse
// goes from low to HIGH to low, so we specify
// that we want a HIGH-going pulse below:

pulseTime = pulseIn(echoPin, HIGH);

// print out that number
Serial.println(pulseTime, DEC);

// if the switch is high, motor will turn on one direction:
if (digitalRead(echoPin) < 2600) {
digitalWrite(motor1Pin,LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
}
// if the switch is low, motor will turn in the other direction:
else {
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
}
}