Sparkfun Analog Joystick
Sparkfun Analog Joystick
An analog joystick for user input for direction, magnitude and push button. Developer can use it for 360 degree freedom.
Overview
Features:
- Analog Output of X,Y axis voltage
- Additional Push Button
- Simple Setup
Pin Setup
| Embed Pin | Joystick | LCD |
|---|---|---|
| GND | GND | GND |
| VOut | VCC | |
| p18 | VERT | |
| p19 | HORZ | |
| p20 | SEL | |
| VU | +5V | |
| p13 | RX | |
| p14 | TX | |
| p15 | Reset |
Library
Import librarySparkfunAnalogJoystick
Sparkfun Analog Joystick
Information
Important Note:In our library, we set positive x-axis, y-axis in traditional way.
"right" is positive x
"up" is positive y
However the analog output itself gives "left" as high voltage and "up" as high voltage
Information
Important Note:The raw output is a square area of x range [0,1] and y range [0,1]
We normalize the output to a circle are so that x^2+y^2 range [0,1]
Demo Program
Import programLab4
Sparkfun Analog Joystick Test Program
main.cpp
#include "mbed.h"
#include "SparkfunAnalogJoystick.h"
#include "uLCD_4DGL.h"
#define M_PI 3.14159265358979323846
SparkfunAnalogJoystick joysttick(p18, p19, p20);
uLCD_4DGL lcd(p13, p14, p15);
// Test program for the library
// It would output all data about the joystick
// It would also draw a compass to indicate the joystick status on the LCD display
int main() {
lcd.baudrate(3000000);
lcd.background_color(0);
lcd.cls();
float lastx=0;
float lasty=0;
while(1) {
printf("X-Axis: %f\n\r", joysttick.xAxis());
printf("Y-Axis: %f\n\r", joysttick.yAxis());
printf("Angle: %f\n\r", joysttick.angle());
printf("Distance: %f\n\r", joysttick.distance());
printf("Button: %d\n\r\n", joysttick.button());
float distance=joysttick.distance();
float angle=joysttick.angle();
float x=distance*cos(angle*M_PI/180)*40;
float y=distance*sin(angle*M_PI/180)*40;
lcd.circle(60, 60, 40, WHITE);
lcd.line(60, 60, 60+lastx, 60-lasty, BLACK);
lcd.line(60, 60, 60+x, 60-y, WHITE);
lastx=x;
lasty=y;
wait(0.1);
}
}
Demo Video
API List
Import library
Public Member Functions |
|
| SparkfunAnalogJoystick (PinName, PinName, PinName) | |
|
Constructor of Joystick object.
|
|
| int | button () |
|
Get the button status.
|
|
| float | xAxis () |
|
X axis value, reverse the value to get the x value in a normal Cartesian coordinate system.
|
|
| float | yAxis () |
|
Y axis value.
|
|
| float | angle () |
|
Calculate the angle value in a polar coordinate system.
|
|
| float | distance () |
|
Calculate the normalized distance value in a polar coordinate system.
|
|
Please log in to post comments.



