Arduino Forum

Topics => Device Hacking => Topic started by: hydrolisk1792 on Oct 30, 2017, 01:52 am

Title: [SOLVED]Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Oct 30, 2017, 01:52 am
Hello all.

I was wondering if there is a way  to use an arduino leonardo as a way for me to get this golden  tee trackball along with about 10 buttons working as a usb gamepad in my windows environment.  How i see it is if someone here can help me with this then i can do the rest as long as it all works as a usb gamepad in windows.  Like somehow make the trackball show up in the windows joystick window as an analog joystick like you would find on a playstation controller.  In doing so the faster the trackball is moved the more acceleration there is. Any help with this? I have the Leonardo.
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Oct 30, 2017, 09:04 am
This all sounds feasible but you need to supply links/details to the trackball your wanting to use to confirm it can be interfaced with the Leonardo.
For some mad reason the HID library used on the Leonardo does not include joystick descriptions but there are posts outlining the changes needed to the core files to add a joystick that I have used and tested. I cannot remember how many buttons it supported though but can see no reason it should not support at least 8.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Oct 31, 2017, 12:25 am
This all sounds feasible but you need to supply links/details to the trackball your wanting to use to confirm it can be interfaced with the Leonardo.
For some mad reason the HID library used on the Leonardo does not include joystick descriptions but there are posts outlining the changes needed to the core files to add a joystick that I have used and tested. I cannot remember how many buttons it supported though but can see no reason it should not support at least 8.
link to the trackball

