Go Down

Topic: [SOLVED]Arcade trackball to usb mouse help (Read 11132 times) previous topic - next topic

hydrolisk1792

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
}

I make things do what they were never intended to do :P

hydrolisk1792

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
}

I make things do what they were never intended to do :P

Riva

#32
Nov 10, 2017, 09:09 am Last Edit: Nov 10, 2017, 02:01 pm by Riva
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.
Don't PM me for help as I will ignore it.

hydrolisk1792

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! :)
I make things do what they were never intended to do :P

hydrolisk1792

#34
Nov 10, 2017, 10:31 am Last Edit: Nov 10, 2017, 10:49 am by hydrolisk1792
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
I make things do what they were never intended to do :P

Riva

#35
Nov 10, 2017, 02:01 pm Last Edit: Nov 10, 2017, 02:03 pm by Riva
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
}
Don't PM me for help as I will ignore it.

Riva

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.
Don't PM me for help as I will ignore it.

Riva

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
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.
Don't PM me for help as I will ignore it.

hydrolisk1792

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. 
I make things do what they were never intended to do :P

Riva

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.
Don't PM me for help as I will ignore it.

hydrolisk1792

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.
I make things do what they were never intended to do :P

hydrolisk1792

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
}


I make things do what they were never intended to do :P

Go Up