analogRead() !- digitalRead()

Hello,

I'm using a "JoyStick Breakout Module Shield For PS2 Game Controller For Arduino" that you can easilly find on ebay.

I always get a HIGH value when I'm trying to read the button state...

Here a simplified code to understand:

const int joy_z = 6;
void setup()
{
  pinMode(joy_z, INPUT);
  //pinMode(joy_z, INPUT_PULLUP); also tried this option but it doesn't change anything
}

loop {
  value = analogRead(joy_z); //Return a value > 0 when button unpressed and value = 0 when button pressed

  value = digitalRead(joy_z); //Always return HIGH

  delay(700);
}

I was expecting to use digitalRead method to read the button pressed state.

What is wrong ?

Thanks a lot for help,

Kind regards,

So analogRead() does work? You don't make that clear.

The code is missing the declaration for joy_z.

How is the joystick wired? Do you have a datasheet or diagram you can give us?

Hello,

analogRead works correctly.

It was written in the code:
//Return a value > 0 when button unpressed and value = 0 when button pressed

Here an exemple of wirring:

Still not understand why it not work...

Thanks for help

Hi,
Can you post the working sketch not the one you posted. That one will not even compile due to obvious formatting errors.

I always get a HIGH value when I’m trying to read the button state…

If you use this example

Does it work?

Your posting isn’t even reading the separate arduino input that the key/button should be connected to.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a copy of your complete compiling sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom… :slight_smile:

the variable "value" is never declared is it a "byte' or an "int" or?

//Return a value > 0 when button unpressed and value = 0 when button pressed

this doesn't make sense for a analogRead.

the x and y axis require only analogRead

the z axis is a button and so requires only digitalRead.

const int joy_z = 6;
int value;  // needed to declare this

void setup()
{
  
  pinMode(joy_z, INPUT_PULLUP); 
}

void loop() {
 
value = digitalRead(joy_z); // this is the only read needed

  delay(700);
}