Arduino potentiometer code not working

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:


#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;

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.

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.

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.

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.

It only happens for 252.

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

Yeah

So test for a range of values...

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

Confusing names by the way...

NM I see now..

int throttle = 249;

But this creates a duplicate version of '_throttle':

  int throttle_ = 252;

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

Capitals matter btw

Throttle is not throttle those are 2 different values.

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.

variables sorry for the typo.

#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.

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. :slight_smile:

k wait

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.

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

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.