Uno analog Joystick input serial output to Sabertooth

Hello everyone. I have a couple questions that hopefully someone can help steer me in the right direction on. I am new to Arduino and any type of programming in general but I have been told this “should” work.

I have a tank style steering ride on vehicle for my son with cerebral palsy that is currently RC controlled. Since building it i have wanted my son to have a little more independence and wanted it to have a joystick control but have the rc control as a backup in case he needed a little help along the way.

The vehicle has a Sabertooth 2x32 like i said it is currently RC controlled I spoke with Dimension Engineering and explained that I wanted a joystick input as well as RC (takeover) option. They informed me their software DeScribe can do user mode and they had a sample of code that does exactly this. The code uses Serial input from an Arduino and 3 channels of RC input. I have the 3 channels all ready to go and have uploaded test code for the Sabertooth onto my Uno to verify that the RC takeover works but that is where i am stuck.

I have a cheap analog joystick with x and y axis I am trying to figure out how i can output the analog input to serial output the sabertooth can read. I have used some sample joystick code and have seen a map function but do not know how to make this all work. Does anyone have any suggestions? Should i be basing my arduino code off of the sample Sabertooth code and copying the joystick code into it? I have searched and have been unable to find anyone doing the same thing (with working code). I had started working on the code below by using the tank style sweep sabertooth sketch and adding in the joystick code but i think i may be completely off base with it. When i upload the code and switch my RC controller to the Arduino channel i just get one motor spinning full speed.

// Tank-Style (Diff-Drive) Sweep Sample for USB Sabertooth Packet Serial
// Copyright (c) 2012-2013 Dimension Engineering LLC
// See license.txt for license details.

#include <USBSabertooth.h>

USBSabertoothSerial C; // Use the Arduino TX pin. It connects to S1.
                       // See the SoftwareSerial example in 3.Advanced for how to use other pins.

USBSabertooth ST(C, 128); // The USB Sabertooth is on address 128 (unless you've changed it with DEScribe).
                          // We'll name its object ST.
                          //
                          // If you've set up your Sabertooth on a different address, of course change
                          // that here. For how to configure the Sabertooth, see the DIP Switch Wizard at
                          //   http://www.dimensionengineering.com/datasheets/USBSabertoothDIPWizard/start.htm
                          // Be sure to select Packet Serial Mode for use with this library.
                          //
                          // The USBSabertooth library exposes features that only exist on USB-enabled Sabertooth motor drivers, such as
                          // 12-bit motor outputs, power outputs, control over freewheeling, motor current read-back, and User Mode variables.
                          // If you do not need these features, and want your code to be compatible with all Sabertooth/SyRen motor drivers,
                          // including those that are not USB-enabled, use the Sabertooth library instead.
                          
const int analogInPinX = A0;  // Analog input pin X
const int analogInPinY = A1;  // Analog input pin Y 

int sensorValueX = 0;        // value from pot x
int sensorValueY = 0;       // value from pot y
int outputValueX = 0;        // value output to the PWM (analog out) X
int outputValueY = 0;       // value output to the pwm analog out Y

void setup()
{
  SabertoothTXPinSerial.begin(9600); // 9600 is the default baud rate for Sabertooth Packet Serial.
                                     // You can change this with the DEScribe software, available at
                                     //   http://www.dimensionengineering.com/describe
             
  ST.drive(0); // The Sabertooth won't act on mixed mode packet serial commands 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.
}

void loop()
{
    // read the analog X value:
  sensorValueX = analogRead(analogInPinX);
  // map it to the range of the analog out:
  outputValueX = map(sensorValueX, 0, 1023, -2047, 2047);


  // print the results to the serial monitor:
  Serial.print("sensor = " );
  Serial.print(sensorValueX);
  Serial.print("\t output = ");
  Serial.println(outputValueX);
  Serial.write(outputValueX);
  
    // read the analog Y value:
  sensorValueY = analogRead(analogInPinY);
  // map it to the range of the analog out:
  outputValueY = map(sensorValueY, 0, 1023, -2047, 2047);
  

  // print the results to the serial monitor:
  Serial.print("sensor = " );
  Serial.print(sensorValueY);
  Serial.print("\t output = ");
  Serial.println(outputValueY);
  Serial.write(outputValueY);
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);
  
  int power;
  
  // Don't turn. Ramp from going backwards to going forwards, waiting 20 ms (1/50th of a second) per step of 16.
 power = outputValueX
  ;{
    ST.drive(power);
    
  }
   power = outputValueY
  ;{
    ST.turn(power);
    
  }
}

