Ultrasonic range finder and DC Motor problem

I tore apart a cheap R/C car and plugged the DC motors to my arduino via a dual h-bridge. This runs just fine. I added the Parallax Ping Ultrasonic range finder and got it to work fine by itself. However, when I combine the two it doesn't work. The first run of loop() happens as expected. The range finder comes on, gets a ping, and a decision is made about which direction to turn the motor. The motor spins and at the end it prints the range finder value to serial. But on the second run through loop() the range finder is not turning on and get stuck in the LOW while loop since it isn't sending data. So, the question is why do the motors keep the range finder from coming back on?

#define motorPin0 10
#define motorPin1 11
#define motorPin2 2
#define motorPin3 3
#define ultraSoundSignal 13 
#define MOTOR_SPEED 150

int val = 0;
int timecount = 0; // Echo counter

long randomChoice;

void forward(){
analogWrite(motorPin1, MOTOR_SPEED);
analogWrite(motorPin0, 0);
}
void reverse(){
analogWrite(motorPin0, MOTOR_SPEED);
analogWrite(motorPin1, 0);
}
void left(){
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin2, LOW);
}
void right(){
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
}
void straight(){
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
}
void stop(){
analogWrite(motorPin1, 0);
analogWrite(motorPin0, 0);
}

void setup() {
  Serial.begin(9600); 
  pinMode(motorPin0, OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  randomSeed(analogRead(0));
}

void loop() {
  Serial.println("Begin loop");
 timecount = 0;
 val = 0;
 pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor
 * -------------------------------------------------------------------
 */

  digitalWrite(ultraSoundSignal, LOW); // Send low pulse
  delayMicroseconds(2); // Wait for 2 microseconds
  digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
  delayMicroseconds(5); // Wait for 5 microseconds
  digitalWrite(ultraSoundSignal, LOW); // Holdoff

/* Listening for echo pulse
 * -------------------------------------------------------------------
 */

  pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input

  val = digitalRead(ultraSoundSignal); // Append signal value to val
  while(val == LOW) { // Loop until pin reads a high value
    val = digitalRead(ultraSoundSignal);
  }

  while(val == HIGH) { // Loop until pin reads a high value
     val = digitalRead(ultraSoundSignal);
    timecount = timecount +1;            // Count echo pulse time
  }


  randomChoice = random(1,101);

  if (randomChoice <=20) right();      //Random direction for roaming
  else if (randomChoice <=40) left();
  else straight();
  
  if (timecount > 150){          // if there is nothing directly in front then go forward, otherwise back up.
    forward();
  }
  else {
    reverse();
  }
  delay(100);
  Serial.println(timecount);
  
}

hey there,

if you're sure that you're code is right, it might be a problem with noise caused by the dc motors. you could try adding one 1.µF and one 0.1µF Capacitor to your circuit. I'm not too sure with the values but that's what i was told and which worked for me.

best, kuk

thanks for the input. i hadn't though about motor noise. turns out my motors already have capacitors on them, but they may not be big enough. i have a temporary work around of stopping the motors and delaying a bit at the end. the movement is a little jerky, but not that noticeable. once i make it down to radio shack for some capacitors i'll give it a shot. again, thanks!

Just replace your motor with and LED and resistor. Then if the code doesn't misbehave you know you have a noise problem. If you still have the problem then it is a software bug.