Riva:
You will need to decide how your going to setup the trackball as a mouse but one way might be to note the time difference in readings between each encoder change to determine how fast it is being moved and then map this to a value between -127 and 127 to pass to the joystick axis.
From the joystick and encoder sketches you should be able to glean the correct calls needed to adjust the a joystick axis and do button presses but I have attached a totally untested example below that might start you in the right direction.
#include <Encoder.h>
#include <Joystick.h>
// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;
Encoder axisX(0, 1);
Encoder axisY(2, 3);
void setup() {
// Initialize 4 Button Pins
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin(false);
}
void loop() {
Joystick.setXAxis(0); // Center joystick X
int newX = axisX.read(); // Read X encoder
if (newX > 0) // Has the value increased since last read
{
Joystick.setXAxis(127); // Set joystick X hard over to right
axisX.write(0); // Zero encoder count for next time
}
if (newX < 0) // Has encoder decreased since last read
{
Joystick.setXAxis(-127); // Set joystick hard left
axisX.write(0); // Zero encoder count
}
Joystick.setYAxis(0); // Center joystick Y
int newY = axisY.read();
if (newY > 0)
{
Joystick.setYAxis(127);
axisY.write(0);
}
if (newY < 0)
{
Joystick.setYAxis(-127);
axisY.write(0);
}
// Read button pin values
for (byte index = 0; index < 4; index++)
{
byte currentButtonState = !digitalRead(index + pinToButtonMap); // Read button state
Joystick.setButton(index, currentButtonState); // Set state in joystick
}
Joystick.sendState(); // Send the joystick data
delay(50); // Wait a bit
}
Okay so I don't know if this matters or not but I have the Pro Micro, but it shows up in the Arduino IDE as a Leonardo... I tried to add more buttons and such to the code and I'm getting some funny things happening and the track ball is hooked up to A0-A3 (I assume from the code) but it isn't doing anything... Furthermore in windows game controller properties it shows 32 buttons, a stick, rudder, throttle, POV Hat, and x/y axis. Is this supposed to happen? Also some of the buttons show as pressed and I have nothing hooked up to the other pins on the Micro... The pins are very strange on my board compared to an UNO or something else. The pin numbers skip around, I assume that all the numbered pins are just the digital pins and the "A" pins are the analog pins? As you will see in the code I assumed that all the digital pins could be used as buttons? What does this part of the code mean:
"
// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;
"
I'll post the modified code:
#include <Encoder.h>
#include <Joystick.h>
// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;
Encoder axisX(0, 1);
Encoder axisY(2, 3);
void setup() {
// Initialize 4 Button Pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin(false);
}
void loop() {
Joystick.setXAxis(0); // Center joystick X
int newX = axisX.read(); // Read X encoder
if (newX > 0) // Has the value increased since last read
{
Joystick.setXAxis(127); // Set joystick X hard over to right
axisX.write(0); // Zero encoder count for next time
}
if (newX < 0) // Has encoder decreased since last read
{
Joystick.setXAxis(-127); // Set joystick hard left
axisX.write(0); // Zero encoder count
}
Joystick.setYAxis(0); // Center joystick Y
int newY = axisY.read();
if (newY > 0)
{
Joystick.setYAxis(127);
axisY.write(0);
}
if (newY < 0)
{
Joystick.setYAxis(-127);
axisY.write(0);
}
// Read button pin values
for (byte index = 0; index < 12; index++)
{
byte currentButtonState = !digitalRead(index + pinToButtonMap); // Read button state
Joystick.setButton(index, currentButtonState); // Set state in joystick
}
Joystick.sendState(); // Send the joystick data
delay(50); // Wait a bit
}
I have uploaded a picture of the Properties Window as well so you can take a look.