https://na.suzohapp.com/products/trackballs/56-5500-11

Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Oct 31, 2017, 09:43 am
I cannot see a schematic on the pinout of the 6 pin connector but (in no particular order) is probably VCC, GND, Y-A, Y-B, X-A & X-B. The trackball output is just like the output from 2x encoders (https://playground.arduino.cc/Main/RotaryEncoders) so as long as the voltage matches your Arduino it should be easy to interface.

There are loads of examples of different methods of reading encoders in the link above so just pick one that gives you a suitable feel or write your own code if you prefer.

Interfacing the push buttons to the Arduino is very simple and just requires an Arduino pin connected to ground through the button like below
(http://blog.codebender.cc/wp-content/uploads/2014/03/push_button_sch_-1024x461.png)

I cannot find the thread on here that outlines the additions you need to apply to the Leonardo core files to give joystick support but maybe someone else can or you just grab a modified copy from someone/somewhere else.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 01, 2017, 01:04 am
I cannot see a schematic on the pinout of the 6 pin connector but (in no particular order) is probably VCC, GND, Y-A, Y-B, X-A & X-B. The trackball output is just like the output from 2x encoders (https://playground.arduino.cc/Main/RotaryEncoders) so as long as the voltage matches your Arduino it should be easy to interface.

There are loads of examples of different methods of reading encoders in the link above so just pick one that gives you a suitable feel or write your own code if you prefer.

Interfacing the push buttons to the Arduino is very simple and just requires an Arduino pin connected to ground through the button like below
(http://blog.codebender.cc/wp-content/uploads/2014/03/push_button_sch_-1024x461.png)

I cannot find the thread on here that outlines the additions you need to apply to the Leonardo core files to give joystick support but maybe someone else can or you just grab a modified copy from someone/somewhere else.

The trackball uses two sets of two opts. I hooked it up to 5vdc and put a meter on one of the pins and found out that it spits out 5vdc (about) and 0 volts. It does this on both wires on both axis. So would I need to also come up with a voltage divider so I don't release the smoke from the arduino lol? Maybe to drop it down to 3.3 volts or so?  I have the Leonardo joystick library on my Leonardo but I have no idea how to modify the code that is there. It shows up in windows as a joystick. It has 32 buttons, two POV hats, rudder, throttle and two analog joysticks. All the code does though is a test when pin A5 (I think) is shorted to ground. I'm not really sure how to modify the code or make even the buttons work lol. I'm really new to this but I know a lot about hardware and electronics.
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 01, 2017, 09:47 am
If your you using a 3.3V Leonardo/ProMicro then try using 3.3V instead of 5v and see if the output still works. If your using a 5V Leonardo/ProMicro then no need of a voltage divider.
Please link to the site where you got the Joystick library from. The site should probably outline how to use the library or supply examples of its use with the library code.
The version I have has helpful info like...
Code: [Select]
// Joystick
//  Usage: Joystick.move(x, y, throttle, buttons)
//  x & y forward/left = 0, centre = 127, back/right = 255
//  throttle max = 0, min = 255
//  8 buttons packed into 1 byte
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 02, 2017, 12:50 am
Link to the page.

http://www.instructables.com/id/Arduino-LeonardoMicro-as-Game-ControllerJoystick/?amp_page=true

I don't understand how to use the instructions or how to incorporate the track ball into that code.
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 02, 2017, 09:04 am
From a quick look at the link I supplied in #3 a suitable library would probably be this (https://www.pjrc.com/teensy/td_libs_Encoder.html) as it allegedly works well with 2x encoders like you have in the trackball.
Get the trackball connected and working with the example sketch supplied printing results to the serial monitor and then try incorporating the Joystick element.

I'm not convinced how well a trackball will work as a joystick but each to there own.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 02, 2017, 05:17 pm
From a quick look at the link I supplied in #3 a suitable library would probably be this (https://www.pjrc.com/teensy/td_libs_Encoder.html) as it allegedly works well with 2x encoders like you have in the trackball.
Get the trackball connected and working with the example sketch supplied printing results to the serial monitor and then try incorporating the Joystick element.

I'm not convinced how well a trackball will work as a joystick but each to there own.
Well if the encoders show up in windows as four buttons then I should be able to use a key mapper program to take those four buttons and turn them into the mouse on the PC.  I will try what you supplied when I get home, in at work right now. Oddly enough I work at a vending company and I'm in charge of the amusement machines as a tech lol. This is where I originally got the track ball from :) I get a lot of strange parts from old machines and such. I actually have another post in the displays section of this forum for a 1x16 vfd star burst display but no one has answered it yet. Would you maybe mind going over there and taking a look at it and see if you could sort it out for me?
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 02, 2017, 07:11 pm
Well if the encoders show up in windows as four buttons then I should be able to use a key mapper program to take those four buttons and turn them into the mouse on the PC.
Does this mean you really want to use the trackball as a mouse and not some sort of joystick as you posted in your OP. Maybe you should explain exactly what your hoping to achieve (mouse, joystick or something else).
Converting the trackball output to a mouse should be a lot easier.

Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 03, 2017, 12:41 am
I need it to show up in windows as a gamepad. From there i can also have the other ten buttons. from there i can set everyrhing up with xpadder for the trackball as the mouse, three buttons for the mouse and the other seven buttons as hotkeys like media controls and such.  this is for an embeded project.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 03, 2017, 05:25 am
Okay I used that link you suggested and seen it in the serial monitor. So it does see the trackball. Now how would i make this code work as a gamepad with the buttons and such? My apologizes for so many questions. I'm new to this arduino programming thing. thank you for all your help so far.
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 03, 2017, 10:35 am
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.

Code: [Select]
#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
}


Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 04, 2017, 02:26 am
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.

Code: [Select]
#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:

Code: [Select]

#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.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 04, 2017, 03:13 am
I also tried the first library I downloaded and tried to modify it with your example and got the same result.  Here is the code from that attempt as well.  The axis don't move when I move the trackball around and some of the buttons are stuck on.

Code: [Select]

// Simple example application that shows how to read four Arduino
// digital pins and map them to the USB Joystick library.
//
// The digital pins 9, 10, 11, and 12 are grounded when they are pressed.
//
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//
// by Matthew Heironimus
// 2015-11-20
//--------------------------------------------------------------------

#include <Encoder.h>
#include <Joystick.h>

Encoder axisX(0, 1);
Encoder axisY(2, 3);

void setup() {
  // Initialize 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(true);
}

// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;

// Last state of the button
int lastButtonState[12] = {0,0,0,0,0,0,0,0,0,0,0,0};

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(255);           // 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(-255);          // 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(255);
    axisY.write(0);
  }
  if (newY < 0)
  {
    Joystick.setYAxis(-255);
    axisY.write(0);
  }

{

  // Read pin values
  for (int index = 0; index < 12; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(50);
}
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 04, 2017, 09:33 am
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... 
Does not matter for this.

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... 
Try getting the code working with the 4 buttons first (on the pins specified) and then start to expand on that if it works. The encoders are connected to digital pins 0-3 not analogue pins A0-A3 as the pins 0-3 are external interrupt pins and give the best results with the encoders (read the blurb on the page I had linked the library to).


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?
Yes. The Joystick library defines the maximum number of axis/buttons usable but you don't need to use them all.

  Also some of the buttons show as pressed and I have nothing hooked up to the other pins on the Micro... 
This is probably because you have altered the code and have the encoders connected to the wrong pins.

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?
Yes the pin numbers do jump around a bit but if you get the pin map from Sparkfun you will see where they all fit. Analogue pins can also be used for digital signals but digital pins cannot be used for analogue.

What does this part of the code mean:
// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;

It is the base pin number from where the buttons are defined.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 04, 2017, 09:25 pm
I'm not understanding what the base pin number is for and why it is needed. I did hook up the trackball to the other pins after reading this and am now getting button presses in the windows properties instead of the joystick moving around in the properties window. :smiley-confuse:
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 05, 2017, 09:34 am
I'm not understanding what the base pin number is for and why it is needed. I did hook up the trackball to the other pins after reading this and am now getting button presses in the windows properties instead of the joystick moving around in the properties window. :smiley-confuse:
What pins did you connect the trackball to?
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 05, 2017, 05:33 pm
Quote
what pins did you connect the trackball to?
A0 - A3
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 05, 2017, 05:54 pm
Assuming you have a genuine (or standard clone) of the Sparkfun Pro Micro then the pinout for it is here (https://learn.sparkfun.com/tutorials/pro-micro--fio-v3-hookup-guide/hardware-overview-pro-micro) and the pins you should be using for the encoders are pins 0-3 NOT A0-A3.
I see from this pinout there is no pins 11-12 on the pro micro so you should also have 4 buttons that connect GND to pins 4-7 and alter the sketch line to
Code: [Select]
const int pinToButtonMap = 4;
and
Code: [Select]
 // Initialize 4 Button Pins
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 06, 2017, 07:15 am
So that means that i hook trackball up to the teal coloured numbered pins on the sparkfun pinout and i can have all the other pins as buttons that activate when shorted to ground? So by hooki g trackball to the pins you specified it will manipulate the joystick portion of the windows properties window and the buttons will manipulate the buttons in the windows properties window?
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 06, 2017, 08:57 am
So that means that i hook trackball up to the teal coloured numbered pins on the sparkfun pinout and i can have all the other pins as buttons that activate when shorted to ground? So by hooki g trackball to the pins you specified it will manipulate the joystick portion of the windows properties window and the buttons will manipulate the buttons in the windows properties window?
If your Teal is what the image legend names as 'Arduino' then you should get movement of the joystick X/Y axis when the trackball is rotated. The code is a simple test and should simulate the joystick being pushed hard over in the trackball direction of rotation. The same with the buttons on the pins 4-7.
This code is only a test that things are connected properly and will need changing to add extra buttons and adjust the trackball operation to suit your needs.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 07, 2017, 12:56 am
If your Teal is what the image legend names as 'Arduino' then you should get movement of the joystick X/Y axis when the trackball is rotated. The code is a simple test and should simulate the joystick being pushed hard over in the trackball direction of rotation. The same with the buttons on the pins 4-7.
This code is only a test that things are connected properly and will need changing to add extra buttons and adjust the trackball operation to suit your needs.

Okay will do. Ill try that as soon as i get home. Thank you.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 07, 2017, 03:52 am
Okay, so I did as you stated and modified the code and got everything to work.  How would I go about making the trackball a lot less sensitive and more like the joystick?  I can already tell by my test that I will need to somehow make a dead zone so the computer will realize that if I'm rolling the ball Up it actually goes up and ignores the little fidgets from side to side for instance.  Also I'm looking for a way to have the acceleration taken into account. Like the faster I roll it the more the joystick registers in that direction.  Like when you push just a bit on an actual analog joystick it goes a little bit in that direction.  It is all working how you stated it would though, so many thanks to you for all your help thus far and I gave you some good karma for putting up with me though all this.  I know I can be a bit annoying with simple to answer questions and all.  I'm new to this but am learning a lot from you and I thank you for that as well.

Furthermore, how would I go about adding one more actual rotary encoder to the sketch as the Z axis.  I tried doing it and all I get is it fidgeting back and forth in the properties window.  I took the X axis and copied it and changed all the values that had "X" and changed them to Z and reduced the numbers to 63 and -63 but that didn't seem to work the way I hoped.  Also if you could get this working, is there a way to have it accelerate the faster I turn the knob is that direction?  I did this on pins 4 & 5.

I'm also having trouble adding buttons above pin ten.  I'm getting the same result as last time where some of the buttons are locked down like they are being pressed even though they aren't.  I'll post my code.

EDIT:  I went ahead and installed some 1K pullup resistors on pins 4 & 5 but that didn't seem to fix the problem.  I was reading on a different forum that sometimes the rotary encoder needs theses resistors to operate properly, however that didn't seem to do the trick :( Also should I be hooking the centre pin of the encoder to GND or 5VDC?  I currently have it hooked to GND.

Code: [Select]


#include <Encoder.h>
#include <Joystick.h>

// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 6;
 
Encoder axisX(0, 1);
Encoder axisY(2, 3);
Encoder axisZ(4, 5);

void setup() {
  // Initialize Buttons

  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP);
  pinMode(16, INPUT_PULLUP);
  pinMode(18, INPUT_PULLUP);
  pinMode(19, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(21, 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);
  }

  Joystick.setZAxis(0);               // Center joystick Z
  int newZ = axisZ.read();
  if (newZ > 0)
  {
    Joystick.setZAxis(63);
    delay(50);
    axisZ.write(0);
  }
  if (newZ < 0)
  {
    Joystick.setZAxis(-63);
    delay(50);
    axisZ.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
}

Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 07, 2017, 09:09 am
A lot of questions...
Adding more encoders is possible but it may impact the performance of the first 2 encoders. From the encoder page I linked it...
Quote
Encoders have 2 signals, which must be connected to 2 pins. There are three options.
    Best Performance: Both signals connect to interrupt pins.
    Good Performance: First signal connects to an interrupt pin, second to a non-interrupt pin.
    Low Performance: Both signals connect to non-interrupt pins, details below.
I would then suggest change to the 'good performance' model and have one encoder pin connected to an interrupt pin (pins 0-3) instead of the current two pins.

The button reading code was only for testing the joystick library worked as expected and is not really suitable for more buttons like this as it cannot cope well non contiguous pin numbers, as you have found.
I suggest putting the button pin numbers into an array and then using that array to configure the pins and read them.

A dead zone might not be so important if you can make the trackball act like an analogue joystick as the off axis values will be small when rolling in a single direction compared to the test code that makes any movement in any axis a full joystick axis move.
A possible way to do the acceleration is to count the number of changes in an axis within a fixed time interval as this will be higher the faster the encoder is rotated, you then map this change value to the amount of joystick movement.
I don't have the hardware to check what value you would get for this so you will need to work on this (with a bit of help).
I would start with loading the original encoder code posted on the library web page that prints the encoder changes to the Serial monitor and then modify it to suit your encoder pins and time interval, and run it.
Start turning the encoder at a fixed speed that sort of matches your minimum joystick movement so you get several results printed in the serial monitor and then do the same at the speed you would expect for full joystick movement. This should give an idea of the min/max values used for acceleration.
Once you have that the code can be altered to use it.
I will try to make time to change the code to suit more buttons and post it here but I'm working (or meant to be :) so might not have time.


Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 08, 2017, 09:50 am
Try this sketch to test the encoder counts at different speeds.
Code: [Select]

#include <Encoder.h>

// Change these pin numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder knobLeft(0, 1);
Encoder knobRight(2, 3);
//   avoid using pins with LEDs attached

void setup() {
  Serial.begin(115200);
  Serial.println("TwoKnobs Encoder Test:");
}

void loop() {
  int newLeft, newRight;
  newLeft = knobLeft.read();
  newRight = knobRight.read();
  if (newLeft != 0 || newRight != 0) {
    Serial.print("Left = ");
    Serial.print(newLeft);
    Serial.print(", Right = ");
    Serial.print(newRight);
    Serial.println();
    knobLeft.write(0);
    knobRight.write(0);
  }
  delay(50);
}
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 08, 2017, 09:52 am
And this sketch is your code modified to allow the reading of all buttons.
Code: [Select]

#include <Encoder.h>
#include <Joystick.h>

Encoder axisX(0, 1);
Encoder axisY(2, 3);
Encoder axisZ(4, 5);

const byte buttonArray[] = {6, 7, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};

void setup() {
  // Initialize Buttons
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    pinMode(buttonArray[x], 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);
  }
 
  Joystick.setZAxis(0);               // Center joystick Z
  int newZ = axisZ.read();
  if (newZ > 0)
  {
    Joystick.setZAxis(63);
    delay(50);
    axisZ.write(0);
  }
  if (newZ < 0)
  {
    Joystick.setZAxis(-63);
    delay(50);
    axisZ.write(0);
  }
 
  // Read button pin values
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    byte currentButtonState = !digitalRead(buttonArray[x]);   // Read button state
    Joystick.setButton(x, currentButtonState);                // Set state in joystick
  }
 
  Joystick.sendState();               // Send the joystick data
  delay(50);                          // Wait a bit
}
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 08, 2017, 09:26 pm
And this sketch is your code modified to allow the reading of all buttons.
Code: [Select]

#include <Encoder.h>
#include <Joystick.h>

Encoder axisX(0, 1);
Encoder axisY(2, 3);
Encoder axisZ(4, 5);

const byte buttonArray[] = {6, 7, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};

void setup() {
  // Initialize Buttons
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    pinMode(buttonArray[x], 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);
  }
 
  Joystick.setZAxis(0);               // Center joystick Z
  int newZ = axisZ.read();
  if (newZ > 0)
  {
    Joystick.setZAxis(63);
    delay(50);
    axisZ.write(0);
  }
  if (newZ < 0)
  {
    Joystick.setZAxis(-63);
    delay(50);
    axisZ.write(0);
  }
 
  // Read button pin values
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    byte currentButtonState = !digitalRead(buttonArray[x]);   // Read button state
    Joystick.setButton(x, currentButtonState);                // Set state in joystick
  }
 
  Joystick.sendState();               // Send the joystick data
  delay(50);                          // Wait a bit
}

So over a length of about ten to fifteen feet away from the arduino cable length, would i need to add some 1k pullup resistors as well as the internal pullups for the buttons and the encoders?  Now because of you i understand the button code fully, thank you.  Okay for the trackball encoders, would i be able to use the same counting technique as the normal encoder knob?  I havent tried the code you posted yet as i just changed benches and some things are shuffeled around lol. I should be able to trt your code and report back to you with the results.
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 09, 2017, 10:30 am
So over a length of about ten to fifteen feet away from the arduino cable length, would i need to add some 1k pullup resistors as well as the internal pullups for the buttons and the encoders?
1K seems a bit strong as a pullup but this will all depend on the type of wire and if it's screened. If your getting spurious readings the maybe start with 10K and work your way down toward 1K. Another option might be to put the Arduino near the encoders and run a longer USB lead back to the computer.

Now because of you i understand the button code fully, thank you.
The original code was just the example supplied with the joystick library but it doies not suit none contiguous pin numbers so using an array seemed the sensible thing to do.

Okay for the trackball encoders, would i be able to use the same counting technique as the normal encoder knob?
Not sure what you mean here. As the test code stands it sends the joystick data every 50 milliseconds (plus overhead) and the encoders are read in a interrupt so may/may not update more than once in that time. How fast they update also depends on there resolution and how fast you turn them and this might be okay at a lower speed.
The test code reads the encoder counts every 50ms then zeros them and prints it out so you should be able to determine how many pulses you get at different encoder speeds and this information can be used to add velocity to the encoder joystick axis.
If you want to add a third encode to the mix then either the other two encoders need connecting differently so only one interrupt is used per encoder instead of two (reducing performance) or the code will need to poll the third encoder quickly to manually count pulses.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 09, 2017, 07:33 pm
Okay I hooked the Trackball to to the serial monitor and that is exactly what I needed the trackball to do.  So I will try to somehow mix that code into the joystick library and see what happens.  The Minimum number is about 0 - 1  Plus Minus. and the max number is roughly 210 - 222 Plus Minus.  I also unhooked the trackball and hooked up one rotary encoder to the serial monitor and I'm thinking that my rotary encoder is damaged or something because I couldn't get it to go above Plus Minus 2 in the serial monitor...  I'm going to try spraying some cleaner into it and see if that will fix it.  I've seen this happen to rotary encoder knobs on the Microwaves we have at my work and I spray them with this special cleaner and they work like new, so I'll take it to work with me tonight and spray some stuff in it and see if that does the trick...  All the buttons work BTW :)  Thank you for that.  I'm going to experiment with the two codes you gave me and see if I can get the track ball to act the way it does in the serial monitor.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 09, 2017, 09:13 pm
So I tried a bunch of things and just kept getting errors when  I went to verify the code...  I only came up with one that didn't throw an error and was actually able to upload but it doesn't work at all.  All it does is fidget around the cross in the properties window in like a one pixel circle.  All the buttons still work though.  :smiley-confuse:   I'll post the code that "Kind of" works.

Code: [Select]


#include <Encoder.h>
#include <Joystick.h>
 
Encoder axisX(0, 1);
Encoder axisY(2, 3);
Encoder axisZ(4, 5);


//Number of Physical Pins that buttons are attached to. Ground out to simulate button press.
const byte buttonArray[] = {6, 7, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};

void setup() { 
  // Initialization of Buttons with internal pullup resistors
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    pinMode(buttonArray[x], 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(newX != 210);           // 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(newX != -210);          // 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(newY != 210);
    axisY.write(0);
  }
  if (newY < 0)
  {
    Joystick.setYAxis(newY != -210);
    axisY.write(0);
  }
 
  Joystick.setZAxis(0);               // Center joystick Z
  int newZ = axisZ.read();
  if (newZ > 0)
  {
    Joystick.setZAxis(newZ != 63);
    delay(50);
    axisZ.write(0);
  }
  if (newZ < 0)
  {
    Joystick.setZAxis(newZ != -63);
    delay(50);
    axisZ.write(0);
  }


  // Read button pin values
  for (byte x = 0; x < sizeof(buttonArray); x++)
  {
    byte currentButtonState = !digitalRead(buttonArray[x]);     // Read button state
    Joystick.setButton(x, currentButtonState);                  // Set state in joystick
  }

  Joystick.sendState();               // Send the joystick data
  delay(50);                          // Wait a bit
}

Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 09, 2017, 11:49 pm
So I did this (see code)  instead and it is working a load better.  However the actual rotary encoder knob I have on the Z axis is still acting up.  I haven't cleaned it yet but when I do I will report back to you and let you know.

Code: [Select]


#include <Encoder.h>
#include <Joystick.h>
 
Encoder axisX(0, 1);
Encoder axisY(2, 3);
Encoder axisZ(4, 5);


//Number of Physical Pins that buttons are attached to. Ground out to simulate button press.
const byte buttonArray[] = {6, 7, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};

void setup() { 
  // Initialization of Buttons with internal pullup resistors
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    pinMode(buttonArray[x], 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(axisX.read());           // 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(axisX.read());          // 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(axisY.read());
    axisY.write(0);
  }
  if (newY < 0)
  {
    Joystick.setYAxis(axisY.read());
    axisY.write(0);
  }
 
  Joystick.setZAxis(0);               // Center joystick Z
  int newZ = axisZ.read();
  if (newZ > 0)
  {
    Joystick.setZAxis(axisZ.read());
    axisZ.write(0);
  }
  if (newZ < 0)
  {
    Joystick.setZAxis(axisZ.read());
    axisZ.write(0);
  }


  // Read button pin values
  for (byte x = 0; x < sizeof(buttonArray); x++)
  {
    byte currentButtonState = !digitalRead(buttonArray[x]);     // Read button state
    Joystick.setButton(x, currentButtonState);                  // Set state in joystick
  }

  Joystick.sendState();               // Send the joystick data
  delay(50);                          // Wait a bit
}

Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 10, 2017, 09:09 am
Try the below code that maps the values (with a little overhead) you supplied from #29 to the range expected by the joystick.

When you say the Z encoder did not work well it may be because the encoder is not as higher resolution as the trackball but it might also be because of what pins you connected it to?

Code: [Select]

Deleted due to being wrong version.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 10, 2017, 09:26 am
Try the below code that maps the values (with a little overhead) you supplied from #29 to the range expected by the joystick.

When you say the Z encoder did not work well it may be because the encoder is not as higher resolution as the trackball but it might also be because of what pins you connected it to?

Code: [Select]

#include <Encoder.h>
#include <Joystick.h>

Encoder axisX(0, 1);
Encoder axisY(2, 3);
Encoder axisZ(4, 5);

const byte buttonArray[] = {6, 7, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};

void setup() {
  // Initialize Buttons
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    pinMode(buttonArray[x], INPUT_PULLUP);
  }
 
  // Initialize Joystick Library
  Joystick.begin(false);
}

