I'm getting input from 0
to 360
from my sensor. But I want to map things so 340
degrees equals 0
and 20
degrees equals 1
.
I've tried doing things like using the map
function
float myInput = readData();
float myVar = map(myInput, 0, 360, 0, 1);
But that just makes 340
higher than 20
. I think the fact that it goes from 340
to 0
and then to 20
is what's complicating things. Is there a way to map a custom portion of 360
degrees from 0
to 1
when my lower point would have a higher number than the higher point?
You could convert your input to a different range first. Something like this:
if(myInput > 180) {
myInput = myInput - 360;
}
That will convert values like 340
to -20
. So now your input range is -20
to 20
and you can use the map
function:
float mappedInput = map(myInput, -20, 20, 0, 1);
You haven't said what should happen if your input is outside that range, so you might have to think harder about those cases, but the general approach should work.