Setting a min/max value on an analog sensor?

Hey guys,

Got a potentiometer on the shield I'm using for a CompSci practical, which by default has the range 0-1023. However I need a range of 20-500.

How do I go about doing this?

Thanks,

Harry

ThatAdamsGuy:
Hey guys,

Got a potentiometer on the shield I'm using for a CompSci practical, which by default has the range 0-1023. However I need a range of 20-500.
How do I go about doing this?
Thanks,
Harry

To scale the value:-

val=map(potVal,0,1023,20,500);

(You can use the same variable for the result as the one used to read the pot if you want, too. I just used 'val' and 'potVal' to make it clearer.)

Edit: Or if you want to ignore values below 20 and above 500, use:-

val=constrain(potVal,20,500);

OldSteve:
To scale the value:-

val=map(potVal,0,1023,20,500);

(You can use the same variable for the result as the one used to read the pot if you want, too. I just used 'val' and 'potVal' to make it clearer.)

Edit: Or if you want to ignore values below 20 and above 500, use:-

val=constrain(potVal,20,500);

Hey, thanks for the response. I'm currently using

timeUnit = 10 + analogRead(POTENTIOMETER)

So instead would I want

timeUnit = map(analogRead(POTENTIOMETER), 0, 1023, 20, 500);

?

Thanks

ThatAdamsGuy:
Hey, thanks for the response. I'm currently using

timeUnit = 10 + analogRead(POTENTIOMETER)

So instead would I want timeUnit = map(analogRead(POTENTIOMETER), 0, 1023, 20, 500);[/code]

?

Thanks

Yep. That scales the pot's 0-1023 to 20-500, whereas previously you would have been getting 10-1033.

This is linear mapping, which works for a linear potmeter.
If the potmeter is logarithmic you need another mapping function.

or you want to have something special you could use any transform function

timeUnit = convert(analogRead(POTENTIOMETER));

and the convert function is something like

int convert(int x)
{
return 255 + 255 * sin(x * 2 * PI / 1023.0);
}

int convert(int x)
{
if (x < 100) return 20;
if (x < 300) return 120;
if (x < 500) return 220;
if (x < 700) return 320;
return 500;
}

int convert(int x)
{
return 20 + random(min(x, 480));
}

or
...