A month ago, I connected a joystick with an Arduino Uno. VCC with Arduino 5v, GND with ground and Y axis with analog. I tried all, but the error persist with all 6.
A month ago, in the serial monitor I got data ranging from 0 to 1023. Today, when I retry it, something strange happens. In the center position it shows 255. As I move my joystick forward, the pattern goes like this 255 0... 50...200... 255... 0.. 50... 150.... and stops at 255. When I pull it back same happens, 255... 200... 150... 100... 0 255... 100.. 50... and stops at 0.
I don't understand why it changed. It was kept in a plastic box under normal conditions, and now the analog pins give incorrect data.
I tried the joystick with another Arduino, and I get the right data, but I want to know what the problem is, if I can fix it, how to, why it happens and how to prevent it.
PS: I didn't change any wiring or give extra voltage or did some stuff that could damage my Arduino. All other parts work fine.
Can you tell me what mistake I might be making?
#include<SPI.h>
void setup() {
Serial.begin(9600);
pinMode(A3, INPUT); // I tried with this line and without this line too. Same results
}
void loop() {
y1 = analogRead(4);
y2 = analogRead(5);
Serial.println(y1);
Serial.println(y2);
delay(50);
}