Member Avatar for Member #1126982

So I developed an application for an arduino uno that uses an IteadStudio Joystick Shield with 7 buttons and a joystick with an x/y movement tracker. The 7 buttons are connected to pins 3 through 9, and the joystick is connected to analog inputs 0 and 1, with 0 being the Y and 1 being the X. When I test the buttons, it works correctly. However, if I test the Analog inputs with the code below, the Serial monitor goes completely crazy, producing tons of random ascii characters and those letters with the dots above them. It produces these so fast that if I trigger a button, the serial message doesn't appear until about 5 seconds later.

Code:
int XValue;
int YValue;

void setup() {
    pinMode(3, INPUT);
    pinMode(4, INPUT);
    pinMode(5, INPUT);
    pinMode(6, INPUT);
    pinMode(7, INPUT);
    pinMode(8, INPUT);
    pinMode(9, INPUT);
    Serial.begin(9600);
}

void loop() {
    if (digitalRead(3) == LOW) {
        Serial.println("Digital pin 3 is LOW");
    } else if (digitalRead(4) == LOW) {
        Serial.println("Digital pin 4 is LOW");
    } else if (digitalRead(5) == LOW) {
        Serial.println("Digital pin 5 is LOW");
    } else if (digitalRead(6) == LOW) {
        Serial.println("Digital pin 6 is LOW");
    } else if (digitalRead(7) == LOW) {
        Serial.println("Digital pin 7 is LOW");
    } else if (digitalRead(8) == LOW) {
        Serial.println("Digital pin 8 is LOW");
    } else if (digitalRead(9) == LOW) {
        Serial.println("Digital pin 9 is LOW");
    }
    YValue = analogRead(A0);
    XValue = analogRead(A1);
    Serial.println("X=" + XValue);
    Serial.println("Y=" + YValue);
}

Dani AI

Generated

Two quick, practical causes for the “crazy” serial output you saw: (1) a mismatch between the Serial baud rate in your sketch and the Serial Monitor, and (2) a C/C++ pointer/concatenation mistake that prints garbage instead of numbers. The line in the original post that used "X=" + XValue does pointer arithmetic on the string literal (it does not build a text string), so Serial ends up printing whatever memory it finds. Also, printing every loop iteration without throttling will flood the serial buffer and make button messages appear delayed.

Correct printing examples (do not use string-literal + int):

Serial.print("X=");
Serial.println(XValue);
Serial.print("Y=");
Serial.println(YValue);

For formatted output without heap fragmentation, use a small buffer:

char buf[24];
snprintf(buf, sizeof(buf), "X=%d Y=%d", XValue, YValue);
Serial.println(buf);

To avoid flooding the serial monitor and to follow ’s suggestion about smoothing, only send data when it meaningfully changes and optionally average samples. Simple helpers:

int readAvg(int pin, int samples=8) {
  long sum = 0;
  for (int i = 0; i < samples; ++i) sum += analogRead(pin);
  return sum / samples;
}
const int DEAD = 6; // deadband
if (abs(x - lastX) > DEAD || abs(y - lastY) > DEAD) {
  Serial.print("X="); Serial.print(x);
  Serial.print(" Y="); Serial.println(y);
  lastX = x; lastY = y;
}

Extra tips: confirm Serial.begin(...) baud matches the Serial Monitor; analogRead returns 0–1023 (center ~512) so use a deadband around center for joysticks; prefer INPUT_PULLUP for buttons when wired to ground; avoid heavy use of Arduino String on AVR targets to reduce heap fragmentation. These changes explain and fix the garbled output and the delayed button messages seen by .

Recommended Answers

All 2 Replies

This is not uncommon when reading analog data like this. You need to filter out most of the input. There are a number of methods to do this, from simple (which is where you want to start) to complex such as using Kalman filters (non-trivial engineering stuff). So the simple methods are to read some number of inputs, average them out, and use that as the next data point. If some inputs are way out of known range, then throw them away before the average is done. This usually will work for situations like yours. Later, you might want to perform some curve-smoothing to make the imput smooth (duh!) in the visual sense. FWIW, I developed the curve-smoothing algorithms for the first analog touch tablets back in the 1980's, and adapted those for an analog joystick to emulate a mouse for X-Windows in the early 1990's.

The tablet stuff was a contract consulting job. The joystick was for my own use and fun!

Member Avatar for Member #1126982

IT WORKS THANKS A MILLION!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.