void loop() {
  Joystick.setXAxis(0);               // Center joystick X
  int newX = axisX.read();            // Read X encoder
  newX = constrain(newX, -255, 255);  // Constrain the value as mapping can be wrong if range is exceeded
  newX = map(newX, -255, 255, -127, 127); // Map encoder values to the range expected by the joystick
  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();
  newY = constrain(newY, -255, 255);  // Constrain the value as mapping can be wrong if range is exceeded
  newY = map(newY, -255, 255, -127, 127); // Map encoder values to the range expected by the joystick
  if (newY > 0)
  {
    Joystick.setYAxis(127);
    axisY.write(0);
  }
  if (newY < 0)
  {
    Joystick.setYAxis(-127);
    axisY.write(0);
  }
 
  Joystick.setZAxis(0);               // Center joystick Z
  int newZ = axisZ.read();
  if (newZ > 0)
  {
    Joystick.setZAxis(63);
    delay(50);
    axisZ.write(0);
  }
  if (newZ < 0)
  {
    Joystick.setZAxis(-63);
    delay(50);
    axisZ.write(0);
  }
 
  // Read button pin values
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    byte currentButtonState = !digitalRead(buttonArray[x]);   // Read button state
    Joystick.setButton(x, currentButtonState);                // Set state in joystick
  }
 
  Joystick.sendState();               // Send the joystick data
  delay(50);                          // Wait a bit
}

