Last active
July 19, 2021 12:57
-
-
Save VLTNOgithub/bce7b35f5111465b36f9b494dbb88109 to your computer and use it in GitHub Desktop.
Thumbstick code for arduino
This file contains 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
const int thumbstick_pin = 8; | |
const int X_pin = A0; // Plug thumbstick X direction into pin A0 | |
const int Y_pin = A1; // Plug thumbstick Y direction into pin A1 | |
int xc; | |
int yc; | |
int TSButton; | |
void setup() { | |
for (int i = 0; i < 2; i++) { | |
pinMode(thumbstick_pin, INPUT); | |
} | |
Serial.begin(115200); | |
} | |
void loop() { | |
int x = analogRead(X_pin) - 517; // Read x direction value and -517 to bring back to around 0 | |
int y = analogRead(Y_pin) - 512; // Read y direction value and -512 to bring back to around 0 | |
if (x <-10) { // Thumbstick has off set of +/-8 so this negates that | |
xc = 0; // Turn analogue value into integer. 0, 1 or 2 depending on state | |
} else if (x >10) { | |
xc = 2; | |
} else { | |
xc = 1; | |
} | |
if (y <-10) { | |
yc = 0; | |
} else if (y >10) { | |
yc = 2; | |
} else { | |
yc = 1; | |
} | |
int buttonStates = 0; | |
Serial.print("S"); // Start printing the data, format is Sxc,yc,buttonStates > S1,1,0 | |
Serial.print(xc); | |
Serial.print(","); | |
Serial.print(yc); | |
Serial.print(","); | |
Serial.println((buttonStates)); | |
delay(40); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment