{"id":116,"date":"2014-03-11T20:45:36","date_gmt":"2014-03-11T20:45:36","guid":{"rendered":"http:\/\/www.flat-planet.net\/?p=116"},"modified":"2014-03-12T22:58:50","modified_gmt":"2014-03-12T22:58:50","slug":"sensitive-sensors","status":"publish","type":"post","link":"https:\/\/flat-planet.net\/?p=116","title":{"rendered":"Sensitive Sensors"},"content":{"rendered":"<p>As I finally got my Arduino reading the temperature sensor, and sending the values to <a title=\"Sensor Data\" href=\"http:\/\/www.flat-planet.net\/?page_id=110\">this page<\/a> consistently I noticed that the values were quite &#8216;jittery&#8217; &#8211; the issue being that I was basically grabbing a single read from the sensor every 60 seconds, and then firing it on. Since this was a single read any issues affecting the sensor at the instant of the read could throw off the value significantly.<\/p>\n<p><a href=\"http:\/\/www.flat-planet.net\/wp-content\/uploads\/2014\/03\/temperature_jitter.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-117\" alt=\"temperature_jitter\" src=\"http:\/\/www.flat-planet.net\/wp-content\/uploads\/2014\/03\/temperature_jitter.jpg\" width=\"489\" height=\"259\" \/><\/a>As you can see in the above chart we can have massive changes in the read temperature without any major changes (that I could tell) in the the actual physical environment &#8211; anything between 5 and 20 degrees between samples.<\/p>\n<p>I decided that a better approach would be to average the sensor values over a time and use that as the value sent to the server. The code below shows how I&#8217;ve done that:<\/p>\n<pre><span style=\"color: #cc6600;\">float<\/span> readTempAvg(){\r\n\u00a0\u00a0<span style=\"color: #cc6600;\">static<\/span> <span style=\"color: #cc6600;\">float<\/span> buff[12];\r\n\u00a0\u00a0<span style=\"color: #cc6600;\">static<\/span> <span style=\"color: #cc6600;\">int<\/span> index = 0;\r\n\u00a0\u00a0<span style=\"color: #cc6600;\">static<\/span> <span style=\"color: #cc6600;\">float<\/span> sum = 0;\r\n\u00a0\u00a0<span style=\"color: #cc6600;\">static<\/span> <span style=\"color: #cc6600;\">int<\/span> count = 0;\r\n\r\n\u00a0\u00a0sum\u00a0-=\u00a0buff[index];\r\n\u00a0\u00a0buff[index]\u00a0=\u00a0(<span style=\"color: #cc6600;\">analogRead<\/span>(A0) \/ 9.31);\r\n\u00a0\u00a0sum\u00a0+=\u00a0buff[index];\r\n\r\n\u00a0\u00a0index\u00a0=\u00a0(\u00a0(index\u00a0+\u00a01)\u00a0%\u00a012\u00a0);\r\n\r\n\u00a0\u00a0<span style=\"color: #cc6600;\">if<\/span>( count &lt; 12 ){\r\n\u00a0\u00a0\u00a0\u00a0count++;\r\n\u00a0\u00a0}\r\n\r\n\u00a0\u00a0<span style=\"color: #cc6600;\">return<\/span>( sum \/ count);\r\n}<\/pre>\n<p>The <code>sum-=buff[index]<\/code> has the value that is about to be overwritten in the buffer removed from the sum. This means that <code>sum<\/code> is always kept up to date. Small improvement to this code would be to define a buffer size at the start and replace all the &#8216;magic&#8217; const <em><strong>12<\/strong><\/em> that is in the code.<\/p>\n<p>Following this change I&#8217;m getting much smoother temperature readings &#8211; generally +\/-1 degree or less every 60 seconds.<\/p>\n<p><a href=\"http:\/\/www.flat-planet.net\/wp-content\/uploads\/2014\/03\/temperature_newAlgorithm.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-120\" alt=\"temperature_newAlgorithm\" src=\"http:\/\/www.flat-planet.net\/wp-content\/uploads\/2014\/03\/temperature_newAlgorithm.jpg\" width=\"700\" height=\"341\" \/><\/a>Next step is to try and sort out the Ethernet Shield crashes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I finally got my Arduino reading the temperature sensor, and sending the values to this page consistently I noticed that the values were quite &#8216;jittery&#8217; &#8211; the issue being that I was basically grabbing a single read from the sensor every 60 seconds, and then firing it on. Since this was a single read [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,27],"tags":[30,51,29,33,50],"series":[],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-arduino","category-projects","tag-arduino-2","tag-graphing","tag-programming","tag-projects-2","tag-sensors"],"_links":{"self":[{"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flat-planet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":4,"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":123,"href":"https:\/\/flat-planet.net\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions\/123"}],"wp:attachment":[{"href":"https:\/\/flat-planet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flat-planet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flat-planet.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/flat-planet.net\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}