Here is a pic of my Son in his ride thanks for any help you can offer.

Previous discussions concerning joysticks and differential steering.

http://forum.arduino.cc/index.php/topic,157967.0.html

http://forum.arduino.cc/index.php?topic=172581.msg1281784#msg1281784

Thank You zoomcat i had seen those and went back and re-read through them. I will be honest i have very little idea what i am doing. But with that said i tried my code again tonight after rechecking my connections i had inadvertently connected the ground from the Sabertooth to the +5 on the arduino. After fixing that i wanted to see what my code did and low and behold it somehow worked. I tweaked it a little to slow it down some but here is my code it works for analog joystick input to sabertooth serial out in case anyone else wants to try it in the future. I am sure it could be cleaned up some but it is good enough for me right now.

//Analog Joystick to Sabertooth Serial Output

#include <USBSabertooth.h>

USBSabertoothSerial C; // Use the Arduino TX pin. It connects to S1.
                       // See the SoftwareSerial example in 3.Advanced for how to use other pins.

USBSabertooth ST(C, 128); // The USB Sabertooth is on address 128 (unless you've changed it with DEScribe).
                          // We'll name its object ST.
                          //
                          // If you've set up your Sabertooth on a different address, of course change
                          // that here. For how to configure the Sabertooth, see the DIP Switch Wizard at
                          //   http://www.dimensionengineering.com/datasheets/USBSabertoothDIPWizard/start.htm
                          // Be sure to select Packet Serial Mode for use with this library.
                          //
                          // The USBSabertooth library exposes features that only exist on USB-enabled Sabertooth motor drivers, such as
                          // 12-bit motor outputs, power outputs, control over freewheeling, motor current read-back, and User Mode variables.
                          // If you do not need these features, and want your code to be compatible with all Sabertooth/SyRen motor drivers,
                          // including those that are not USB-enabled, use the Sabertooth library instead.
                          
const int analogInPinX = A0;  // Analog input pin X
const int analogInPinY = A1;  // Analog input pin Y 

int sensorValueX = 0;        // value from pot x
int sensorValueY = 0;       // value from pot y
int outputValueX = 0;        // value output to the PWM (analog out) X
int outputValueY = 0;       // value output to the pwm analog out Y

void setup()
{
  SabertoothTXPinSerial.begin(9600); // 9600 is the default baud rate for Sabertooth Packet Serial.
                                     // You can change this with the DEScribe software, available at
                                     //   http://www.dimensionengineering.com/describe
             
  ST.drive(0); // The Sabertooth won't act on mixed mode packet serial commands 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.
}

void loop()
{
    // read the analog X value:
  sensorValueX = analogRead(analogInPinX);
  // map it to the range of the Serial:
  outputValueX = map(sensorValueX, 0, 1023, -1000, 1000); //-1000, 1000 can be changed to -2047, 2047 for full speed operation 


  // print the results to the serial monitor:
  Serial.print("sensor = " );
  Serial.print(sensorValueX);
  Serial.print("\t output = ");
  Serial.println(outputValueX);
  Serial.write(outputValueX);
  
    // read the analog Y value:
  sensorValueY = analogRead(analogInPinY);
  // map it to the range of the serial:
  outputValueY = map(sensorValueY, 0, 1023, 1000, -1000); //1000, -1000 can be changed to 2047, -2047 for full speed operation
  

  // print the results to the serial monitor:
  Serial.print("sensor = " );
  Serial.print(sensorValueY);
  Serial.print("\t output = ");
  Serial.println(outputValueY);
  Serial.write(outputValueY);
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);
  
  int power;
  
 power = outputValueX
  ;{
    ST.drive(power);
    
  }
   power = outputValueY
  ;{
    ST.turn(power);
    
  }
}

Hey pungohboy, What sabertooth mode were you running on and what are the dip switch settings for the sabertooth? Tried you code and it didn't work for me.

Thanks,