# 3 potentiometers clutch

Hello there,

I’m having some troubles with programing 3 potentiometers intended to work as a clutch for my steering wheel.

What I want to do, is to make 1 potentiometer to control the percentage of the other 2.
Like if the first potentiometer is in the minimum position, only the second will work, if the first potentiometer is in half, both the other will take half of the total value each.

Here follows the code,

``````#include <Joystick.h>

Joystick_ Joystick;

int zAxis_=0;
int RxAxis_=0;
int RyAxis_=0;
int RzAxis_=0;
const bool initAutoSendState = true;

void setup()
{
Joystick.begin();
}

void loop(){

RxAxis_ = map(RxAxis_,0,1023,0,255);

RyAxis_ = map(RyAxis_,0,1023,0,255);

zAxis_ =  ((RzAxis_/100) * RxAxis_) + RyAxis_;
zAxis_ = map(zAxis_,0,1023,0,255);
Joystick.setZAxis(zAxis_);

delay (50);
}
``````

I believe it is quite simple, but I’m no programmer and I’m having some troubles to do so…
Can this be done? Is there a better way?

RafaelEsperanca: What I want to do, is to make 1 potentiometer to control the percentage of the other 2. Like if the first potentiometer is in the minimum position, only the second will work, if the first potentiometer is in half, both the other will take half of the total value each.

Leave the programming aside for a moment and let's try to describe this in English.

My interpretation is as follows

• The output is the sum of the values of P2 and P3
• If P1 is 0 then the value of P3 is treated as 0 (so that P2 + P3 is just the value of P2)
• if P1 is greater than 0 then value P2 + P3 is multiplied by the ratio P1/1023

...R

Robin2: Leave the programming aside for a moment and let's try to describe this in English.

My interpretation is as follows

• The output is the sum of the values of P2 and P3
• If P1 is 0 then the value of P3 is treated as 0 (so that P2 + P3 is just the value of P2)
• if P1 is greater than 0 then value P2 + P3 is multiplied by the ratio P1/1023

...R

Almost there, if P1 is greater than 0 then value P2 is multiplied by the ratio P1/1023 and P3 would use an inverted value of P1 to be multiplied with.

Something like this i guess,

P2*(P1/100) + P3*(1-(P1/100))

RafaelEsperanca: Almost there, if P1 is greater than 0 then value P2 is multiplied by the ratio P1/1023 and P3 would use an inverted value of P1 to be multiplied with.

Something like this i guess,

P2*(P1/100) + P3*(1-(P1/100))

You are the person that is defining the requirement so you need to do better than "guess".

I did not see any reference to an inverted value of P1 in your Original Post - but if that is the requirement then go for it.

Now that we have spent some time describing the requirement have another go at writing code to implement it.

By the way, analogRead() gives values from 0 to 1023 so your maths should be

``````P2*(P1/1023) + P3*(1-(P1/1023))
``````

And if you are doing integer maths be careful to avoid underflow. For example 1 / 1023 gives 0. And the calculation P2*(P1/1023) should be calculated as P2 * P1 / 1023 - i.e. do the multiplication first. Even then you may have underflow. A simple way would be to multiply by 2048 first, then do the calculations and after that divide by 2048 to get back to regular values. Make sure to use long variables.

...R

Robin2:
And if you are doing integer maths be careful to avoid underflow. For example 1 / 1023 gives 0. And the calculation P2*(P1/1023) should be calculated as P2 * P1 / 1023 - i.e. do the multiplication first. Even then you may have underflow. A simple way would be to multiply by 2048 first, then do the calculations and after that divide by 2048 to get back to regular values. Make sure to use long variables.

Thank you so much!
Actually all I had to do was to correct the calculation as you said and change from int to float.

This is how it looks now

``````#include <Joystick.h>

Joystick_ Joystick;

float zAxis_=0;
float Pot1=0;
float Pot2=0;
float Pot3=0;
float A;

const bool initAutoSendState = true;

void setup()
{
Joystick.begin();
}

void loop(){

Pot2 = map(Pot2,0,1023,0,255);

Pot3 = map(Pot3,0,1023,0,255);

A = (Pot3 * Pot1/1023)+ (Pot2 * (1-(Pot1/1024)));

zAxis_ = A;
zAxis_ = map(zAxis_,0,1023,0,255);
Joystick.setZAxis(zAxis_);

delay (50);
}
``````

And again, thank you