Correct bme280 code samples for negative values (#1735)

* Corrected examples for negative values

See issue #1734
Negative values need to be treated differently. Changed all the samples to reflect that

* Update bme280.md
This commit is contained in:
Felix Knecht 2017-01-17 21:52:45 +01:00 committed by Marcel Stör
parent 0d572eedca
commit 07031f569a
1 changed files with 35 additions and 7 deletions

View File

@ -137,15 +137,27 @@ QNH = bme280.qfe2qnh(P, alt)
print(string.format("QNH=%d.%03d", QNH/1000, QNH%1000)) print(string.format("QNH=%d.%03d", QNH/1000, QNH%1000))
H, T = bme280.humi() H, T = bme280.humi()
print(string.format("T=%d.%02d", T/100, T%100)) if T<0 then
print(string.format("T=-%d.%02d", -T/100, -T%100))
else
print(string.format("T=%d.%02d", T/100, T%100))
end
print(string.format("humidity=%d.%03d%%", H/1000, H%1000)) print(string.format("humidity=%d.%03d%%", H/1000, H%1000))
D = bme280.dewpoint(H, T) D = bme280.dewpoint(H, T)
print(string.format("dew_point=%d.%02d", D/100, D%100)) if D<0 then
print(string.format("dew_point=-%d.%02d", -D/100, -D%100))
else
print(string.format("dew_point=%d.%02d", D/100, D%100))
end
-- altimeter function - calculate altitude based on current sea level pressure (QNH) and measure pressure -- altimeter function - calculate altitude based on current sea level pressure (QNH) and measure pressure
P = bme280.baro() P = bme280.baro()
curAlt = bme280.altitude(P, QNH) curAlt = bme280.altitude(P, QNH)
print(string.format("altitude=%d.%02d", curAlt/100, curAlt%100)) if curAlt<0 then
print(string.format("altitude=-%d.%02d", -curAlt/100, -curAlt%100))
else
print(string.format("altitude=%d.%02d", curAlt/100, curAlt%100))
end
``` ```
Or simpler and more efficient Or simpler and more efficient
@ -155,17 +167,29 @@ alt=320 -- altitude of the measurement place
bme280.init(3, 4) bme280.init(3, 4)
T, P, H, QNH = bme280.read(alt) T, P, H, QNH = bme280.read(alt)
print(string.format("T=%d.%02d", T/100, T%100)) if T<0 then
print(string.format("T=-%d.%02d", -T/100, -T%100))
else
print(string.format("T=%d.%02d", T/100, T%100))
end
print(string.format("QFE=%d.%03d", P/1000, P%1000)) print(string.format("QFE=%d.%03d", P/1000, P%1000))
print(string.format("QNH=%d.%03d", QNH/1000, QNH%1000)) print(string.format("QNH=%d.%03d", QNH/1000, QNH%1000))
print(string.format("humidity=%d.%03d%%", H/1000, H%1000)) print(string.format("humidity=%d.%03d%%", H/1000, H%1000))
D = bme280.dewpoint(H, T) D = bme280.dewpoint(H, T)
print(string.format("dew_point=%d.%02d", D/100, D%100)) if D<0 then
print(string.format("dew_point=-%d.%02d", -D/100, -D%100))
else
print(string.format("dew_point=%d.%02d", D/100, D%100))
end
-- altimeter function - calculate altitude based on current sea level pressure (QNH) and measure pressure -- altimeter function - calculate altitude based on current sea level pressure (QNH) and measure pressure
P = bme280.baro() P = bme280.baro()
curAlt = bme280.altitude(P, QNH) curAlt = bme280.altitude(P, QNH)
print(string.format("altitude=%d.%02d", curAlt/100, curAlt%100)) if curAlt<0 then
print(string.format("altitude=-%d.%02d", -curAlt/100, -curAlt%100))
else
print(string.format("altitude=%d.%02d", curAlt/100, curAlt%100))
end
``` ```
Use `bme280.init(sda, scl, 1, 3, 0, 3, 0, 4)` for "game mode" - Oversampling settings pressure ×4, temperature ×1, humidity ×0, sensor mode: normal mode, inactive duration = 0.5 ms, IIR filter settings filter coefficient 16. Use `bme280.init(sda, scl, 1, 3, 0, 3, 0, 4)` for "game mode" - Oversampling settings pressure ×4, temperature ×1, humidity ×0, sensor mode: normal mode, inactive duration = 0.5 ms, IIR filter settings filter coefficient 16.
@ -175,7 +199,11 @@ Example of readout in forced mode (asynchronous)
bme280.init(3, 4, nil, nil, nil, 0) -- initialize to sleep mode bme280.init(3, 4, nil, nil, nil, 0) -- initialize to sleep mode
bme280.startreadout(0, function () bme280.startreadout(0, function ()
T, P = bme280.read() T, P = bme280.read()
print(string.format("T=%d.%02d", T/100, T%100)) if T<0 then
print(string.format("T=-%d.%02d", -T/100, -T%100))
else
print(string.format("T=%d.%02d", T/100, T%100))
end
end) end)
``` ```