I tried the code above and it still throws the cross in the properties window to the extreme edges with no variables in between.  The code I posted in #31 works the best with the trackball.  It is acting as if an actual analog joystick is attached to the pins with variables included and acceleration.

As for the Z encoder knob I have attached. I cleaned it tonight at work but I am still getting the same result  :(  So my encoder knob has 5 pins on it.  The top two are the button which I have attached to one of the buttons, then the three pins at the bottom are attached to pin 4 and 5 and the middle pin I have going to ground.  I also have a 10K resistor attached as a pullup on pins 4 and 5.  I tried it with and without the resistors but I get the same results.  So in short, the Z encoder is where all my troubles are now.  Any ideas?  I will try to monkey around with the code and all and see if I can sort out something whilst I wait for you.  Thank you again mate!  I have learned so much from you so far! :)
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 10, 2017, 10:31 am
OMFG, I figured it out.  When I went back and read your post wondering what pins I had the encoder hooked to, I figured it out.  I remembered reading about interrupt pins and looked that up on the forums, like which pins on my board have these.  I changed the code around, added a few things and came up with this code.  Everything is working nicely.

Code: [Select]


#include <Encoder.h>
#include <Joystick.h>
 
Encoder axisX(0, 1);
Encoder axisY(4, 7);
Encoder axisZ(2, 3);


