Would your situation work if you had to connect the joystick to a host computer instead? I use dual T1600 joysticks to fly my drone.
It uses a python script that implements pygame and pyserial that looks like this
import pygame
import serial
import time
ser = serial.Serial(
port='/dev/cu.usbmodem1421',
baudrate=4000000,
bytesize=8
)
pygame.init()
def remap(value, oldMin, oldMax, newMin, newMax):
oldRange = (oldMax - oldMin)
newRange = (newMax - newMin)
return chr(int(((((value - oldMin) * newRange) / oldRange) + newMin)))
def main():
jstick = []
data = []
aux1 = 0;
aux2 = 0;
oldAux1 = 0;
oldAux2 = 0;
beenPressed1 = 0;
beenPressed2 = 0;
print "helloworld"
for i in range(0, pygame.joystick.get_count()):
jstick.append(pygame.joystick.Joystick(i))
jstick[i].init()
print "Detected Joystick '", jstick[i].get_name(),"'"
while True:
if (ser.in_waiting>0):
ser.reset_input_buffer();
pygame.event.pump()
aux1 = jstick[1].get_button(0);
aux2 = jstick[0].get_button(0);
if(aux1 and not(oldAux1)):
beenPressed1 = not(beenPressed1);
if(aux2 and not(oldAux2)):
beenPressed2 = not(beenPressed2);
data = bytearray([remap(jstick[0].get_axis(1), -1, 1, 255, 0), remap(jstick[0].get_axis(0), -1, 1, 0, 255),remap(jstick[1].get_axis(1), -1, 1, 255, 0),remap(jstick[1].get_axis(0), -1, 1, 0, 255),beenPressed1,beenPressed2])
oldAux1 = aux1;
oldAux2 = aux2;
ser.write(data)
if __name__ == '__main__':
main()
my Arduino loop looks like this
void loop()
{
while (Serial.available()<channels) {} // Wait 'till there are 9 Bytes waiting
for(int n=0; n<channels; n++)
bytes[n] = Serial.read(); // Then: Get them.
Serial.write('A'); //tell python we received data
data.pitch = bytes[0];
data.roll = bytes[1];
data.throttle = bytes[2];
data.yaw = bytes[3];
data.AUX1 = bytes[4];
data.AUX2 = bytes[5];
printValues(data);
radio.write(&data, sizeof(MyData));
}