So I am trying to control two motors with an xbox controller. the goal is to use the y axis of each xbox joystick to control each motor separately. I am using a sabertooth 2x60 motor driver, and it seems to be giving me problems. I have the switches set as 1,2,5,6 up while 3 and 4 are down. this is for a lithium ion battery, analog individual linear control. I thought S1 would control M1 while S2 would control M2. this does not appear to be the case, as even when I disconnect S2 entirely and just use the left joystick to control what should just be M1, both motors are affected by the joystick movement in the exact same way. the code is below. when I look at the serial monitor it is giving me all the values I want to control the motors, with the positive Y joysticks giving 127.5-255 and the negative Y joysticks giving 0-127.5 (0 at the bottom).
#include<XBOXRECV.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
USB Usb;
XBOXRECV Xbox(&Usb);
int powerL;
int powerR;
void setup() {
pinMode(A0,INPUT);
Serial.begin(9600);
#if !defined(MIPSEL)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
}
void loop() {
Usb.Task();
if (Xbox.XboxReceiverConnected)
{
if(Xbox.Xbox360Connected)
{
if(Xbox.getAnalogHat(LeftHatY) >7500 || Xbox.getAnalogHat(LeftHatY) < -7500 )
{
if(Xbox.getAnalogHat(LeftHatY) >7500)
{
powerL = map(Xbox.getAnalogHat(LeftHatY), 7500 , 32767 , 127.5 , 255);
analogWrite(5,powerL);
Serial.print(F("powerL: "));
Serial.println(powerL);
}
if (Xbox.getAnalogHat(LeftHatY) < -7500)
{
powerL = map(Xbox.getAnalogHat(LeftHatY), -7500 , -32767 , 127.5 , 0);
analogWrite(5,powerL);
Serial.print(F("powerL: "));
Serial.println(powerL);
}
}
if (Xbox.getAnalogHat(LeftHatY) < 7500 && Xbox.getAnalogHat(LeftHatY) > -7500)
{
powerL = 127.5;
analogWrite(5,powerL);
Serial.println(powerL);
}
if(Xbox.getAnalogHat(RightHatY) >7500 || Xbox.getAnalogHat(RightHatY) < -7500)
{
if(Xbox.getAnalogHat(RightHatY) >7500)
{
powerR = map(Xbox.getAnalogHat(RightHatY), 7500 , 32767 , 127.5 , 255);
analogWrite(6,powerR);
Serial.print(F("powerR: "));
Serial.println(powerR);
}
if (Xbox.getAnalogHat(RightHatY) < -7500)
{
powerR = map(Xbox.getAnalogHat(RightHatY), -7500 , -32767 , 127.5 , 0);
analogWrite(6,powerR);
Serial.print(F("powerR: "));
Serial.println(powerR);
}
}
if (Xbox.getAnalogHat(RightHatY) < 7500 && Xbox.getAnalogHat(RightHatY) > -7500)
{
powerR = 127.5;
analogWrite(6,powerR);
Serial.println(powerR);
}
}
}
}