Skip to content

Instantly share code, notes, and snippets.

@fmilburn3
Last active August 29, 2015 14:22
Show Gist options
  • Save fmilburn3/ecd049e566dbef14c178 to your computer and use it in GitHub Desktop.
Save fmilburn3/ecd049e566dbef14c178 to your computer and use it in GitHub Desktop.
Energia two axis joystick with pushbutton demonstration
/*
Read Joystick and Push Button - display result to serial monitor
Tested with the Texas Instruments MSP540F5529LP LaunchPad using Energia
and an Addicore joystick with push button:
https://www.addicore.com/Dual-Axis-XY-Joystick-Module-with-Push-Button-p/139.htm
Note: The pin connections shown below will work with most MSP430 LaunchPads.
The maximum values of xValue and yValue will depend on the Analog Digital
Converter (ADC) - e.g. the F5529 is 12 bit (0 to 4095) while the 430G2
is 10 bit (0 to 1023).
Joystick Pin F5529LP Label
-------- --- -------------
GND 20 GND
+5V 1 3V3
VRX 2 P6.5
VRY 6 P6.6
SW 5 P1.6
Released into the public domain
F. Milburn 5/27/2015
*/
int xPin = 2; // x direction potentiometer pin
int yPin = 6; // y direction potentiometer pin
int pushPin = 5; // Push button pin
int xValue = 0; // x direction potentiometer value (0 to 4095)
int yValue = 0; // y direction potentiometer value (0 to 4095)
int pushState = 0; // Push button state (0 or 1)
void setup()
{
Serial.begin(9600);
Serial.println("Starting joystick demonstration...");
pinMode(pushPin, INPUT_PULLUP);
}
void loop()
{
// Read joystick position
xValue = analogRead(xPin); // Read x
yValue = analogRead(yPin); // Read y
Serial.print("X = ");
Serial.print(xValue);
Serial.print(" ");
Serial.print("Y = ");
Serial.print(yValue);
Serial.print(" ");
// Read button status
pushState = digitalRead(pushPin); // See if joystick has been pushed
Serial.print("Joystick is ");
if (pushState == 0)
{
Serial.println("pushed");
}
else
{
Serial.println("not pushed");
}
delay(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment