# handling the analog input values

Any idea what is the best way execute the following function, so the range between 0-1023 in analog input
should be divided by 30 and if the analog input value is between 0 - 34.1 so the parameter value is 1 and
if the analog input value is between 34.1 - 68.2 so the parameter values is 2 and so on…

I know that I’m able to do that with following function:

value = analogRead(iPotentiometer);
if (value < 34.1 && value > 0)
value2 = 1;

if (value < 68.2 && value > 34.1)
value2 = 2;
.
.
.
if (value < 1023 && value > 988.9)
value2 = 30;

…but this take a lot of space, any shortest possibilities ?

``````value = analogRead(iPotentiometer);
if (value < 34.1 && value > 0)
``````

analogRead returns only integer values.
Tell us what you are trying to do.
Have you thought of using “map”?

Why make life hard for you and your arduino by doing the divide by 30. This means that everything that follows is using floats. Stick to ints and scale up your compare values instead. To shorten the code put the compare values into an array and use a for loop to step through the array looking for the right window.

You might want to look at the map() function. Map the range 0 to 1024 to 0 to 31 to get values between 0 and 30. Map from 0 to 1024 to 0 to 30, and add 1, to get values in the range 1 to 30.

i’m tryining to read input value from potentiometer and whole scale 1023 is divided by 30, so I have to be able to control output based on potentiometer value (thirty different step for output)), so do I need do thirty if loop really ?

so do I need do thirty if loop really ?

No … Read my reply.

Or just use the output of the map function without the use of any if statements at all.

saratusa: i'm tryining to read input value from potentiometer and whole scale 1023 is divided by 30, so I have to be able to control output based on potentiometer value (thirty different step for output)), so do I need do thirty if loop really ?

No, you need to use the map() function, as others have already posted. It is there for -exactly- what you are trying to do.

http://arduino.cc/en/Reference/Map

map(value, fromLow, fromHigh, toLow, toHigh) Description

Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.