Turing LED's On With Joystick Movement

Hi, I have some pretty simple code here, but something is weird and I'm not sure how to fix it. Basically I'm trying to have an LED turn on on my Arduino Uno when I move the Joystick's position to x 0 and y 0.
Eventually the joystick will be controlling a motor on my RC Tank. The joystick works fine and the readings are okay, but when I try to get the readings and then turn on an LED if the readings are such and such, it goes haywire. This especially happens if I don't use an if statement but instead use a when. Also, the light turns on even when the readings don't say 0. I attached my code so you can see it. Also, I tried first reading the serial input, but that didn't work, so instead I read the analog inputs, and that resulted in this problem

Thanks for the help!

RC_Tank.ino (741 Bytes)

Vulcan666 wrote (in part):

This especially happens if I don't use an if statement but instead use a when.

What language is this? The Arduino uses C/C++ and assembly language. None of these has a when.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

I'm sorry I mistyped that, I meant while, not when. Thanks for the tips on posting code. I have read the three locked topics at the top but didn't notice how to post code discussed in them, but now I know how. The problem isn't an error message, it is that I am unsure how to write code for my arduino that will make it so when I move my joystick to X-axis 0 and Y-axis 0 that an LED light in pin twelve turns on. Like I said before, the joystick works fine and the X-axis and Y-axis report correctly. However, when I try to make an LED Light turn on when my joystick is at X-axis 0 and Y-axis 0, the results are haywire and messed up, although it does upload without an error message. This involves some wire, my LED and the joystick, however it is correctly wired, and works flawlessly when I don't try to add the code that makes the LED turn on, thus I know it's something wrong with my code.

Here is my code embedded with the code tags like you showed me:--

const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
int code = 0;

void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  pinMode(12, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  Serial.print("Switch:  ");//             Joystick Module Code Startup
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);

  if (analogRead(X_pin == 0) && (analogRead(Y_pin == 1))) {
    digitalWrite(12, HIGH);
  }
}

Hope this response helps you understand how to help me better!
Thanks for helping!

analogRead(X_pin == 0)

You have your brackets in the wrong place. Try...

analogRead(X_pin) == 0

Thank-you so much MorganS! That got it working! I can now go on to make it control motors instead of LED's!

P.S. I knew a person called Morgan, he played ping-pong in North Carolina.

P.P.S Amazing how simply moving a bracket a few spaces to the right can make all the difference in the world!