const byte xPin = A0, yPin = A1, buttonPin = 2; int centerX = 512, centerY = 512; const int deadZone = 35; unsigned long reportedAt = 0; int axisValue(int raw, int center) { const int offset = raw - center; if (abs(offset) <= deadZone) return 0; if (offset > 0) { return constrain(map(raw, center + deadZone, 1023, 0, 100), 0, 100); } return constrain(map(raw, 0, center - deadZone, -100, 0), -100, 0); } void setup() { pinMode(buttonPin, INPUT_PULLUP); Serial.begin(9600); Serial.println("Release the stick; center calibration starts in 2 seconds"); delay(2000); long xSum = 0, ySum = 0; for (byte i = 0; i < 64; ++i) { xSum += analogRead(xPin); ySum += analogRead(yPin); delay(5); } centerX = constrain(xSum / 64, deadZone + 1, 1022 - deadZone); centerY = constrain(ySum / 64, deadZone + 1, 1022 - deadZone); } void loop() { const unsigned long now = millis(); if (now - reportedAt < 100) return; reportedAt = now; Serial.print("x="); Serial.print(axisValue(analogRead(xPin), centerX)); Serial.print(" y="); Serial.print(axisValue(analogRead(yPin), centerY)); Serial.print(" pressed="); Serial.println(digitalRead(buttonPin) == LOW ? 1 : 0); }