1

I designed a custom ATMega32U4 based Arduino Leonardo board. I'm using MHeironimus/ArduinoJoystickLibrary to implement an HID joystick. I need to create also a virtual serial port to send NON joystick data to the USB host (Android device). If I try to set use the usb serial the joystick device is not recognized. If I stop the serial the joystick works fine. This's the code:

#include <Arduino.h>
#include <Joystick.h>

#define JOYSTICK_PERIOD 20

const int BUTTON1_PIN = 4; const int BUTTON2_PIN = 5;

int x = 0, y = 0, rx = 0, ry = 0, din = 0; long time1, time2;

Joystick_ Joystick( JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 2, 0, true, true, false, true, true, false, false, false, false, false, false );

void setup() { pinMode(4, INPUT_PULLUP); pinMode(5, INPUT_PULLUP);

pinMode(A0, INPUT); pinMode(A1, INPUT); pinMode(A2, INPUT); pinMode(A3, INPUT);

Joystick.begin(); Joystick.setXAxisRange(0, 1000); Joystick.setYAxisRange(0, 1000); Joystick.setRxAxisRange(0, 1000); Joystick.setRyAxisRange(0, 1000);

time1 = millis();

Serial1.begin(9600); Serial.begin(9600); }

void SerialEvent() { int _count = Serial.available();

for(int _i=0;_i<_count;_i++) Serial1.write(Serial.read()); }

void Serial1Event() { int _count = Serial1.available();

for(int _i=0;_i<_count;_i++) Serial.write(Serial1.read()); }

void loop() { time2 = millis();

long _period = time2 - time1;

time2 = time1;

if(_period >= JOYSTICK_PERIOD) { din = (digitalRead(BUTTON1_PIN) << 1) | digitalRead(BUTTON2_PIN);

x = analogRead(A0);
y = analogRead(A1);
rx = analogRead(A2);
ry = analogRead(A3);

Joystick.setButton(0, din);

Joystick.setXAxis(x);
Joystick.setYAxis(y);
Joystick.setRxAxis(rx);
Joystick.setRyAxis(ry);

} }

Anyone has hints?

weirdgyn
  • 111
  • 5

0 Answers0