# how to map pitch to a linear function?

I am trying to take a sqaure wave audio signal from a synthesizer into my arduino and map the wavelength to the hue of a LED. I am doing this by calculating the duration of the pulsein from pin 7 (aka "sqaurewave)::

`````` duration = pulseIn(squarewave, HIGH);
huemap = map(duration, 200, 17000, 0, 359);
H2R_HSBtoRGB(huemap, 99, brightonoff, colors);
``````

**H2R_HSBtoRGB() is a cool HSB to RGB library that makes it easy to do colors on RGB leds
GitHub - julioterra/HSB_Color: Arduino library that converts HSB values into RGB. Enables you to determine the values for RGB leds from hue, saturation and brightness values.. **

for the highest note on the keyboard, i read 200 for the duration
for the lowest note on the keyboard i read 17000 for the duration
the problem is that the pitch wavelength vs octave function isn’t linear, and therefore when i go down one octave, duration reads 400, when i go down two octaves duration reads 800, when i go down 3 octaves, duration reads 1600 etc…

essentially, i want some way to map the duration from 200 to 17000 in a linear fashion so i have an even hue shift between all notes (right now i am getting basically all red for 3 octaves…)

anyone have any idea on a function i could call do this??

let me know if this doesnt make any sense or needs clarification!

Take the Log of the value. Doesn't matter what base: 2, 10, e... Then you can map that range to the range of hues.

how do you take the log of the value in arduino code?

float a = log(value);