For some reason by default my joystick is at x-8 y-1 while in center and when I run this example my mouse is constantly moving up and to the right. Is there a way to calibrate the joystick to zero?
I've worked around it by modifying the code:
int xValue = Esplora.readJoystickX()+8; // read the joystick's X position
int yValue = Esplora.readJoystickY()+1; // read the joystick's Y position
I've now added functionality for this and fixed this problem:
#include <Esplora.h>
int initx,inity; //declares global variables for inital x/y values
void setup()
{
Serial.begin(9600); // initialize serial communication with your computer
Mouse.begin(); // take control of the mouse
initx = Esplora.readJoystickX(); //read initial value for x
inity = Esplora.readJoystickY(); //read initial value for y
}
void loop()
{
int xValue = Esplora.readJoystickX() - initx; // read the joystick's X position - initial value
int yValue = Esplora.readJoystickY() - inity; // read the joystick's Y position - initial value
int button = Esplora.readJoystickSwitch(); // read the joystick pushbutton
int sw1 = Esplora.readButton(SWITCH_DOWN); // read switch 1 state
int sw4 = Esplora.readButton(SWITCH_RIGHT); // read switch 4 state
Serial.print("Joystick 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("\tStick Button: "); // print a tab character and a label for the button
Serial.print(button); // print the button value
Serial.println(); // create a new line
Serial.print("Switch 1: "); // print a label for switch 1 value
Serial.print(sw1); // print the switch 1 value
Serial.print("\tSwitch 4: "); // print a tab character and a label for switch 4 value
Serial.print(sw4); // print the switch 4 value
Serial.println(); // create a new line
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
if((Esplora.readButton(SWITCH_DOWN) == LOW)
|| (Esplora.readJoystickButton() == LOW)){ // checks to see if switch 1 or joystick button is pressed
Mouse.press(MOUSE_LEFT); // holds down left mouse button
}
else{
Mouse.release(MOUSE_LEFT); // releases left mouse button when not pressed
}
if(Esplora.readButton(SWITCH_RIGHT) == LOW){ // checks to see if switch 4 is pressed
Mouse.press(MOUSE_RIGHT); // holds down right mouse button
}
else{
Mouse.release(MOUSE_RIGHT); // releases right mouse button when not pressed
}
Mouse.move(mouseX, mouseY, 0); // move the mouse
delay(10); // a short delay before moving again
}