View Full Version : Potentiometer Help

04-23-2014, 08:03 AM
Hi ,
Having some issues with the coding
trying to use a potentiometer for the obs knob and translating that via mapping it, yet I cant seem to get it to work,

just the last part of the code

void INPUTS() // Simconnect codes and "Keys" section
Kstringnewstate = "";
for (int KpinNo = 8; KpinNo < 70; KpinNo++){
KpinStateSTR = String(digitalRead(KpinNo));
KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 8));
if (KpinStateSTR != KoldpinStateSTR)
if (KpinNo != 13){
if (KpinNo == 8 && KpinStateSTR == "0"){Serial.println ("C01");} //sets gear handle up
if (KpinNo == 9 && KpinStateSTR == "0"){Serial.println ("C02");} //sets gear handle down
if (KpinNo == 10 && KpinStateSTR == "0"){Serial.println ("C15");} //Decrements flap handle position
if (KpinNo == 11 && KpinStateSTR == "0"){Serial.println ("C14");} //Increments flap handle position
if (KpinNo == 12 && KpinStateSTR == "0"){Serial.println ("A02");} //Increments COM1sb by one MHz
if (KpinNo == 14 && KpinStateSTR == "0"){Serial.println ("A01");} //Decrements COM1sb by one MHz
if (KpinNo == 15 && KpinStateSTR == "0"){Serial.println ("A426543");} //Sets transponder code to 6543
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 360); // scale it to use it with the servo (value between 0 and 180)
{Serial.println ("A33") (val);

// Add more here but remember to change the figure in the next line down. (the 15)
if (KpinNo > 15){ //Change pinNo number to same as the highest one used for simconnect codes.(the 15)
Serial.print ("D");
if (KpinNo < 10) Serial.print ("0");
Serial.print (KpinNo);
Serial.println (KpinStateSTR);
Kstringnewstate += KpinStateSTR;
Kstringoldstate = Kstringnewstate;
}// end of INPUTS void

any help would be appreciated



04-24-2014, 04:43 AM
I suggest you study some codes for servos. Think about the difference of 180 degrees servos and the instrument that has 360 degree rotation of the needle, and position determination of the servo.

04-24-2014, 05:54 AM
Um, I mean using a raw potentiometer as an input for the obs knob.


04-24-2014, 11:24 AM
I recommend using a Rotary Encoder to make your OBS or HDG knob.


04-24-2014, 02:27 PM
<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--> Henri, in response to your last thread Jim suggested that you should learn some basic C programming before trying to write your own sketches. You’ve got the same problem here; you are confusing string variables with integer variables and using them interchangeably. Can’t do that, it doesn’t work (as you have found out).

C programming is really not hard to learn, there are plenty of books and on-line tutorials to get you started. You will gain a tremendous feeling of accomplishment when you can read a line of code and understand exactly what it does and why it’s there, and eliminate your current frustration from just copying code and not having it work.

<!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

Matti V
04-25-2014, 04:21 AM
Normally potentiometers read analogread (value) and if else what doing.
but flight sim have better use encoder if need use knob.
jims site have good examples and help to encoder and many other. just i learn thats too :)
and can google net many examples how use.
And servo have many way too how tell to servo were must go position.

04-25-2014, 06:46 AM
Hi Henri,
I suggest you to use rotary encoder as others in this forum
But even with potentiometer, it'll work very well
Many things I don't understand in your code
1) you want to move the obs knob so you use "A33" code but obs code is normally "B33"
2)your print code:
Serial.println ("A33") (val);
May be I wrong, but I not sure the print C language command in arduino accepts 2 parameters. Second point, the variable val need to be convert in ascii characters because it's an integer value

I'll do that...(a code among many other)

char buf[6]; <- temporay variable for conversion. Need to be stated in your global variable section or in the localy fonction INPUT()


Hope this help you a little