Strange Mouse/Joystick Behavior

Hi, I also recently picked up an Esplora. I tried uploading that code you found online to my Esplora. It's a highly modified version of the Esplora Joystick Mouse code. When I pressed switch 3 I was able to move my joystick/mouse but at a very slow pace. Moving the linear pot will adjust the mouse speed. Other switches including joystick switch would be a mouse click. There wasn't any drift in that new code. But I digress.

I was also looking into why my Esplora would drift to the top right corner of my screen and was able to find a different solution. Here's my code.

/*
 Esplora Joystick Mouse
 
 This  sketch shows you how to read the joystick and use it to control the movement
 of the cursor on your computer.  You're making your Esplora into a mouse!
 
 WARNING: this sketch will take over your mouse movement. If you lose control
 of your mouse do the following:
 1) unplug the Esplora.
 2) open the EsploraBlink sketch
 3) hold the reset button down while plugging your Esplora back in
 4) while holding reset, click "Upload"
 5) when you see the message "Done compiling", release the reset button.
 
 This will stop your Esplora from controlling your mouse while you upload a sketch
 that doesn't take control of the mouse.
 
 Created on 22 Dec 2012
 by Tom Igoe
 
 This example is in the public domain.
 */

#include <Esplora.h>

int xDrift = map(Esplora.readJoystickX(), -512, 512, 10, -10);
int yDrift = map(Esplora.readJoystickY(), -512, 512, -10, 10);

void setup()
{
  Serial.begin(9600);       // initialize serial communication with your computer
  Mouse.begin();            // take control of the mouse
} 

void loop()
{
  int xValue = Esplora.readJoystickX();        // read the joystick's X position
  int yValue = Esplora.readJoystickY();        // read the joystick's Y position
  int button = Esplora.readJoystickSwitch();   // read the joystick pushbutton
  Serial.print("\nJoystick X: ");                // print a label for the X value
  Serial.print(xValue);                        // print the X value
  Serial.print("\tY: ");                       // print a tab character and a label for the Y value
  Serial.print(yValue);                        // print the Y value
  Serial.print("\tButton: ");                  // print a tab character and a label for the button
  Serial.print(button);                        // print the button value
  Serial.print("\txDrift: ");
  Serial.print(xDrift);
  Serial.print("\tyDrift: ");
  Serial.print(yDrift);
  int mouseX = map( xValue,-512, 512, 10, -10);  // map the X value to a range of movement for the mouse X
  int mouseY = map( yValue,-512, 512, -10, 10);  // map the Y value to a range of movement for the mouse Y
  Serial.print("\tmouseX: ");
  Serial.print(mouseX);
  Serial.print("\tmouseY: ");
  Serial.print(mouseY);
  Mouse.move(mouseX-xDrift/10, mouseY-yDrift/10, 0);                 // move the mouse
  
  delay(10);                                  // a short delay before moving again
}

Trying uploading my code and CTRL + SHIFT + M to bring up serial monitor. It would make my explanation a lot easier to digest.

The reason for the drift is that the joysticks is not mechanically calibrated. Ideally the X and Y readings from the joystick should be 0 and 0 when they're not moved. Howver, without even moving the joystick my readings for X is at -8 and Joystick Y is at -9. This is the unmapped reading from the joystick that ranges from -512 to 512. After using the map() function, the x and y readings gets scaled from (-512, 512) to (-10,10). However, the output of the map() function doesn't allow fractions so it gets rounded off to the nearest integer. In my case mouseX = 1 and mouseY = -1 which causes my mouse to slowly move to the top right corner of my screen.

To solve this problem, I introduced xDrift and yDrift to calibrate the mouse. My code would take the X and Y readings from the joystick in their resting position then map (or scale) those readings to a range from (-512, 512) to (-10, 10) only once when the esplora is turned on. During the Mouse.move() function I would subtract the X and Y drift from the X and Y movement so that it would be calibrated.

Although I've gotten this far, there's still something that I can't seem to understand. My xDrift and yDrift value seems to be 10 and -10 after being going through the map() function. Because of this, I divided it by 10 when I'm subtracting it from mouseX and mouseY assuming that its scaled to a factor of 10. However, this is not the real solution to the problem as I could've just subtract 1 and -1 from mouseX and mouseY and still get the same non-drifting result. If anybody could figure this out please let me know. If I can figure it out I'll follow up on this post.