# Arduino potentiometer code not working

**URL:** <https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796>\
**Category:** Programming\
**Created:** [January 12, 2023, 11:42pm UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796 "2023-01-12T23:42:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 12, 2023, 11:42pm UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/1 "2023-01-12T23:42:17Z")

</div>

Hi, i am having a problem with programming my arduino.I am trying to make a throttle for xplane12.  
I did the mechanical stuff but my potentiometer is kind of glitchy.Basically what i am trying to say is that my potentiometer is giving me two different numbers.What i am trying to make is 250 and 251  
252.I have already wrote one code for this but, it is not working.Here is the code that i wrote:

```cpp

#include <Joystick.h>

#define Throttle A1

int throttle_ = 0;

Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK,0,0,false,false,false,false,false,false,false,true,true,true,false);

const bool initAutoSendState = true;

void setup() {
  // put your setup code here, to run once:
Joystick.begin();
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
int throttle = 249;

throttle_ = analogRead(Throttle);
throttle_ = map(throttle_,0,1023,0,255);
if(throttle_ >= throttle){
  int throttle_ = 252;
}
Joystick.setThrottle(throttle_);
Serial.println(throttle_);
}

```

Dont focus on Joystick.setThrottle(throttle\_);  
That's just to make ingame stuff work.  
That is not important.  
What is important is this part of the code:  
if(throttle\_ \>= throttle){  
int throttle\_ = 252;

```cpp

```

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [January 12, 2023, 11:46pm UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/2 "2023-01-12T23:46:14Z")

</div>

if you have a cheap pot or you are sending too much current through the pot, then make your test cover a small range of numbers. Greater than one and less than the other.

---

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 12, 2023, 11:53pm UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/3 "2023-01-12T23:53:34Z")

</div>

I do have a cheap pot but and its giving me 2 different values.If you want i can send you a screenshot of my serial monitor.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 12, 2023, 11:56pm UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/4 "2023-01-12T23:56:07Z")

</div>

No need, we believe you. But, you're not listening. It is entirely normal for the values to change like that. You have to test for a range of values.

---

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 12, 2023, 11:58pm UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/5 "2023-01-12T23:58:18Z")

</div>

I did test it.I know that it is normal but xplane12 the game i am playing is very picky about that stuff. So i really really need to make 252.

---

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 12, 2023, 11:59pm UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/6 "2023-01-12T23:59:28Z")

</div>

It only happens for 252.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 12, 2023, 11:59pm UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/7 "2023-01-12T23:59:47Z")

</div>

Only, needs to **see the value** 252, though...

---

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 13, 2023, 12:00am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/8 "2023-01-13T00:00:00Z")

</div>

Yeah

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 13, 2023, 12:00am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/9 "2023-01-13T00:00:17Z")

</div>

So test for a range of values...

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 13, 2023, 12:02am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/10 "2023-01-13T00:02:35Z")

</div>

> [@azur123123](#):
>
> ```arduino
> throttle_ = analogRead(Throttle);
> throttle_ = map(throttle_,0,1023,0,255);
> if(throttle_ >= throttle){
> int throttle_ = 252;
> }
> 
> ```

Won't this always happen? 'throttle' is always 4 times the value of '\_throttle".

Confusing names by the way...

NM I see now..

```arduino
int throttle = 249;

```

But this creates a duplicate version of '\_throttle':

```arduino
  int throttle_ = 252;

```

---

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 13, 2023, 12:04am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/11 "2023-01-13T00:04:44Z")

</div>

Throttle is A1 pin on arduino it is reading thing from the pot.  
throttle\_ is the variable that you send to the game.

---

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 13, 2023, 12:05am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/12 "2023-01-13T00:05:14Z")

</div>

Capitals matter btw

---

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 13, 2023, 12:05am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/13 "2023-01-13T00:05:45Z")

</div>

Throttle is not throttle those are 2 different values.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 13, 2023, 12:06am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/14 "2023-01-13T00:06:11Z")

</div>

How many versions of 'throttle' do you have? Are you trying to obfuscate the code? You almost couldn't make it more confusing without trying...

This is very frustrating for me. I'll sit back and watch the thread. Good luck.

---

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 13, 2023, 12:06am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/15 "2023-01-13T00:06:20Z")

</div>

variables sorry for the typo.

---

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 13, 2023, 12:09am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/16 "2023-01-13T00:09:09Z")

</div>

#include \<Joystick.h\>

#define potpin A1

int throttle\_ = 0;

Joystick\_ Joystick(0x12, JOYSTICK\_TYPE\_JOYSTICK,0,0,false,false,false,false,false,false,false,true,true,true,false);

const bool initAutoSendState = true;

void setup() {

// put your setup code here, to run once:

Joystick.begin();

Serial.begin(9600);

}

void loop() {

// put your main code here, to run repeatedly:

int variable1 = 249;

throttle\_ = analogRead(potpin);

throttle\_ = map(throttle\_,0,600,0,255);

if(throttle\_ \>= variable1){

int throttle\_ = 252;

}else

Joystick.setThrottle(throttle\_);

Serial.println(throttle\_);

}  
There you go i changed it up a bit.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 13, 2023, 12:09am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/17 "2023-01-13T00:09:41Z")

</div>

Please go back and edit your post and put the code in code tags.

> There you go i changed it up a bit.

But didn't say how. I made the right decision. 🙂

---

<div class="post-metadata">

**Author:** ![azur123123](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/azur123123/32/908778_2.png) [@azur123123](https://forum.arduino.cc/u/azur123123)\
**Post date:** [January 13, 2023, 12:10am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/18 "2023-01-13T00:10:54Z")

</div>

k wait

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 13, 2023, 12:13am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/19 "2023-01-13T00:13:52Z")

</div>

Rewrite the whole thing with sensible variable names and I might help, but I can't say when. It's getting late here. But other people will also care.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [January 13, 2023, 12:15am UTC](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796/20 "2023-01-13T00:15:59Z")

</div>

> [@azur123123](#):
>
> What i am trying to make is 250 and 251  
> 252

Then change the map scale. Easy peasy. I'm assuming the requirement is actually x \>= 252. Such as

```arduino
throttle_ = map(throttle_,0,1012,0,255);

```

Before you even touch the keys to program, you should run a test program to print un-converted ADC values. Then you absolutely know what the high pot setting will yield.

[Next page](https://forum.arduino.cc/t/arduino-potentiometer-code-not-working/1076796.md?page=2)
