# Potentiometer as encoder, missing something simple?

Hi,
I'm writing a code for a potentiometer to convert the voltage it reads to degrees. Now, everything was going swimmingly until I used the line equation to calculate degrees. The potentiometer I have turns from about 240 degrees to -60 (I used some electrical tape to make a flag so I could measure that more easily.)
The voltage is reading just fine from 0 to 1023, but when I try to get degrees it prints -60. I've tried a few variations, but the equation just isn't getting the right value.

``````int potPin = 2;
float val;
float temp;
float temp2;
float deg;

void setup() {
Serial.begin(9600);
getVolt();
}

void loop() {

if (abs(getVolt()-temp) > 2){
temp = val;
Serial.println(val);
deg = val*(100/341)-60;
Serial.println(deg);
Serial.print(" degrees.");
}
else{

}
}

float getVolt(){

return val;
}
``````

I got it working!

I put in 'deg = val100;
changed it to deg = val
100/341;
that ALSO worked,
finally I put in deg = val*100/341-60;

holy freak'n cow it worked....the exact code I had before, I guess I just had to
break it to the arduino slowly...I guess it got emotional and couldn't handle all
the sweat math all at once, lol.

Do you know of the map() function? Use it to map one range of numbers to another range of numbers.

``````deg = val*(100/341)-60;
``````

Now you have

``````deg = val*100/341-60;
``````

So you don't have

.the exact code I had before

groundFungus:
Do you know of the map() function? Use it to map one range of numbers to another range of numbers.

He want to convert to degree in float type, therefore map() function CANNOT be used. Instead, use floatMap() function