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;
}


