From 068d3a6f7048e8a40ae8cae37056f08d4bcfb173 Mon Sep 17 00:00:00 2001 From: Lukian Date: Tue, 22 Oct 2024 23:25:38 +0200 Subject: [PATCH] commit --- dht22.lua | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/dht22.lua b/dht22.lua index 3d39f96..060a8d9 100644 --- a/dht22.lua +++ b/dht22.lua @@ -1,3 +1,5 @@ +-- écrit par Lukian Leizour le 22/10/2024 + function print_temp() s = sensor.attach("DHT22", pio.GPIO18) tmr.delayms(500) @@ -20,3 +22,56 @@ function humi_neo(min, max) tmr.delayms(500) end end + +function min(tab) + local min = tab[1] + for i=1,#tab do + if tab[i] < min then min = tab[i] end + end + return min +end + +function max(tab) + local max = tab[1] + for i=1,#tab do + if tab[i] > max then max = tab[i] end + end + return max +end + +function avg(tab) + local sum = 0 + for i=1,#tab do + sum = sum + tab[i] + end + return sum / #tab +end + +function temp_humi_avg() + temps = {} + humis = {} + s = sensor.attach("DHT22", pio.GPIO18) + device = pwm.attach(pio.GPIO2, 3, 0.5) + blinking = false + tmr.delayms(500) + while true do + temp = s:read("temperature") + humi = s:read("humidity") + if #temps == 10 then + for i=1,9 do + temps[i] = temps[i + 1] + humis[i] = humis[i + 1] + end + temps[10] = temp + humis[10] = humi + else + temps[#temps + 1] = temp + humis[#humis + 1] = humi + end + print("temp: "..temp..", min temp: "..min(temps)..", max temp: "..max(temps)..", avg temp: "..avg(temps)) + print("humi: "..humi..", min humi: "..min(humis)..", max humi: "..max(humis)..", avg humi: "..avg(humis).."\n") + if humi > avg(humis) and not blinking then device:start(); blinking = true + elseif humi <= avg(humis) and blinking then device:stop(); blinking = false end + tmr.delayms(10000) + end +end