Joystick Example

Joystick Example

This example shows how to use the Joystick integrated on Arduino Esplora. 
In particular it is explained how to read the button status and the axes position of the joystick.

Hardware

  • Arduino Esplora

Circuit

Joystick of Arduino Esplora 
In this sketch you need only your Arduino Esplora connected in your PC with USB cable.

Code

In this example it is included the Esplora.h library to use the joystick. 
In the setup function it is initialized the serial communication with the Serial.begin(9600) command. 
In the loop block is read the axes position of joystick through the functions: "Esplora.readJoystickX()"and"Esplora.readJoystickY()". 
The values for each axis are between -512 and 512. When the joystick is centered, the X and Y axes report the zero value. 
Also it is used "Esplora.readJoystickButton()" to read the status of switch joystick, this value is 0 when pressed, and 1 otherwise. 
Finally the results are shown on serial monitor through the Serial.print() command. 
NOTE: 
You need the "Esplora.h" library to use this sketch.

 
 
/*Joystick example*/
#include <Esplora.h>
 
void setup() {
Serial.begin(9600);// initialize the serial communication
}
 
void loop() {
// read the sensors into variables:
int xAxis = Esplora.readJoystickX();
int yAxis = Esplora.readJoystickY();
int button = Esplora.readJoystickButton();
 
// print the levels:
Serial.print("Xasis:");
Serial.print(xAxis);
Serial.print('\t');
Serial.print("Yasis:");
Serial.print(yAxis);
Serial.print('\t');
Serial.print("Button:");
Serial.println(button);
delay(10);
}
}