//Number of Physical Pins that buttons are attached to. Ground out to simulate button press.
const byte buttonArray[] = {5, 6, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};

void setup() { 
  // Initialization of Buttons with internal pullup resistors
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    pinMode(buttonArray[x], INPUT_PULLUP);
  }
 
  // Initialize Joystick Library
  Joystick.begin(false);
}

void loop()

//Trackball to Joystick bit
{
  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(axisX.read());  // Set joystick to read value
    axisX.write(0);                   // Zero encoder count for next read
  }
  if (newX < 0)                       // Has encoder decreased since last read
  {
    Joystick.setXAxis(axisX.read());  // Set joystick to read value
    axisX.write(0);                   // Zero encoder count
  }
 
  Joystick.setYAxis(0);               // Center joystick Y
  int newY = axisY.read();            // Read Y encoder
  if (newY > 0)                       // Has the value increased since last read
  {
    Joystick.setYAxis(axisY.read());  // Set joystick to read value
    axisY.write(0);                   // Zero encoder count for next read
  }
  if (newY < 0)                       // Has encoder decreased since last read
  {
    Joystick.setYAxis(axisY.read());  // Set joystick to read value
    axisY.write(0);                   // Zero encoder count for next read
  }

  //Volume encoder bit
  Joystick.setZAxis(0);               // Center joystick Z
  int newZ = axisZ.read();            // Read Z encoder
  if (newZ > 0)                       // Has the value increased since last read
  {
    Joystick.setZAxis(63);            // Set encoder to read value
    axisZ.write(0);                   // Zero encoder count for next read
  }
  if (newZ < 0)                       // Has encoder decreased since last read
  {
    Joystick.setZAxis(-63);           // Set encoder to read value
    axisZ.write(0);                   // Zero encoder count for next read
  }


  // Read button pin values
  for (byte x = 0; x < sizeof(buttonArray); x++)
  {
    byte currentButtonState = !digitalRead(buttonArray[x]);     // Read button state
    Joystick.setButton(x, currentButtonState);                  // Set state in joystick
  }

  Joystick.sendState();               // Send the joystick data
  delay(50);                          // Wait a bit
}

 


