Created
August 9, 2021 16:55
-
-
Save unixfool/75e16ff131cadd37e43bdea77655934c to your computer and use it in GitHub Desktop.
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
// 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