17.05.2016, 11:43
(This post was last modified: 17.05.2016, 11:46 by gjniewenhuijse.)
something like this?
resident script every 60 seconds:
user_library nit_measurement:
resident script every 60 seconds:
Code:
-- better to save the last 5 readings and avg them and then write to bus
require('ow')
require('user.nit_measurements')
-- second argument temperature10 (10-bit value)
-- third argument will force a new conversion
readValue = ow.readfile('28.690E0A070000', 'temperature', true)
-- measure last 5 readings
mValue = measValue (5, readValue)
log (round(mValue,1))
user_library nit_measurement:
Code:
avgMax = 5
function measValue (iMax,iValue)
-- init value array
if not measCount then
if iMax then
avgMax = iMax
end
measCount = 0
measValues = {}
for i=1,avgMax do
measValues[i] = iValue
end
end
measCount = measCount + 1
-- set values
for i=avgMax,2,-1 do
measValues[i] = measValues[i-1]
end
measValues[1] = iValue
--log(measValues)
-- reset counter
if (measCount == avgMax) then
measCount = 0
end
rValue = 0
for i=1,avgMax do
rValue = rValue + measValues[i]
end
rValue = rValue / avgMax
return rValue
end