I know this is off topic, but it is about another topic, could you take a look at this for me?  I'm completely lost on this.

https://forum.arduino.cc/index.php?topic=509500.0 (https://forum.arduino.cc/index.php?topic=509500.0)
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 10, 2017, 02:01 pm
Head slap moment here. I added in the code to read the encoder & map its range but forgot to alter the code to reflect this. Below is what I should have sent you in #32.  :smiley-red:
Code: [Select]

#include <Encoder.h>
#include <Joystick.h>

Encoder axisX(0, 1);
Encoder axisY(2, 3);
Encoder axisZ(4, 5);

const byte buttonArray[] = {6, 7, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};

void setup() {
  // Initialize Buttons
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    pinMode(buttonArray[x], INPUT_PULLUP);
  }
 
  // Initialize Joystick Library
  Joystick.begin(false);
}

void loop() {
  Joystick.setXAxis(0);               // Center joystick X
  int newX = axisX.read();            // Read X encoder
  newX = constrain(newX, -255, 255);  // Constrain the value as mapping can be wrong if range is exceeded
  newX = map(newX, -255, 255, -127, 127); // Map encoder values to the range expected by the joystick
  Joystick.setXAxis(newX);            // Set joystick X
  axisX.write(0);                     // Zero encoder count for next time
 
  Joystick.setYAxis(0);               // Center joystick Y
  int newY = axisY.read();
  newY = constrain(newY, -255, 255);
  newY = map(newY, -255, 255, -127, 127);
  Joystick.setYAxis(newY);
  axisY.write(0);
 
  Joystick.setZAxis(0);               // Center joystick Z
  int newZ = axisZ.read();
  if (newZ > 0)
  {
    Joystick.setZAxis(63);
    delay(50);
    axisZ.write(0);
  }
  if (newZ < 0)
  {
    Joystick.setZAxis(-63);
    delay(50);
    axisZ.write(0);
  }
 
  // Read button pin values
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    byte currentButtonState = !digitalRead(buttonArray[x]);   // Read button state
    Joystick.setButton(x, currentButtonState);                // Set state in joystick
  }
 
  Joystick.sendState();               // Send the joystick data
  delay(50);                          // Wait a bit
}
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 10, 2017, 02:05 pm
OMFG, I figured it out.  When I went back and read your post wondering what pins I had the encoder hooked to, I figured it out.  I remembered reading about interrupt pins and looked that up on the forums, like which pins on my board have these.  I changed the code around, added a few things and came up with this code.  Everything is working nicely.
I would suggest altering this to use the constrain/map code I posted as you can end up with out of range values else.
Glad you got it sorted in the end.
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 10, 2017, 02:21 pm
I know this is off topic, but it is about another topic, could you take a look at this for me?  I'm completely lost on this.

