# Potentiometer Values jump from 30 to 500

I am using a parallax joystick a control for a motor speed. First I am reading the value of the Pot using the serial monitor. Once the monitor starts it reads about 30 (joystick at center position), then when I move the stick up it jumps straight to 500ish and continues to increment up to about 680. At first when it happened it thought it could be the joystick, so i tried out a 10k Pot, but it still didn't read properly. I have the joystick wired correctly, i checked the data sheet.

``````int JoystickVal = 0;
int PotVal = 0;
int PotPin = 0;

void setup() {

//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin

Serial.begin(9600);

}

void loop () {

JoystickVal = map(PotVal, 0, 1023, 0, 255);
JoystickVal = constrain(JoystickVal, 0, 255);

Serial.println(PotVal);

if (JoystickVal > 30) {
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW);   //Disengage the Brake for Channel A
analogWrite(3, JoystickVal);   //Spins the motor on Channel A at full speed
}

else {
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW);   //Disengage the Brake for Channel A
analogWrite(3, 0);   //Spins the motor on Channel A at full speed
}

delay(100);

}
``````

Thanks!

connor1868:
I am using a parallax joystick a control for a motor speed. First I am reading the value of the Pot using the serial monitor. Once the monitor starts it reads about 30 (joystick at center position), then when I move the stick up it jumps straight to 500ish and continues to increment up to about 680. At first when it happened it thought it could be the joystick, so i tried out a 10k Pot, but it still didn't read properly.

What values are you expecting? When it jumps, have you tried measuring the voltage across the analog pin and ground to see if it matches with the sensor data?

``````  JoystickVal = map(PotVal, 0, 1023, 0, 255);
``````

A simple right shift by 2 will accomplish the same thing much more efficiently.

``````  JoystickVal = constrain(JoystickVal, 0, 255);
``````

Since PotVal will never be below 0 and never above 1023, it will never be below 0 and above 255, so constraining it to values it is already constrained by isn't necessary.

connor1868:
I have the joystick wired correctly, i checked the data sheet.

OK. The datasheet shows more than one way to hook it up. Please tell us how you connected the joystick. Please tell us how you connected the potentiometer, too. Please be specific about your setup, rather than the datasheet.

Try disconnecting the pot and connecting the analog input directly to ground, and then to 5V. You should get readings around 0 and 1023. If you get the readings you expect, that proves your Arduino and sketch are working OK and you have a hardware or wiring problem with the joystick/pot.

A multimeter (which you can get online here in the U.S. starting around \$10) would be a good investment... You could measure the voltage out of the pot.

Is the pot/joystick ground connected to the Arduino ground?

(If you happen to have an audio taper pot it will be non-linear. It will still put-out about 5V maximum and 0V minimum, but in the center it will put-out about 0.5V or about 4.5V depending on which way you wire it.)

I am using a parallax joystick to determine the speed of a motor using pwm. When i read the value of the pot using the serial monitor on the arduino, compared to when im using the motor shield. The value when using just the arduino is as expected, 0 - 1023, but when using the motor shield it is 0 - 30ish, then jumps to about 550ish. This makes no sense because it is using the same pin.

My code

``````int PotPin = A0;
int PotVal = 0;
int JoystickVal = 0;
int MotorSpeedForward = 0;
int MotorSpeedBackward = 0;

void setup()
{

//pinMode( PotPin, INPUT)
Serial.begin(9600);

}

void loop()
{

int JoystickVal = map(PotVal, 0, 1023, 0, 255);

Serial.println(JoystickVal);

MotorSpeedForward = map(JoystickVal, 147, 255, 0, 255);

MotorSpeedBackward = map(JoystickVal, 0, 107, 0, 255);

if (JoystickVal > 150)
{

digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW);   //Disengage the Brake for Channel A
analogWrite(3, MotorSpeedForward);   //Spins the motor on Channel A at full speed

}

else if(JoystickVal < 115)
{

digitalWrite(12, LOW); //Establishes backward direction of Channel A
digitalWrite(9, LOW);   //Disengage the Brake for Channel A
analogWrite(3, MotorSpeedBackward);   //Spins the motor on Channel A at full speed

}

}
``````

Edit: Topics merged, my post makes no sense now.

Very similar topics merged

connor1868:
when using the motor shield it is 0 - 30ish, then jumps to about 550ish.

What is 'it'?

Pot/joystick code setup for testing with the serial monitor.

``````//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor

#include <Servo.h>
Servo myservo1;
Servo myservo2;

int potpin2 = 1;

int newval1, oldval1;
int newval2, oldval2;

void setup()
{
Serial.begin(9600);
myservo1.attach(2);
myservo2.attach(3);
Serial.println("testing dual pot servo");
}

void loop()
{
newval1 = map(newval1, 0, 1023, 0, 179);
if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){
myservo1.write(newval1);
Serial.print("1- ");
Serial.println(newval1);
oldval1=newval1;
}