Also, if you are feeling ambitious, you could try to solve this problem in the analog domain, with something like this:
Then just analogRead() and Bob's your uncle. This approach would probably be non-trivial to actually implement, for the average Arduino hobbyist, but I did want to point out that it's available.