Order on gauge Degrees from website Desired PWM output
S 180 0
W 270 256
N 0 512
E 90 767
S 180 1023
This is very confusing.
180 degrees from the website is supposed to produce a desired PWM output of 0. No, wait, it shows up again with an output of 1023.
90 degrees from 180 to 270 produces an output that varies by 256 from 0 to 256. 90 degrees from 90 to 180 produces an output that varies by 256 from 767 to 1023. BUT: 90 degrees from 0 to 90 produces an output that varies only by 255 from 512 to 767.
How can anyone work with a specification like that?!
Some 'if' statements and the map(...) function could handle this, but the requirements are needed FIRST.
Cool! Especially since the first post said nothing about 360, 45, 135, 225, or 315 degrees. I still do not see anything about the Desired PWM Output, though.
Well they are rotated 90 degree counter clockwise than normal angle and they count in opposite direction...
So if sensor says x, then bring it back to normal with (360-x) and then rotate it by 90 deg
=> (360-x)+90 = 450-x
If x is less than 90 then you are above 360, which does not create a pb for sinus or cosinus calculations, but in your case if you want to fit always between 0 and 360 then just take the modulo by 360 - assuming you are working with int.
So your angle on a traditional angle representation is ((450-x)%360)
I think you need some hysteresis / dead-band around south (if that's where the branch-cut is going to be),
so that you can stray past south a small amount without constantly moving the needle all the way back and
forth. Perhaps have the range so SE-S-SW-W-NW-N-NE-E-SE-S-SW, so there's quite a definite overlap.
The alternative is use some sort of stepper-motor guage that does 360 rotation... Assuming you
can find one without an internal stop.
So far, so good.
(Note: I edited the formatting to make it easier to read.)
Here is the next step:
// convert "0 to 360" scale to "0 to 1023"
int output_pwm = (wind_degrees * 1023L) / 360;
The reason I wrote 1023L instead of 1023 is to force the use of long arithmetic.
wind_degrees can go as high as 359. Multiplying 359 by 1023 will get you 367257. An int cannot handle numbers this high, but a long can.
After the division by 360, the result will again fit in an int, which is why I am using an int variable to store it.
Note that if the wind direction changes , say, from 175 degrees (i.e. slightly East of South) to 185 degrees (i.e. slightly West of South), that the needle will swing wildly. As long as you are not reading the wind direction too frequently, this should not be a problem. But if you are reading the wind direction, say, once every second or two, then it might be a problem.