Help with Arduino and Analog Min Max settings
Hey there, I have the following code:
sV开发者_如何转开发al = 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.
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();
there is also a range mapping function already in the API, e.g.:
res = map(analogRead(potPin), 0,1023, 1,128);
精彩评论