If statment confusion

My code seems to be wrong but I don't understand how...

int joy_x=A0, joy_y=A1, joy_k=8, joy_center=504;
int bt_a=2,bt_b=3,bt_c=4,bt_d=5,bt_e=6,bt_f=7;


void setup() {
  Serial.begin(115200);
  Serial.println(F("Startup"));

}

void loop() {
  //Serial.println(analogRead(joy_x));
  if (joy_x != 504){
    Serial.println("offset");
    Serial.println(analogRead(joy_x));}
  if (joy_x==504){
    Serial.println("Joy_Center");
  }
  if (joy_x<504){
    Serial.println("Joy Left");}
  delay(1000);
  Serial.println("_________");
}

To clearify my issue, im never getting 'Joy_Center' its always 'offset' and 'Joy Left' with a value of 504

I think if you add 'Serial.print("joy_x = "); Serial.println(joy_x);' to the start of your loop function, you'll see that joy_x is not 504. If you're using an Uno R3, it's going to be 14, which is the value of A0 that you assigned to it.

I'm going to assume that you've got some sort of joystick hooked up to analog inputs A0 and A1? If that's the case, you're going to need to read the values of the inputs in your loop function. Something along the lines of:

void loop() {
  joy_x = analogRead(A0);
  joy_y = analogRead(A1);
  if (joy_x != 504){
    Serial.println("offset");
  ...

I am using an R3. When I change my code like you said i am not only getting 'Joy Center'
The value shows 504 when im not touching it (0-1023 far right) 1024/2=512 mind you. I don't know.

Thank you for your help, that worked.
Working code...

void loop() {
  Serial.print("joy_x=");Serial.println(analogRead(joy_x));
  if (analogRead(joy_x) != 504){
    Serial.println("offset");
    Serial.println(analogRead(joy_x));}
  if (analogRead(joy_x)==504){
    Serial.println("Joy_Center");
  }
  if (analogRead(joy_x)<504){
    Serial.println("Joy Left");}
  delay(500);
  Serial.println("_________");
}

I get what I did wrong now

Let me suggest you clean up the code a little like this, so it makes more sense

int joy_x=A0, joy_y=A1, joy_k=8, joy_center=504;
int bt_a=2,bt_b=3,bt_c=4,bt_d=5,bt_e=6,bt_f=7;


void setup() 
{
  Serial.begin(115200);
  Serial.println(F("Startup"));
}


void loop() 
{
  Serial.print("joy_x=");
  Serial.println(analogRead(joy_x));
  if (analogRead(joy_x) != 504)
    {
      Serial.println("offset");
      Serial.println(analogRead(joy_x));
    }
  if (analogRead(joy_x)==504)
    {
      Serial.println("Joy_Center");
    }
  if (analogRead(joy_x)<504)
    {
      Serial.println("Joy Left");
    }
  delay(500);
  Serial.println("_________");
}

See how that works ?

also what was the purpose of

  Serial.println(F("Startup"));
// Why not just do this
  Serial.println("Startup");

The "F macro" above saves RAM by putting character strings in program memory, and is almost always a very good idea.

1 Like

26 posts were split to a new topic: Use and effectiveness of the F macro

When it's not exactly that (504!) then it's something else.
Your if needs a reasonable range (joy_x >= 502 && joy_x <= 506 ).

For you A0 (or any A input) noise and imprecise position will make it unlikely to be exactly 504. You should make a test program that prints out the A0 input every second or so. Then try to get the value equal to 504. If I'm right simple noise will make the input "wander" around some value near 504.

Adding a 0.1µF capacitor right at A0 to ground will help but still not going to be able to settle at a single value.

Hello joetech89

Welcome to the worldbest Arduino forum ever.

Consider this small example for one joystick:

// make variables
constexpr uint8_t Joy_x {A0};
// make application
void setup()
{
  Serial.begin(115200);
}
void loop()
{
  switch (analogRead(Joy_x))
  {
    case 0 ... 500: Serial.println(F("lower bounds")); break;
    case 501 ... 520: Serial.println(F("middle bounds")); break;
    case 521 ... 1023: Serial.println(F("upper bounds")); break;
  }
  delay (1000); // for testing only 
}

Have a nice day and enjoy coding in C++.

2 Likes

I learn something new every day here.
Thank you for sharing this :+1:

1 Like

I've removed the debate about the F macro, it has no place here.
I have closed the topic as it was answered in reply #5. I have left in the following replies that seem, to me anyway, to add more useful information for the OP.

@joetech89,
Welcome to the forum :woozy_face:
Sometimes people here get a bit over enthusiastic with the help, they mean well but it can get out of hand. I've closed the topic as I think you have your answer, however, if you would prefer to let the discussion continue, or need to ask supplementary questions, then please ask me or one of the other mods to open the topic for you.

Thank you.

3 Likes