Difficulty working with the joystick

Hey guys, I've been trying to use a joystick with arduino but I've been running into the same problem. The code is attached in this post. This code is from the one that is given on this website and the function I created displays a message on the serial monitor depending on the values.

In the beginning while I was learning how the joystick works I just used this code to display the values on the monitor after the measurements were converted:

Serial.print(X);
Serial.print(',');
Serial.println(Y);

And those values were between 0 and 9. However when using this code:

Serial.println(X, Y)

I would get drastically different values and they were all over the place. One of the Y coordinates didn't even have a value to it and the serial monitor printed .

I learned the itoa() function on stack overflow and used it in my code. I tried all the directional values in the if statement. Some would work but when I changed the direction of the joystick the serial monitor would still keep printing the same line even though the values of the sensor changed. I had even tried writing a separate String function and then mentioning the function in the loop function but I would get exactly the same results. I would appreciate if someone could tell me what in my code is wrong or should be changed.

int joyPin1 = A0;                 // slider variable connecetd to analog pin 0
int joyPin2 = A1;                 // slider variable connecetd to analog pin 1
int value1 = 0;                  // variable to read the value from the analog pin 0
int value2 = 0;                  // variable to read the value from the analog pin 1

void setup() {
  Serial.begin(9600);
  pinMode(joyPin1,INPUT);
  pinMode(joyPin2,INPUT);

 }

 int treatValue(int data) {
  return (data * 9 / 1024);
 }
 void loop() {
  value1 = analogRead(joyPin1);
  delay(500);                  //short pause
  value2 = analogRead(joyPin2);   
  delay(500);                 //short pause
  char comma = ',';           //to separate both values
  int X = treatValue(value2); //converts X axis measurements to a value between 0 and 9
  int Y = treatValue(value1); //converts Y axis measurements to a value between 0 and 9
  itoa(X, comma, Y);          //Converts integer into a String
  if(itoa == 4,',',4){        // When X=4 and Y=4 the joystick is in a standby position
    Serial.println("standby");
  }else{
    Serial.println("error");
  }
  delay(1000);
 }

You can't use pins 0 and 1 and also do things like Serial.print().
Use some other pins.

Edit:

Also, use code tags for such code, and put it in the main body of your post like this:

 /* Read Jostick
  * ------------
  *
  * Reads two analog pins that are supposed to be
  * connected to a jostick made of two potentiometers
  * 
  * FUNCTION 
  * Transforms the sensors measurements to a value between 0 and 9
  * Determines if the joystick is in motion or in standby from the sensor values
  */

 int joyPin1 = 0;                 // slider variable connecetd to analog pin 0
 int joyPin2 = 1;                 // slider variable connecetd to analog pin 1
 int value1 = 0;                  // variable to read the value from the analog pin 0
 int value2 = 0;                  // variable to read the value from the analog pin 1

 void setup() {
  Serial.begin(9600);

 }

 int treatValue(int data) {
  return (data * 9 / 1024);
 }
 void loop() {
  value1 = analogRead(joyPin1);
  delay(500);                  //short pause
  value2 = analogRead(joyPin2);   
  delay(500);                 //short pause
  char comma = ',';           //to separate both values
  int X = treatValue(value2); //converts X axis measurements to a value between 0 and 9
  int Y = treatValue(value1); //converts Y axis measurements to a value between 0 and 9
  itoa(X, comma, Y);          //Converts integer into a String
  if(itoa == 4,',',8){        // When X=4 and Y=4 the joystick is in a standby position
    Serial.println("forward");
  }else{
    Serial.println("error");
  }
  delay(1000);
 }

I can't believe I missed the input pin. Unfortunately though, it still produced the exact same results. I would just get the same direction phrased over and over again as if it's not being read. Also thank you for telling me about the code tags, I had no idea to use them, I'll edit the post and make it easier for people to understand.

DanielMontazeri:
Serial.print(X);
Serial.print(',');
Serial.println(Y);

And those values were between 0 and 9. However when using this code:

Serial.println(X, Y)

[b]Serial.print[/b] allows you to specify the number base being printed. For example, [b]Serial.print (65,HEX) [/b] is equivalent to [b]Serial.print (65, 16)[/b] and will result in "[b]41[/b]" (the hex value of 65).

So, you see that using [b]Serial.print (x,y)[/b] really means [b]Serial.print (x, random_base)[/b]. The "random base" being whatever Y happens to be. Get it?

Thank you very much for telling me that!

I looked back and I made a lot of mistakes in the beginning with the inputs. I didn't have the analog pins set as input and since I'm a beginner I can't really catch mistakes in my code as fast as many of you guys do. I tried inputting random numbers for the arguments in the if and if/else section of my code and the serial monitor would still print standby which means that it's definitely not being read.
*Edit I just took off the joystick and the serial monitor would still print "standby"which means that it's not reading signals from the joystick at all and I tripled checked and my pins are all connected correctly. I switched boards to a mega and I'm getting the same results and unfortunately i don't have another joystick to test with. I'm hoping that the problem is in the code so it can easily be fixed.

DanielMontazeri:
I didn't have the analog pins set as input

You don’t have to before using analogRead, the function call does it.

As you keep changing your code you need to keep posting the changed code so we can see the latest version and check you have done the changes correctly and not introduced new errors.

Also a schematic and photograph of your hardware would help correct any hardware errors you have.

  itoa(X, comma, Y);          //Converts integer into a String

if(itoa == 4,',',8){        // When X=4 and Y=4 the joystick is in a standby position

Look at the reference for itoa(). Can you see what it's doing with Y there? Pretty much the same as Serial.print(X, Y).

Then look at the string where you're storing the result. That isn't even a string. It's a single character. I'm surprised that the compiler even accepts this. This will put the result into a specific but useless location in memory. That location is undoubtedly being used for something else.

Even if you had specified char comma[] = ","; then you've only got a string which can contain a single character plus the invisible null character on the end. You can't put anything longer in there.

Then we get to the second line I quoted. itoa without the brackets is a function pointer. It points to the location of that code in memory. It won't do anything useful to compare that to the number 4. Then the commas make a compound expression that does nothing except stop the compiler giving you error messages on this nonsense code.