perkee's blog

I heard you like science. tags
Mar 11
Permalink

flip() for Arduinos, or really any C-esque language

I just helped a friend with a digital art project. She is a great artist and a great programmer (double major in Visual Art and Computer Science) so she knows what she’s doing. What she needed was to borrow my soldering iron and expertise, both of which I love lending out, especially when I get cookies in return. Anyways, her project has 6 potentiometers functioning as voltage dividers. They have one leg on the Arduino’s +5V pin, the other on Ground, and each has a wiper going to an Analog in pin. The input is read in as follows:

v0 = analogRead(0);
v0 = map(v0, 0, 1023, 0, 255);
Serial.print(v0);

After this, a bunch of reading happens in processing that manipulates video frames, so the knob position plays back a short clip. Pretty cool, eh. Problem is that we might have soldered them in backwards, such that turning to the right lowers the voltage. Not a problem. May I present flip():

int flip(int value, int from, int to)
{
    return to-value+from;
}

int flip(int value, int to)
{
    return flip(value, 0, to);
}

int flip(byte value)
{
   return (byte) flip(value, 255);
}

Note that the operation order in the first one is important; you might have weird overflows, etc. if you add before subtracting.

Comments (View)
blog comments powered by Disqus