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?
#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!