Analog Input: map() and constrain()
map() #
Arduino has a a very useful map() function that might be familiar to you from Processing or p5.js.
int sensorValue;
int mappedValue;
sensorValue = analogRead(A0);
mappedValue = map(sensorValue, 0, 1023, 0, 255);
constrain() #
The map() function only scales the values from one range to another, it does not clamp or constrain the values to the range that you have set. Fortunately, there is also constrain() that we can use. Just add it after your map() like in the example below.
int sensorValue;
int mappedValue;
sensorValue = analogRead(A0);
mappedValue = map(sensorValue, 0, 1023, 0, 255);
mappedValue = constrain(mappedValue, 0, 255);