This commit is contained in:
Lukian 2024-10-22 23:25:38 +02:00
parent 84036b5d43
commit 068d3a6f70

View file

@ -1,3 +1,5 @@
-- écrit par Lukian Leizour le 22/10/2024
function print_temp() function print_temp()
s = sensor.attach("DHT22", pio.GPIO18) s = sensor.attach("DHT22", pio.GPIO18)
tmr.delayms(500) tmr.delayms(500)
@ -20,3 +22,56 @@ function humi_neo(min, max)
tmr.delayms(500) tmr.delayms(500)
end end
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