Examples > Esplora
This sketch shows you how to read information from the Esplora's joystick and use it to control the movement of the cursor on your computer. You're making your Esplora into a mouse!
This sketch will take over the mouse movement of your computer. If you lose control of your cursor do the following :
This will stop your Esplora from controlling your cursor while you upload a sketch that doesn't take control of the mouse.
Only your Arduino Esplora is needed for this example. Connect the Esplora to your computer with a USB cable and open the Arduino's Serial Monitor.
Joystick on the Esplora
To send data to your computer, you need to open a serial connection. Use Serial.begin() to open a serial port at 9600 baud on the Esplora.
To start communication as a mouse, call Mouse.begin(). This makes the Esplora appear as a mouse to your computer.
To read the position of the joystick, call Esplora.readJoystickX() and Esplora.readJoystickY(), saving the values in variables. This gives you values between -512 and 512 for each axis. When the joystick is centered, the X and Y axes will report 0.
The joystick also acts as a switch when pressed. To read the button, call Esplora.readJoystickSwitch(). This will give you a value of 1 when pressed, and 0 when it is not.
To send the values to the Serial Monitor, you call Serial.print(). When the Esplora is connected, and the Serial Monitor is open, you should start to see values reported like this :
Joystick X: 0 Joystick Y: 0 Button: 0
To get numbers appropriate for moving the mouse, use the map() function to scale the joystick values, saving these numbers into new variables.
To actually move the cursor, call Mouse.move(). It takes three arguments, the numbers represent the amount of movement on the x-axis, the y-axis, and the mouse wheel. This example only moves the mouse around on the x and y axes, use the new mapped numbers to move the cursor.
When you attach the Esplora, press Shift-Command-M in the Arduino software to open the serial monitor. As you move the joystick, you'll see the values in the serial monitor as the cursor moves around the screen.