flip values

Hello there. I'm needing a little help here. This is something I forgot to do and hoping someone will understand what I mean.

I am playing with the arduino.cc/en/Tutorial/Tone2 example

At its current state the sensorReading is the hz played (more light = higher pitch) but I want to make it so that more light = a lower pitch.

I can't remember how to do it. Any help will be greatly appreciated. Thanks

subtract?

subtract?

Not really in this case.

At its current state the sensorReading is the hz played (more light = higher pitch) but I want to make it so that more light = a lower pitch.

Very easy, it's time to read up a little on the map function. You can use the map function to reverse the direction too, just make the 4th parameter (toLow) bigger then the 5th parameter (toHigh). In the sample change the line with the map call to:

``````int thisPitch = map(sensorReading, 400, 1000, [glow]1000[/glow], [glow]100[/glow]);
``````

That's all. Just move a one 0 to another place.

Korman

Not really in this case.

No, Groove was quite correct - subtraction would do the trick with the original code:`int thisPitch = 1100 - map(sensorReading, 400, 1000, 100, 1000);`
Or, more correct and complete:

``````int thisPitch = (HIGH_FREQ + LOW_FREQ) - map(sensorReading, 400, 1000, LOW_FREQ, HIGH_FREQ);
``````

Though yes, map approach is simpler, but the subtraction can be shorter and is a common inversion technique.

awesome. Thanks a lot everyone. I didn't know map could do that. I remember my professor telling us the subtraction method. Thanks again. Works like a charm.