Math problem
Moskog
Posts: 554
Hello
I have this little math problem:
I want to make a NIB out of numbers from 27 and up to 228.
27 should be the same as 0 and 228 should be 15.
I have been studying the scaling description in What's a microcontroller, around page 155, but having a hard time figuring out how to solve my own problem.
Background information to the problem:
I have this MLX90316 mag. rotation sensor that outputs an analog voltage from 0,5 to 4,5 volts depending on the angel of the magnet. Through a AD-converter I get the values 27 to 228 from those voltages as the magnet rotates one revolution.
The resolution of one revolution does not have to be more then 16 levels, thats why I only need a NIB-variable.
The whole thing is for detecting wind direction!
KjellO
·
I have this little math problem:
I want to make a NIB out of numbers from 27 and up to 228.
27 should be the same as 0 and 228 should be 15.
I have been studying the scaling description in What's a microcontroller, around page 155, but having a hard time figuring out how to solve my own problem.
Background information to the problem:
I have this MLX90316 mag. rotation sensor that outputs an analog voltage from 0,5 to 4,5 volts depending on the angel of the magnet. Through a AD-converter I get the values 27 to 228 from those voltages as the magnet rotates one revolution.
The resolution of one revolution does not have to be more then 16 levels, thats why I only need a NIB-variable.
The whole thing is for detecting wind direction!
KjellO
·
Comments
In your case, the sensor would be the X and the nibble's range the Y.
If you calculate the nibble's value each time, be sure to pay attention to the fact of integer math on the Stamp. You might wish to multiply the D by 100 and then divide the result of the multiply & divide by 100 before adding the C.
If you want to use the Stamp's operators for multiplying by a fraction, take a look at Tracy Allen's page (http://www.emesystems.com/BS2math1.htm) that describes them in the context of scaling the result from a temperature sensor.
Why don't you work out the math in a spreadsheet for the range of your sensor's input. You will start to see a pattern in the computed result.
Once you have done the spreadsheet analysis of the scaling, you might then decide to instead use a LOOKDOWN function to fit the sensor's input into your nibble.
Daniel
A LOOKDOWN statement is another way to handle this if you want a non-linear conversion.
a = (x - 27) / 13
With the stamp's integer math this should truncate to 0 <= a <= 15.
Or you could use the /* to divide by a more accurate scaler... $0D55 I think it would be?
Not only do you want 201-->15, but also any number in the range {189--201}-->15. You can lay it out as
{0--12}-->0
{13--25}-->1
{26--37}-->2
...
{189--201}-->15
The best divisor is 202/16 = 12.625. The easiest way to deal with the fraction is to multiply everything times 8, so 12.625 * 8 becomes 101:
Another refinement with azimuth is that you might want 0 to be centered around North, say, for example
{196--0--6} --> 0
{7--18} --> 1
etc. and in that case you can do an offset in software.
That kind of offset in software may also be necessary if you for some reason you need to correct the direction reading after the wind vane is installed.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
KjellO