Last active
December 17, 2019 14:26
-
-
Save LucienLee/9c0e1ac51ca8f89e2a75 to your computer and use it in GitHub Desktop.
Sanwa joystick on Arduino example code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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