user7936: I am using the Grove - Slide Potentiometer (http://wiki.seeedstudio.com/Grove-Slide_Potentiometer). How can I set the potValue so that at maximum it will 128?
int potPin= A0;   
int readValue;   
int potValue;  

void setup() {
  pinMode(potPin, INPUT);   
  Serial.begin(9600);       
}

void loop() {
 readValue = analogRead(potPin);   
 potValue = (255./500.) * readValue;  
 Serial.print("You are writing a value of ");   
 Serial.println(potValue);
}


user31105: You can use the map function, see https://www.arduino.cc/reference/en/language/functions/math/map/.
Example: If your pot meter gives values from 100 to 1000 and you want to map that range to a range of 0 to 128 you can easily write:
potValue = map(readValue, 100, 1000, 0, 128);

High likely, in your case and if the potentiometer is 'perfect' it returns values from 0 to 1023 and you can write:
potValue = map(readValue, 0, 1023, 0, 128);


user42539: analogRead() returns 0x0000 to 0x03ff
If the max data 0x03ff is shifted to the right by 3 bits (or divide by 8), 
then 0x03ff becomes 0x007f, or 127.
I think this will do it:
desiredMax = analogRead(A0) >> 3; // divide 1023 max by 8, becomes 127 max


7936: <p>I am using the Grove - Slide Potentiometer (<a href="http://wiki.seeedstudio.com/Grove-Slide_Potentiometer" rel="nofollow noreferrer">http://wiki.seeedstudio.com/Grove-Slide_Potentiometer</a>). How can I set the potValue so that at maximum it will 128?</p>

<pre><code>int potPin= A0;   
int readValue;   
int potValue;  

void setup() {
  pinMode(potPin, INPUT);   
  Serial.begin(9600);       
}

void loop() {
 readValue = analogRead(potPin);   
 potValue = (255./500.) * readValue;  
 Serial.print("You are writing a value of ");   
 Serial.println(potValue);
}
</code></pre>
 31105: <p>You can use the <code>map</code> function, see <a href="https://www.arduino.cc/reference/en/language/functions/math/map/" rel="nofollow noreferrer">https://www.arduino.cc/reference/en/language/functions/math/map/</a>.</p>

<p>Example: If your pot meter gives values from 100 to 1000 and you want to map that range to a range of 0 to 128 you can easily write:</p>

<pre><code>potValue = map(readValue, 100, 1000, 0, 128);
</code></pre>

<p>High likely, in your case and if the potentiometer is 'perfect' it returns values from 0 to 1023 and you can write:</p>

<pre><code>potValue = map(readValue, 0, 1023, 0, 128);
</code></pre>
 42539: <p>analogRead() returns 0x0000 to 0x03ff</p>

<p>If the max data 0x03ff is shifted to the right by 3 bits (or divide by 8), 
then 0x03ff becomes 0x007f, or 127.</p>

<p>I think this will do it:</p>

<pre><code>desiredMax = analogRead(A0) &gt;&gt; 3; // divide 1023 max by 8, becomes 127 max
</code></pre>
