USB-Joystick with Arduino

Hi All,

I'm having a bit of a trouble, and I hope you guys are able to help me out.

I have bought a Logitech Extreme3DPro USB-Joystick, and connected it to an Arduino Mega via an USB Host Bord LINK .

I’m trying to link a normal 9g micro servo on pin 3 with the inputs from the Joystick. I get some signals from the Host Bord (see attached image), but I guess there is something wrong with the code. I can’t ‘access’ the signals from the Joystick.

Anyone got an idea on what the problem might be?

Code:

/* 
   USB Host Joystick - Serial HEX Data
   Hobbytronics ltd, 2016
   
*/
#include <Wire.h>
#define baudRate 115200    // Serial port baud rate
#include "joystick.h"
#include <Servo.h>
int servoPin = 3;
Servo start;

int startv = 0;
int startvscale;

void setup()  { 
  Serial1.begin(baudRate);

  start.attach(servoPin);
  start.write(90);
  delay(300);
} 

void loop()  { 
   get_joystick();
   if(joystick_ok==1)
   {
      joystick_ok=0;   
      startv = (255-tdata[6]);
      startvscale = map(startv, 0, 255, 0, 180);
          delay(300);
      // Do your processing of Joystick data here
      // See joystick.h for data struct definition.
      // E.g. to access Joystick X-axis data use
      // joystick.joystick_x
   }  
Serial.println(tdata[6]);
delay(50);
}

Included library code (joystick.h):

/*
  USB Host Joystick Controller
  HEX serial interface 
*/
#include <Arduino.h> //needed for Serial
#define JOYSTICK_DATA 15  // 15 chars sent including CR/LF but excluding JOY

unsigned char crc_val;
unsigned char tdata[JOYSTICK_DATA];
unsigned char str_start = 0;
unsigned int  joystick_i;
unsigned char joystick_ok = 0;  // valid data received

// data storage structure to store joystick values
struct {
  unsigned char  joystick_x;
  unsigned char  joystick_y;
  unsigned char  joystick_z;
  unsigned char  joystick_rx;
  unsigned char  joystick_ry;
  unsigned char  joystick_rz; 
  unsigned char  slider; 
  unsigned char  dial; 
  unsigned char  wheel; 
  unsigned char  hat;   
  unsigned char  buttons_1_8; 
  unsigned char  buttons_9_13;  
} joystick;

unsigned char crc8(unsigned char *data, unsigned char len);
void get_joystick(void);

void get_joystick(void){
  // if there's any serial available, read it:
  while (Serial.available() > 0) {
     byte c = Serial.read();  //gets one byte from serial buffer
     // Looking for start of input string CR LF J O Y 
     if(str_start<3){
        switch(c){
           case '

:
             str_start++;
             break;
          case 'J':
             str_start++;
             break;
          case 'O':
             if(str_start==1){
              str_start++;
             }  
            break;
          case 'Y':
             if(str_start==2){
               str_start++;
               joystick_i=0;
             }  
             break;  
          default:
             str_start=0;    
             break;
       }          
    }
    else{
       tdata[joystick_i++] = c;
       if(joystick_i>=JOYSTICK_DATA) {
         // Complete string received
         str_start=0;  
         // Calculate check digit
         crc_val=crc8(tdata,JOYSTICK_DATA-3);
         if(crc_val==tdata[JOYSTICK_DATA-3]){
            // CRC Match - Data Valid
            joystick.joystick_x=tdata[0];
            joystick.joystick_y=tdata[1];
            joystick.joystick_z=tdata[2];
            joystick.joystick_rx=tdata[3];
            joystick.joystick_ry=tdata[4];
            joystick.joystick_rz=tdata[5];
            joystick.slider=tdata[6];
            joystick.dial=tdata[7];
            joystick.wheel=tdata[8];
            joystick.hat=tdata[9];  
            joystick.buttons_1_8=tdata[10];
            joystick.buttons_9_13=tdata[11];
           
            joystick_ok = 1;  // need to improve this      
         }  
         else{
           // Serial.println ('CRC Fail');
         }          
       }
    }      
 }
}

// CRC-8 Check Digit
unsigned char crc8(unsigned char *data, unsigned char len) {
  unsigned char crc = 0x00;
  unsigned char extract, i, sum;
  while(len--){
     extract = *data++;
     for (i = 8; i; i--) {
        sum = (crc ^ extract) & 0x01;
        crc >>= 1;
        if (sum) {
          crc ^= 0x8C;
        }  
        extract >>= 1;
     }
  }  
  return crc;
}


![Arduino USB Plot.PNG|1304x715](upload://wQzGrObcmlUHEoPwj4AlO6RHw8M.png)

![Arduino USB Monitor.PNG|1296x704](upload://crOoPEWvJ2AWecM1uKuxnxy3MWW.png)

![Arduino USB Sche.png|2128x1100](upload://ccDAk5uIL8R8hmIrdYGDYMKcpX6.png)

I got the impression you were confused by serial ports. Post a wiring diagram! If you connected the USB joystick board to Serial1 you must only communicated over that port with it. At the moment you're using Serial as the debugging/monitoring output as well as the connection to the board (in the code).

Please edit your post and change the quote tags to code tags!

Hi pylon,

Thanks for reply. I apologize, I'am a bit new to this.

I have now attached a wiring diagram, made in paint and changed the quote tags. Hope it works.

Thanks,

I have now attached a wiring diagram, made in paint and changed the quote tags. Hope it works.

You connected the joystick board to Serial but you're using the same interface for the communication with the PC. That doesn't work. Connect to joystick board to pins 18/19 (RX1/TX1) and change the object for the communication with it from "Serial" to "Serial1".

Hi pylon,

Thanks for helping out! Much appreciated

I'm sorry, I understand what you're saying.. But i'm having a bit of a trouble "the communication with Serial". Could you tell me what to change, or guide me where I can learn more about it?

Could you tell me what to change, or guide me where I can learn more about it?

In your get_joystick() routine you're currently using the Serial object (Serial.available(), Serial.read(), etc.). Change "Serial" to "Serial1" once you connected the joystick board to RX1/TX1 pins! You already have the Serial1.begin() call in your setup, I guess that was a left over from some other code that did the same.