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]