Skip to content

Instantly share code, notes, and snippets.

@unixfool
Created August 9, 2021 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixfool/75e16ff131cadd37e43bdea77655934c to your computer and use it in GitHub Desktop.
Save unixfool/75e16ff131cadd37e43bdea77655934c to your computer and use it in GitHub Desktop.
// Variables for arduino
int valX;
int valY;
bool changedX;
bool changedY;
// Normalized values
int horizontalVal = 0;
int verticalVal = 0;
// Some allowance to reduce noise down
const int threshold = 5;
void setup() {
Serial.begin(115200);
}
void loop() {
valX = analogRead(A0);
valY = analogRead(A2);
changedX = abs( valX - horizontalVal) >= threshold || ( valX == 0 && horizontalVal !=0) || (valX == 1023 && horizontalVal != 1023);
changedY = abs( valY - verticalVal ) >= threshold || (valY == 0 && verticalVal !=0 ) || (valY ==1023 && verticalVal != 1023);
if (changedX || changedY) {
int invertedX = abs(1024 - valX);
int invertedY = abs(1024 - valY);
Serial.print(invertedX);
Serial.print(",");
Serial.println(invertedY);
horizontalVal = valX;
verticalVal = valY;
}
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment