tags:

views:

40

answers:

2

Hey there, I have the following code:

  sVal = analogRead(potPin);    // read the value from the sensor
  valMin = min(sVal, 1);
  valMax = max(sVal, 128);
  constrain(sVal,valMin,valMax);

  itoa(sVal, res, 10);
  println(res);
  println(" ");
  delay(150);
  clearScreen();

Now for some reason, the output on the gLCD screen is almost constantly 1023. I would like the minimum for the potentiometer to be 1 and the maximum to be 128.

+5  A: 

Your code indicates a lack of understanding of the min, max and constrain functions. I suggest you read the documentation more carefully.

In the meantime, here is what I think you're after:

sVal = analogRead(potPin);
sVal = sVal / 8 + 1; //scale value [0.. 1023] to [1.. 128]

itoa(sVal, res, 10);
println(res);
println(" ");
delay(150);
clearScreen();
Artelius
Thank you, I re-read it ;)
Neurofluxation
+3  A: 

there is also a range mapping function already in the API, e.g.:

 res = map(analogRead(potPin), 0,1023, 1,128);
Joakim Rosqvist