Member Avatar for Matthew_13

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);
}

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 Matthew_13

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.