// Filtered joystick read and report. union { uint32_t accum; struct { uint16_t lower; uint16_t upper; } words; } potval_X, potval_Y;; unsigned long oldmillis; void setup() { Serial.begin(115200); oldmillis = millis(); } #define WEIGHT 64 // Must be power of two. #define FREQ 1000 // Millis between reports when no change. void loop() { uint32_t X_Value = (uint32_t)analogRead(A0) << 16; uint32_t Y_Value = (uint32_t)analogRead(A1) << 16; uint16_t oldval_X = potval_X.words.upper; uint16_t oldval_Y = potval_Y.words.upper; potval_X.accum = ((WEIGHT - 1) * potval_X.accum + X_Value + 32768) / WEIGHT; potval_Y.accum = ((WEIGHT - 1) * potval_Y.accum + Y_Value + 32768) / WEIGHT; unsigned long newmillis = millis(); if (potval_X.words.upper != oldval_X || potval_Y.words.upper != oldval_Y || newmillis - oldmillis >= FREQ) { oldmillis = newmillis; Serial.write(':'); Serial.print(potval_X.words.upper); Serial.write(' '); Serial.print(potval_Y.words.upper); Serial.write(10); } }