Thursday, May 21, 2015

Addicore Joystick with Push Button

Here is a nice little joystick from Addicore with push button action.  It's easy to use on the MSP430F5529LP.  It uses dual potentiometers for the x and y axis.  Note that there is a commented out line that you can use if desired to change the numeric value for the y axis so that zero voltage is down instead of up.  The x axis goes from zero at left to 4095 at right.  The F5529 has analog resolution of 4096 - you will need to change this to 1023 for Arduino or lower resolution MSP430s.



The Energia sketch below demonstrates how it works.

 /*
 Read Joystick and Push Button - display to Serial Monitor
 Tested with MSP540F5529LP
 Addicore joystick with push button
 https://www.addicore.com/Dual-Axis-XY-Joystick-Module-with-Push-Button-p/139.htm

 F. Milburn 5/19/2015

 Joystick  MSP430F5529LP
 --------  -------------
 GND       GND
 +5V       3V3
 VRX       P6.0
 VRY       P6.1
 SW        P3.4

 */

 int xPin = P6_0;           // x direction potentiometer pin
 int yPin = P6_1;           // y direction potentiometer pin
 int pushPin = P3_4;        // 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...");
   pinMode(pushPin, INPUT_PULLUP);
 }

 void loop()
 {
   // Read joystick position
   xValue = analogRead(xPin);         // Read x     (x0 left)
   yValue = analogRead(yPin);         // Read y     (y0 top)
   // yValue = abs(4095 - yValue);    // Reverse y direction  
  
   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 in
  
   Serial.print("Joystick is ");
   if (pushState == 0)
   {
     Serial.println("pushed in");
   }
   else
   {
     Serial.println("not pushed");
   }
  
   delay(200); 
 }

No comments:

Post a Comment