Because MakeCode has a problem calculating the power function, the altitude calculation result of the pressure sensor is always 0. Today, the MakeCode development team said that the power function requires 4K of program space and the chip space is insufficient, so it cannot be used ( https://github.com/microsoft/pxt-microbit/issues/2192#issuecomment-515120256 ). So I wonder if there is another way to calculate.
The formula for calculating height is as follows:
altitude = ((pow((101325 / pressure(Pa), 1/5.257) - 1.0) * (temperature(C) + 273.15)) / 0.0065
Because the atmospheric pressure ranges from 860-1060hPa, the power function part can be regarded as (1+x)^N, where x is close to 0, so the power function can be approximated using the extreme number method.
function apow(x: number, n: number): number {
let d = x - 1
return 1 + (n * d) + (n * (n - 1) * d * d) / 2
}
I made a simple comparison using this method and found that the error of the calculated height is very small. Please refer to the complete program:
https://github.com/makecode-extensions/LPS22
This content is originally created by dcexpert , a user of EEWORLD forum. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source