This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

1-wire DS18B20 change resolution
#9
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:
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.
Reply


Messages In This Thread
RE: 1-wire DS18B20 change resolution - by admin - 17.05.2016, 12:47

Forum Jump: