Problem reading analog inputs with nano

i am trying to read the values of the positions of a joystick module using this code:

int x;
int y;
void setup(){
  Serial.begin(9600);
}
void loop(){
  x=map(analogRead(0),0,1023,0,100);
  y=map(analogRead(1),0,1023,0,100);
  if ( x < 50 ) Serial.print("back ");
  else if( x == 50 ) Serial.print("equal ");
  else Serial.print("forward ");

  if ( y < 50 ) Serial.print("left ");
  else if( y == 50 ) Serial.print("equal ");
  else Serial.print("right ");
  Serial.println();
  delay(25);
}

but its not giving the right values. Its always giving 'forward right' or 'back left' and never changing. Ive tested the joystick module on a working arduino uno and it works perfectly with it. Ive also used this nano's digital pins and they work fine, too. So now im at loss as to where the problem is.

try just printing the values being read to see if they change correctly based on the stick positions.

Serial.println (analogRead (0));

It seems that you switch between analog inputs too fast.
Try to move your delay(25) between the readings of values x and y:

1 Like

huh?

If you read several ADС channels in a row, their values begin to influence each other.
For accurate measurements, you need to add a delay between channels switching or read each channel twice and discard the first result.

The magnitude of that effect depends, in this case, on the resistance of the pot. If they're 10k or less, I would expect minimal 'charge transfer' from one input to the next.
The better fix, if it is charge transfer, is to just read each input twice.
However, it's a red herring, as the described problem doesn't fit the normal charge transfer effects, which would be 'noise'.
@ac-rafi It sounds to me like you're reading a digital, not an analog, for some reason. You're either getting 0, or full ADC counts.

no the adc value hardly changes at all even if i keep moving it. it just gives values somewhere in the middle (623-641 sometimes 700+)

no they dont, i did it with one. it just keeps values in the range 623-641 even though im moving it

Could you try reading A0 and A0?

Just a WAG here.

[quote="ac-rafi, post:1, topic:1184061"]
Ive tested the joystick module on a working arduino uno
[/quote]

Post the test code from the UNO that worked.

a7

i tried, doesnt work. And i used the same code for uno.

could you post the code that "doesn't work" and the output from the serial monitor?

I already gave my original code in my post, but then i simplified it after reading u guys' replies.

void setup(){
  Serial.begin(9600);
}
void loop(){
  Serial.println(analogRead(A0));
  delay(100);
}

these are few lines from the serial monitor:
782

782

783

782

785

789

keep in mind im moving the joystick around constanty

Will do.

Now you need to satisfy our curiosity and post a schematic of how you have this hooked up.

Hand drawn is easiest and perfeectly adequate. Show all parts and supplies of power.

TIA

a7

also can there be something wrong with the ref pin on the board guys? ive soldered the pins myself and maybe i made a mistake? It looks fine but how do i check it properly? i just know that the ref pin has some impact on adc values. But i dont know how it works

im using the built in usb cable of the board to power it. Ive connected the gnd and 5v pins of the joystick to the board's gnd and 5v and the Vx pin of the module to A0 of the board. im using jumber wires(F2F) for connecting them

can you check with a voltmeter? make sure the center pin of the pot (Vx) changes when you move the stick

what would you use the Vref for if you connected the pot across 5V/grnd

What should i connect the multimeter to?

i meant the Analog Ref or just ref pin. I read somewhere that it affects the adc readings

connect the voltmeter between ground and the Vx pin

the reading doesnt change.
but the same joystick works with my arduino uno board

i think ive found the problem. I soldered this nano myself and i mightve displaced these capacitors(i dont know for sure) from the tx, rx and L pins. Does anybody know what these are and how might these affect this?
nan

(the first is a pic i downloaded from the internet and the second one is mine)