 # (Solved) Potentiometer outputs - joystick

Hi!

Im totally new to any sort of programing and im sure the answer to this already is on the forum, but i cant find it!

I want to make the pot to work like this, (joystick)

POT: 0-----512----1023 PWM:255----0----255

the pwm will go to 2 mosfet's -> 2 solenoid

i do get it to work one way but not both...

how do i do?

Did you mean -255 to +255?

``````int PWM = map(analogRead(PotPin), 0, 1023, 255, -255);
``````

Did you mean +255 to 0 to +255?

``````int PWM = map(analogRead(PotPin), 0, 1023, 255, -255);
PWM = abs(PWM);
``````

johnwasser: Did you mean +255 to 0 to +255?

``````int PWM = map(analogRead(PotPin), 0, 1023, 255, -255);
PWM = abs(PWM);
``````

Thats the one, Thanks a million!

i would however need the one input go to 2 outputs,

pot 0-511 -> analog 9 pot 512-1023 -> analog 10

can someone teach an ignorant fool? :D

Use the value of PWM in if/else seems the obvious way.

UKHeliBob: Use the value of PWM in if/else seems the obvious way.

Guess i got it wrong the first 15 times!...

Now im possibly down to the last hurdle, I need the joystick movment on the Y axis substract from the X axis.

Tried alot of different code but cant get it right!

This works:

``````void loop() {

pwmX = map(analogRead(A1), 0, 1023, 255, -255);
pwmX = abs(pwmX);
delay(10);

pwmY = map(analogRead(A0), 0, 1023, 255, -255);
pwmY = abs(pwmY);
delay(10);

{
analogWrite(9, pwmX);
analogWrite(10, pwmX);
}
``````

Here's the problem:

``````else if (analogRead(A1) > 612) while (analogRead(A0) > 612)
{
analogWrite(9, pwmX);
analogWrite(10, ?);         //what should this be?
}
``````

In simple i want the pin 10 to be: pwmX - pwmY (the more i move the joystick to the right, the less pwmX on the pin)

any thoughts??

I need the joystick movment on the Y axis substract from the X axis.

Why? That doesn't make sense.

In simple i want the pin 10 to be: pwmX - pwmY

No, you don't. You MIGHT want the VALUE written to the pin to be pwmX - pwmY. If that IS what you want, why don't you DO that?

PaulS: Why? That doesn't make sense.

Think of a tracked vehicle, pin 9 and pin 10 = forward motors

PaulS: No, you don't. You MIGHT want the VALUE written to the pin to be pwmX - pwmY. If that IS what you want, why don't you DO that?

Well yes, corrected, but pwmX - pwmY changes nothing on my output, neither will x + y, x / y, x * y...

Well yes, corrected, but pwmX - pwmY changes nothing on my output, neither will x + y, x / y, x * y...

What do you see if you print pwmX and pwmY before the subtraction and pwmX - pwmY after the subtraction ?

Think of a tracked vehicle, pin 9 and pin 10 = forward motors

I still don't see what you are doing and why. The normal way to control a tracked vehicle is to control the speed of the two tracks independently but I assume that you have some other scheme in mind.

UKHeliBob: I still don't see what you are doing and why. The normal way to control a tracked vehicle is to control the speed of the two tracks independently but I assume that you have some other scheme in mind.

well thats kind of what im trying to achieve... using 1 joystick to control both motors (or in this case output-> mosFET-> 0v-12v solenoids, controlling hydraulics)

joystick forward - both outputs equal

joystick forward and right - left output more, and right less according to joy right amount

did that make sens? :D

UKHeliBob: What do you see if you print pwmX and pwmY before the subtraction and pwmX - pwmY after the subtraction ?

sorry but i dont understand.. my entire programing experience is 2 days long... code example?

i could go about this all wrong so im open to suggestions but id prefer not to use libraries..

``````int PWM = map(analogRead(PotPin), 0, 1023, 255, -255);
int outPin = 9;
if (PWM > 0)
outPin = 10;
PWM = abs(PWM);
``````

That works on one axis.

``````int PWMY = map(analogRead(PotPinY), 0, 1023, 255, -255);
PWMY = abs(PWMY);
int PWMX = map(analogRead(PotPinX), 0, 1023, 255, -255);
int PWM9 = PWMY;
int PWM10 = PWMY;
if (PWMX > 0)
PWM10 = abs(PWM10 - PWMX);
else
PWM9 = abs(PWM9 + PWMX);
``````

you'll need some additional logic for the motors to determine direction of each one, but that should give you correct relative speeds based on the joystick position.

Ack. Realised that only works if the angle of the joystick is less than 45 degrees. Also, additional logic for the actual direction of the motor, since this assumes that you'll never have one motor going forward and the other going backward, which becomes the case if the angle of the joystick is greater than 45 degrees, ie: abs(PWMX)>abs(PWMY). In this case, reverse the direction of the motor on the appropriate output pin.

Quote from: UKHeliBob on Today at 12:36:54 What do you see if you print pwmX and pwmY before the subtraction and pwmX - pwmY after the subtraction ? sorry but i dont understand.. my entire programing experience is 2 days long... code example?

As an aid to debugging Serial.print() the values that you are interested in where their value is important.

``````Serial.print("pwmX : ");
Serial.println(pwmX);
Serial.print("pwmY : ");
Serial.println(pwmY);
Serial.print("pwmX - pwmY : ");
Serial.println(pwmX - pwmY);
``````

Finally got it to work as i want it!

Thanks so much for all the help!

here’s the code if someone else would need it:

``````int pwmX = 0;
int pwmY = 0;
int pYX = 0;
int turnl = 0;
int turnr = 0;

void setup() {

}

void loop() {

pwmX = map(analogRead(A1), 0, 1023, 255, -255);
pwmX = abs(pwmX);
delay(10);

pwmY = map(analogRead(A0), 0, 1023, 255, -255);
pwmY = abs(pwmY);
delay(10);

pYX = pwmX - pwmY;
pYX = constrain(pYX, 0, 255);

if (turnl)  //forward and turn left
{
analogWrite(10, pYX);
analogWrite(9, pwmX);
}
else if (turnr)      //forward and turn right
{
analogWrite(9, pYX);
analogWrite(10, pwmX);
}

else if (analogRead(A0)) //joystick centered, output cutoff
{
digitalWrite(9, LOW);
digitalWrite(10, LOW);
}