https://forum.arduino.cc/index.php?topic=509500.0 (https://forum.arduino.cc/index.php?topic=509500.0)
You might do better asking an moderator to move the post to a more popular forum like Programming.

It would be difficult to write a library for hardware I don't have plus I'm going to be out of the country for 3 weeks from Wednesday so probably would not have time to do anything.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 10, 2017, 09:16 pm
You might do better asking an moderator to move the post to a more popular forum like Programming.

It would be difficult to write a library for hardware I don't have plus I'm going to be out of the country for 3 weeks from Wednesday so probably would not have time to do anything.
Okay, how do I ask the moderator to move the thread?  I could maybe send you the display and the nano.  Are you in the USA?  I can pay the shipping both ways. 
Title: Re: Arcade trackball to usb mouse help
Post by: Riva on Nov 11, 2017, 08:33 am
Okay, how do I ask the moderator to move the thread?  I could maybe send you the display and the nano.  Are you in the USA?  I can pay the shipping both ways. 
On the bottom of each post is a 'Report to moderator' link. Just click it and ask that the thread be moved to a more suitable forum.
I do not live in the USA.
Title: Re: Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 11, 2017, 08:43 am
On the bottom of each post is a 'Report to moderator' link. Just click it and ask that the thread be moved to a more suitable forum.
I do not live in the USA.
Okay thank you for all your help mate and have fun on your holiday.
Title: Re: [SOLVED]Arcade trackball to usb mouse help
Post by: hydrolisk1792 on Nov 24, 2017, 11:55 pm
Here is the fully working code we all on here came up with if anyone else is interested in doing this as well.

