Skip to content

Instantly share code, notes, and snippets.

@LucienLee
Last active December 17, 2019 14:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LucienLee/9c0e1ac51ca8f89e2a75 to your computer and use it in GitHub Desktop.
Save LucienLee/9c0e1ac51ca8f89e2a75 to your computer and use it in GitHub Desktop.
Sanwa joystick on Arduino example code
//A0 = green, A1 = yellow, A2 = orange, A3 = red
#define downPin 14
#define upPin 15
#define rightPin 16
#define leftPin 17
void setup() {
Serial.begin(9600);
//Joystick setup
pinMode(downPin, INPUT);
digitalWrite(downPin, HIGH);
pinMode(upPin, INPUT);
digitalWrite(upPin, HIGH);
pinMode(rightPin, INPUT);
digitalWrite(rightPin, HIGH);
pinMode(leftPin, INPUT);
digitalWrite(leftPin, HIGH);
int X = 0;
int Y = 0;
}
void loop() {
joystick( &X, &Y );
result(X,Y);
}
void joystick(int* X, int* Y){
//HIGH = non-trigger
if( !digitalRead( leftPin ) ){
*X = 0;
}else if( !digitalRead( rightPin ) ){
*X = 1;
}else{
*X = -1;
}
if( !digitalRead( upPin ) ){
*Y = 1;
}else if( !digitalRead( downPin ) ){
*Y = 0;
}else{
*Y = -1;
}
}
void result( X, Y ) {
Serial.print(X);
Serial.print('\t');
Serial.print(Y);
Serial.println('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment