17.05.2016, 12:47
Here's a function you can use to calculate the average, this version will keep last count values and will returnt the average for every insert:
And here's how you can use it:
You can add an extra counter in your resident script if you want to send a new value for each X samples.
Code:
function avg(store, count, value)
local sum = 0
-- add new value as first (newest) store element
table.insert(store, 1, value)
-- remove old elements, until store size is correct
while #store > count do
table.remove(store)
end
-- calucate value sum
for _, value in ipairs(store) do
sum = sum + value
end
-- get average value
return sum / #store
end
And here's how you can use it:
Code:
if not store then
require('ow')
-- load avg function here
store = {}
end
value = ow.readfile('28.690E0A070000', 'temperature', true)
avgvalue = avg(store, 5, value)
You can add an extra counter in your resident script if you want to send a new value for each X samples.