So I have a project where I want to use an Xbox 360 controller to control 2 separate DC motors, one with the left joystick and one with the right. All I need is to have it read the Y component and adjust motor power accordingly. I have figured out how to get this to work with transistors, but obviously the motors wont go backwards like that, so I implemented an H-bridge. I only had one so I just tested it with the left joystick and one DC motor. The issue i am having is that as soon as I move the joystick the controller disconnects from the receiver and the serial monitor just displays the first coordinate read. I have no idea why. Code is below.
#include<XBOXRECV.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
USB Usb;
XBOXRECV Xbox(&Usb);
int powerL;
void setup() {
pinMode(10,OUTPUT);
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
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 || Xbox.getAnalogHat(RightHatY) >7500 || Xbox.getAnalogHat(RightHatY) < -7500)
{
if(Xbox.getAnalogHat(LeftHatY) >7500 || Xbox.getAnalogHat(LeftHatY) < -7500 )
{
if(Xbox.getAnalogHat(LeftHatY) >7500)
{
Serial.print(F("LeftHatY: "));
Serial.println(Xbox.getAnalogHat(LeftHatY));
digitalWrite(3,LOW);
digitalWrite(2,HIGH);
powerL = map(Xbox.getAnalogHat(LeftHatY), 7500 , 32767 , 0 , 255);
analogWrite(10,powerL);
}
if (Xbox.getAnalogHat(LeftHatY) < -7500)
{
Serial.print(F("LeftHatY: "));
Serial.println(Xbox.getAnalogHat(LeftHatY));
digitalWrite(3,HIGH);
digitalWrite(2,LOW);
powerL = map(Xbox.getAnalogHat(LeftHatY), -7500 , -32767 , 0 , 255);
analogWrite(10,powerL);
}
}
if(Xbox.getAnalogHat(RightHatY) >7500 || Xbox.getAnalogHat(RightHatY) < -7500)
{
Serial.print(F("RightHatY: "));
Serial.println(Xbox.getAnalogHat(RightHatY));
}
}
}
}
}