Drive motors the direction in code until ping reads less than 20"?

I am using a sabertooth 2x25 to drive two 24v motors on a tank style robot. I have successfully combined PING and sabertooth ex. Sweep. It compiles but when it runs the motors have a 7us stall from the ping which causes the motion to be very choppy. Is there any way I can simultaneously check the ping while moving the motors in the direction the code tells it to go? Thanks for helping.

[quote]
[color=#7E7E7E]// Sweep Sample[/color]
[color=#7E7E7E]// Copyright (c) 2012 Dimension Engineering LLC[/color]
[color=#7E7E7E]// See license.txt for license details.[/color]
const [color=#CC6600]int[/color] pingPin = 9;

[color=#CC6600]int[/color] val2;

[color=#7E7E7E]// establish variables for duration of the ping, [/color]
[color=#7E7E7E]// and the distance result in inches:[/color]
[color=#CC6600]long[/color] duration, inches;
#include <[color=#CC6600]SabertoothSimplified[/color].h>

[color=#CC6600]SabertoothSimplified[/color] ST; [color=#7E7E7E]// We'll name the Sabertooth object ST.[/color]
[color=#7E7E7E]// For how to configure the Sabertooth, see the DIP Switch Wizard for[/color]
[color=#7E7E7E]//   http://www.dimensionengineering.com/datasheets/SabertoothDIPWizard/start.htm[/color]
[color=#7E7E7E]// Be sure to select Simplified Serial Mode for use with this library.[/color]
[color=#7E7E7E]// This sample uses a baud rate of 9600.[/color]
[color=#7E7E7E]//[/color]
[color=#7E7E7E]// Connections to make:[/color]
[color=#7E7E7E]//   Arduino TX->1  ->  Sabertooth S1[/color]
[color=#7E7E7E]//   Arduino GND    ->  Sabertooth 0V[/color]
[color=#7E7E7E]//   Arduino VIN    ->  Sabertooth 5V (OPTIONAL, if you want the Sabertooth to power the Arduino)[/color]
[color=#7E7E7E]//[/color]
[color=#7E7E7E]// If you want to use a pin other than TX->1, see the SoftwareSerial example.[/color]

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]()
{
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600); [color=#7E7E7E]// This is the baud rate you chose with the DIP switches.[/color]
}

[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]()


{
  [color=#7E7E7E]// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.[/color]
  [color=#7E7E7E]// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:[/color]
  [color=#CC6600]pinMode[/color](pingPin, [color=#006699]OUTPUT[/color]);
  [color=#CC6600]digitalWrite[/color](pingPin, [color=#006699]LOW[/color]);
  [color=#CC6600]delayMicroseconds[/color](2);
  [color=#CC6600]digitalWrite[/color](pingPin, [color=#006699]HIGH[/color]);
  [color=#CC6600]delayMicroseconds[/color](5);
  [color=#CC6600]digitalWrite[/color](pingPin, [color=#006699]LOW[/color]);

  [color=#7E7E7E]// The same pin is used to read the signal from the PING))): a HIGH[/color]
  [color=#7E7E7E]// pulse whose duration is the time (in microseconds) from the sending[/color]
  [color=#7E7E7E]// of the ping to the reception of its echo off of an object.[/color]
  [color=#CC6600]pinMode[/color](pingPin, [color=#006699]INPUT[/color]);
  duration = [color=#CC6600]pulseIn[/color](pingPin, [color=#006699]HIGH[/color]);
  [color=#7E7E7E]// convert the time into a distance[/color]
  inches = microsecondsToInches(duration);
  [color=#CC6600]int[/color] val=inches ;
  [color=#CC6600]if[/color] (val <=20);
  { 
    ST.[color=#CC6600]motor[/color](1, 127); 
    ST.[color=#CC6600]motor[/color](2, 127);
  }
  [color=#CC6600]if[/color] (val>20.1)
  {ST.[color=#CC6600]motor[/color](1,20);
  ST.[color=#CC6600]motor[/color](1,20);}
  
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](inches);
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"in, "[/color]);
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]();

}
[color=#CC6600]long[/color] microsecondsToInches([color=#CC6600]long[/color] microseconds)
{
  [color=#7E7E7E]// According to Parallax's datasheet for the PING))), there are[/color]
  [color=#7E7E7E]// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per[/color]
  [color=#7E7E7E]// second).  This gives the distance travelled by the ping, outbound[/color]
  [color=#7E7E7E]// and return, so we divide by 2 to get the distance of the obstacle.[/color]
  [color=#7E7E7E]// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf[/color]
  [color=#CC6600]return[/color] microseconds / 74 / 2;

}

[/quote]

Did this fix the codes appearance?

// Sweep Sample
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.
const int pingPin = 9;



// establish variables for duration of the ping, 
// and the distance result in inches:
long duration, inches;
#include <SabertoothSimplified.h>

SabertoothSimplified ST; // We'll name the Sabertooth object ST.
// For how to configure the Sabertooth, see the DIP Switch Wizard for
//   http://www.dimensionengineering.com/datasheets/SabertoothDIPWizard/start.htm
// Be sure to select Simplified Serial Mode for use with this library.
// This sample uses a baud rate of 9600.
//
// Connections to make:
//   Arduino TX->1  ->  Sabertooth S1
//   Arduino GND    ->  Sabertooth 0V
//   Arduino VIN    ->  Sabertooth 5V (OPTIONAL, if you want the Sabertooth to power the Arduino)
//
// If you want to use a pin other than TX->1, see the SoftwareSerial example.

void setup()
{
  Serial.begin(9600); // This is the baud rate you chose with the DIP switches.
}

void loop()


{
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(1);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(3);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  int val=inches ;
  if (val <=20);
  { 
    ST.motor(1, 127); 
    ST.motor(2, 127);


  }
  if (val>20.1)
  {
    ST.motor(1,20);
    ST.motor(1,20);
  }

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

}
long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;

}

It looks as if you're using the hardware serial port to communicate with the motor controller.

What do you suppose this will do?

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

Yes I am sending in Serial to motorcontroller. Those lines come from the ping example I assume that it prints the readout of inches to com monitor.

But it sends that string to your motor controller, isn't that going to screw it up?

impellbm:
Yes I am sending in Serial to motorcontroller. Those lines come from the ping example I assume that it prints the readout of inches to com monitor.

In the ping example, the hardware Serial was connected to the PC via the USB cable. You can print whatever rubbish you want and it just comes out on the screen.

In your system the hardware Serial is connected to the motor controller. In this case you must send the controller what it needs, and nothing more. If you send arbitrary text to the controller, it's going to get confused.

You're probably better off either controlling it via a SoftwareSerial port, or (likely better) in RC mode with the Servo Library. I've used the latter method myself with the Sabretooth motor controllers (I have a couple 2x5). No point tying up your only hardware serial port for something that can be controlled via other methods.

Thank you, for the input guys. I am getting ready now to change code over to rc actually its the only thing I havnt tried I just liked the fact that serial only used one pin on arduino. Will using the rc commands get rid of the pause in motion caused by the ping? I figured it was the code doing it. Though there was a weird latin letter popping up from me sending serial commands to move the motors... So I guess I will see. I intend to add a gyro which controls more motors and other components as I learn more.