Hi, I'm now to the whole programming and arduino stuff. I'm trying to make an object avoidance code that can control the sabertooth. I'm having trouble with working out the commands for forward, back, left and right, would anyone have any ideas of what I can do or where to look? So far I'm working off two examples, one was for a robot with object avoidance and the code for the tank sweep for the sabertooth.
This is what i have so far
#include <NewPing.h> //Include NewPing Library
#include <SabertoothSimplified.h>
SabertoothSimplified ST; // We'll name the Sabertooth object ST.
// For how to configure the Sabertooth, see the DIP Switch Wizard for
// Sabertooth 2X25, 2X12 and 2X5 DIP switch configuration wizard
// 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.
#define TRIGGER_PIN 6 //Trigger pin of Ultrasonic sensor connected to pin 6
#define ECHO_PIN 7 //Echo pin of Ultrasonic sensor connected to pin 7
#define MAX_DISTANCE 100 //The maximum distance we want the sensor to look for is 1m
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); //Create Ultrasonic sensor object
unsigned int time; //Variable to store how long it takes for the ultrasonic wave to come back
int distance; //Variable to store the distance calculated from the sensor
int triggerDistance = 30; //The distance we want the robot to look for a new path
int fDistance; //Variable to store the distance in front of the robot
int lDistance; //Variable to store the distance on the left side of the robot
int rDistance; //Variable to store the distance on the right side of the robot
void setup() {
SabertoothTXPinSerial.begin(9600); // This is the baud rate you chose with the DIP switches.
ST.drive(0); // The Sabertooth won't act on mixed mode until
ST.turn(0); // it has received power levels for BOTH throttle and turning, since it
// mixes the two together to get diff-drive power levels for both motors.
// So, we set both to zero initially.
}
void loop() {
int power;
scan(); //Get the distance retrieved
fDistance = distance; //Set that distance to the front distance
if(fDistance < triggerDistance){ //If there is something closer than 30cm in front of us
moveBackward(); //Move Backward for a second
delay(1000);
moveRight(); //Turn Right for half a second
delay(500);
moveStop(); //Stop
scan(); //Take a reading
rDistance = distance; //Store that to the distance on the right side
moveLeft();
delay(1000); //Turn left for a second
moveStop(); //Stop
scan(); //Take a reading
lDistance = distance; //Store that to the distance on the left side
if(lDistance < rDistance){ //If the distance on the left is smaller than that of the right
moveRight(); //Move right for 200 milliseconds
delay(200);
moveForward(); //Then move forward
}
else{
moveForward(); //If the left side is larger than the right side move forward
}
}
else{
moveForward(); //If there is nothing infront of the robot move forward
}
}
void scan(){
time = sonar.ping(); //Send out a ping and store the time it took for it to come back in the time variable
distance = time / US_ROUNDTRIP_CM; //Convert that time into a distance
if(distance == 0){ //If no ping was recieved
distance = 100; //Set the distance to max
}
delay(10);
}
void moveBackward(){
ST.drive(power = -100);
}
void moveForward(){
ST.drive(power = 100)
}
void moveRight(){
(power = 100; power <= 100; power ++)
ST.turn(power);
delay(50);
}
void moveLeft(){
(power = 100; power = -100; power ++)
ST.turn(power);
delay(50);
}
void moveStop(){
// Now stop turning, and stop driving.
ST.turn(0);
ST.drive(0);
}