Conditional statement for when joystick is being pushed?

Hi!

I'm hopping in on a project and need to work with someone else's code, and learn quickly! Please help:)

So I have two joysticks that connect to stepper motors that control an object.
The problem I'm having, is that the object is moving even when the joysticks aren't being touched.

I'm trying to solve this by testing when the joysticks are actually being pushed, and if they are, then go ahead and do steps, etc.

//CONTROLS THE JOYSTICKS, global variables
#define Xpin1 A0      // Pin A0 connected to Joystick 1 x-axis
#define Ypin1 A1      // Pin A1 connected to Joystick 1 y-axis
#define button_1 38     // Pin 38 connected to Joystick Button 1

#define Xpin2 A3      // Pin A2 connected to Joystick 2 x-axis
#define Ypin2 A2      // Pin A3 connected to Joystick 2 y-axis
#define button_2 39     // Pin 39 connected to Joystick Button 2


void setup() {
   Serial.begin(9600);
   pinMode(button_1, INPUT_PULLUP);
   pinMode(button_2, INPUT_PULLUP);

   //This was never implemented but unsure if should be?
   //pinMode(Xpin1, INPUT);
   //pinMode(Xpin2, INPUT);
   //pinMode(Ypin1, INPUT);
   //pinMode(Ypin2, INPUT);
....
...
...
}


void loop() {
....
....
..
  if(switch1) {
    ///THE IF STATEMENT COUNTER==25 used to be the if statement in the code, however that was causing 
        ///the problem, bc the counter increases no matter if the joysticks are touched or not.
    //if (counter == 25)
   //BELOW is my thoughts, except this doesn't work either. 
    if (analogRead(Xpin1) != 512 || analogRead(Xpin2) != 512
                          || analogRead(Ypin1) != 512 || analogRead(Ypin2) != 512) { 
      set_bottom(scale(analogRead(X_pin_1) - 512.0), scale(analogRead(Y_pin_1) - 512.0));
      set_top();
    } 
  }

....
......


}

My logic for using the current if statement:
When the joystick isn't being touched, analogRead() returns numbers around 512, ranging between 480ish - 528ish. I'm wondering what conditional I can use to determine if either of the two joysticks are being pushed?

I used to think that I could use digitalRead() on the joystick Xpins and Ypins, but someone told me that reading would only work for digital joysticks? Either way, when I don't touch the joysticks and do digitalRead on their position pins, I get one y position reading as HIGH and the rest reading as LOW.

Can someone please enlighten me?

I'm wondering what conditional I can use to determine if either of the two joysticks are being pushed?

How about something like

// reading is the result of an analogRead on the joystick

if( reading > 470 && reading < 530) pushed = false;
else pushed = true;

Grumpy_Mike:
How about something like

// reading is the result of an analogRead on the joystick

if( reading > 470 && reading < 530) pushed = false;
else pushed = true;

Hi! Thanks. I could do that, I was wondering if there was a more precise way to do this though? Or if the joysticks could somehow be calibrated or something so that the center is 512?

ssmhoole:
Hi! Thanks. I could do that, I was wondering if there was a more precise way to do this though? Or if the joysticks could somehow be calibrated or something so that the center is 512?

No. Joysticks are not that precise. You may be able to reduce the deadband to something smaller than what @Grumpy_Mike suggested after you test with your own joysticks.

This short Thread about controlling a joystick may be of interest.

...R

I used your numbers

the joystick isn't being touched, analogRead() returns numbers around 512, ranging between 480ish - 528ish. I'm wondering what conditional I can use to determine if either of the two joysticks are being pushed?

was wondering if there was a more precise way to do this though?

No.
Are you accusing me of giving sub optimal solutions?

Or if the joysticks could somehow be calibrated or something so that the center is 512?

You might have a physical adjustment on the joystick but this is rare, even then any A/D conversion is subject to. +/- 1 in a reading no matter how noise free the signals are.