http://www.servocity.com/html/4_function_joystick.html
Hi Guys,
I have bought a joystick as picture above. I tried to connect the potentiometer(the one on stick) to arduino by using following sample code:
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
delay(10);
}
I got the result sensorValue reading form 0~250. If I leave it in nutrual it reads around 50. I spin left, it read 0 when I'm half way to the end. It seems still planty of angle I can still rotate it.
If I spin right it reads up to 250+, so it looks like:
<------------0-----------50-------------------------250>
N
The neutral reading suppose to be somewhere in between 125 or something?
Any suggestion I can do to get proper reading? Thanks in advance for any advice.
Figure an offset at start up
Here’s what I do:
Right at start-up, read your joystick. Lets say it is 530. Subtract this from what center should be (512). Call this number your offset --actually, you will have 3 offsets, X,Y and rotate.
Now, during your main loop, read your joystick and add this offset back on. You only need to add because your offset will be either positive or neg. and thus will “add or subtract” itself as needed.
This will artificially center your joystick. --I would also do all this offsetting BEFORE you convert to your 0-255 scale.
Have you …
put a multimeter on the pot in question to see what the resistances look like?
Thanks guys for all your
Thanks guys for all your input. I tried all method but still getting strange reading. Maaybe still my mistake. However, I find a way to work around. Since I leave it in neutral it reads 60~70, rotate to the end of left it reads 0 and won’t read minus int further, turn right it reads up to 500 and drop to 200 when I rotate to the end of right. I kinda hack by following code to adjust the reading:
if(sensorValue>70){
sensorRead = sensorValue/5+60;
}else{
sensorRead = sensorValue;
}
This way the reading looks better. Yeah, only looks but I am sure it’s not proper way to solve this. I got 2 of these joysticks and it reads all the same way. X,Y axis reading is 100% perfect but only rotating potentiometer if different. I even open case to try it but seems not much I can do to modify it.
Anyways, thanks again for all helps and suggestions. I’ll keep the way it is now until if anyone else has proper solution for it.