From 07031f569ab75fb29c1c3bc1412442b6a55236f2 Mon Sep 17 00:00:00 2001 From: Felix Knecht Date: Tue, 17 Jan 2017 21:52:45 +0100 Subject: [PATCH] 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 --- docs/en/modules/bme280.md | 42 ++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/docs/en/modules/bme280.md b/docs/en/modules/bme280.md index 925369b0..212139b0 100644 --- a/docs/en/modules/bme280.md +++ b/docs/en/modules/bme280.md @@ -137,15 +137,27 @@ QNH = bme280.qfe2qnh(P, alt) print(string.format("QNH=%d.%03d", QNH/1000, QNH%1000)) 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)) 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 P = bme280.baro() 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 @@ -155,17 +167,29 @@ alt=320 -- altitude of the measurement place bme280.init(3, 4) 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("QNH=%d.%03d", QNH/1000, QNH%1000)) print(string.format("humidity=%d.%03d%%", H/1000, H%1000)) 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 P = bme280.baro() 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. @@ -175,7 +199,11 @@ Example of readout in forced mode (asynchronous) bme280.init(3, 4, nil, nil, nil, 0) -- initialize to sleep mode bme280.startreadout(0, function () 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) ```