Code: [Select]


#include <Encoder.h>
#include <Joystick.h>
 
Encoder axisX(0, 1);
Encoder axisY(4, 7);
Encoder axisZ(2, 3);


//Number of Physical Pins that buttons are attached to. Ground out to simulate button press.
const byte buttonArray[] = {5, 6, 8, 9, 10, 14, 15, 16, 18, 19, 20, 21};

void setup() { 
  // Initialization of Buttons with internal pullup resistors
  for(byte x = 0; x < sizeof(buttonArray); x++)
  {
    pinMode(buttonArray[x], INPUT_PULLUP);
  }
 
  // Initialize Joystick Library
  Joystick.begin(false);
}

void loop()

//Trackball to Joystick bit
{
  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(axisX.read());  // Set joystick to read value
    axisX.write(0);                   // Zero encoder count for next read
  }
  if (newX < 0)                       // Has encoder decreased since last read
  {
    Joystick.setXAxis(axisX.read());  // Set joystick to read value
    axisX.write(0);                   // Zero encoder count
  }
 
  Joystick.setYAxis(0);               // Center joystick Y
  int newY = axisY.read();            // Read Y encoder
  if (newY > 0)                       // Has the value increased since last read
  {
    Joystick.setYAxis(axisY.read());  // Set joystick to read value
    axisY.write(0);                   // Zero encoder count for next read
  }
  if (newY < 0)                       // Has encoder decreased since last read
  {
    Joystick.setYAxis(axisY.read());  // Set joystick to read value
    axisY.write(0);                   // Zero encoder count for next read
  }

  //Volume encoder bit
  Joystick.setZAxis(0);               // Center joystick Z
  int newZ = axisZ.read();            // Read Z encoder
  if (newZ > 0)                       // Has the value increased since last read
  {
    Joystick.setZAxis(63);            // Set encoder to read value
    axisZ.write(0);                   // Zero encoder count for next read
  }
  if (newZ < 0)                       // Has encoder decreased since last read
  {
    Joystick.setZAxis(-63);           // Set encoder to read value
    axisZ.write(0);                   // Zero encoder count for next read
  }


  // Read button pin values
  for (byte x = 0; x < sizeof(buttonArray); x++)
  {
    byte currentButtonState = !digitalRead(buttonArray[x]);     // Read button state
    Joystick.setButton(x, currentButtonState);                  // Set state in joystick
  }

  Joystick.sendState();               // Send the joystick data
  delay(50);                          // Wait a bit
}