Recently I've been trying to use a wired Xbox 360 controller to interface with my Arduino Uno (via Processing) that I'm using in a test circuit to control two brushed motors. I came across this video that uses the library to control a single servo motor along with videos made by the library's author. I made a few modifications to the code (from the first link) just to support the two motors, and it works (sort of). The only problem is that it accepts input from my Bluetooth mouse instead of the Xbox controller which are both connected to my laptop's USB ports. I set the text configuration file so that "Button 0" and "Button 2" -which correspond to the A and X buttons respectively- make two pins on the Arduino go high which feed into the bases of two transistors that control the motors. Instead, the left mouse button and scroll wheel button on my Bluetooth mouse controls these outputs.
I'm a bit confused about why this is happening and I've tried a few different things to resolve the issue. I thought that when creating the configuration file (with a program that comes with the library) I accidentally choose my mouse as the input device, so I made another configuration file just to make sure that this wasn't the case, though I'm not exactly sure what in the configuration file indicates the correct device to use. Maybe I'm missing something extremely obvious, I just know that I need a second set of eyes to look things over for me. If you have experience with this library your help would be much appreciated, thank you.
/////////////////////////////////////////////////////////////////////////////////////////////////////////
Configuration File:
Tests the xbox controller with motors.
lMotor Left Motor 1 BUTTON Button 0 0 0.0 0.0
rMotor Right Motor 1 BUTTON Button 2 0 0.0 0.0
/////////////////////////////////////////////////////////////////////////////////////////////////////////
Java from Processing:
import processing.serial.*;
import net.java.games.input.*;
import org.gamecontrolplus.*;
import org.gamecontrolplus.gui.*;
import cc.arduino.*;
import org.firmata.*;
ControlDevice cont;
ControlIO control;
Arduino arduino;
//variables for the "A Button" and "X Button" on the xbox controller
ControlButton aButton;
ControlButton xButton;
//needed variables of type int for background function to work (change window color when buttons are pressed)
int aInt;
int xInt;
void setup() {
size(360, 200);
control = ControlIO.getInstance(this);
cont = control.getMatchedDevice("xboxtest");
if(cont == null) {
println("Error, something went wrong");
System.exit(-1);
}
//println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(8, Arduino.OUTPUT);
arduino.pinMode(11, Arduino.OUTPUT);
}
//gets the input and is called in the looping function (void draw)
public void getUserInput() {
//rMotor and lMotor are references to the configuration file
aButton = cont.getButton("lMotor");
xButton = cont.getButton("rMotor");
aInt = 0;
xInt = 0;
if (aButton.pressed() == true) {
aInt = 1;
}
if (xButton.pressed() == true) {
xInt = 1;
}
}
void draw() {
getUserInput();
//changes the color of the interactive window when the input changes
background(100 * aInt, 100, 100 * xInt);
arduino.digitalWrite(8, aInt * 255);
arduino.digitalWrite(11, xInt * 255);
}
UPDATE: I was able to fix the problem after looking at some example code provided by the library that I was previously unaware of. In my defense, there is little support for this library and the video that I watched didn't use the line of code that I found in the example. I hope that this benefits someone in the future.
I ended up changing this:
cont = control.getMatchedDevice("xboxtest");
To this:
cont = control.filter(GCP.GAMEPAD).getMatchedDevice("xboxtest");