The Map Function

Here is a function that may be useful for many of the projects.

map(value, fromLow, fromHigh, toLow, toHigh)
http://arduino.cc/en/reference/map

Here is a diagram of how this function works

map1

The function takes a value that is in the range of fromHigh to fromLow.  It then remaps the value to toHigh to toLow.

One very nice applicatin of this technique is for remapping analog input to analog output.

void loop()
{
int val = analogRead(0);
val = map(val, 0, 1023, 0, 255);
analogWrite(9, val);
}
Another interesting aspect of this function is that it allows us to flip the lows and highs as shown in the diagram below.

map2This may be really useful for items such as the RGB LED that work by driving a signal low.