{"id":2249,"date":"2022-02-15T11:50:03","date_gmt":"2022-02-15T16:50:03","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2249"},"modified":"2022-02-15T11:51:18","modified_gmt":"2022-02-15T16:51:18","slug":"assignment-5-smoothing-sensor-data","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2249","title":{"rendered":"Assignment 5: Smoothing Sensor Data"},"content":{"rendered":"<p>For assignment 5, I sampled temperature and humidity data points from the DHT22 sensor to create average temperature and humidity values. I used the serial monitor within Arduino IDE to follow changes in values and added characters to distinguish visually between temperature, temperature average, humidity, and humidity averages within the serial monitor.<\/p>\n<p>Then I used the values from the average temperature (with an average of 10 data points) to calculate a temperature change over a period of values.\u00a0 If the temperature change was greater than 3 degrees within the specified sampling period, the &#8216;alarm&#8217; (an LED light) would turn on. It would turn off again when the temperature change fell below 3 degrees.<\/p>\n<p>The limitations of this method are that slow and steady change in one direction to an average may not be detected. The data smoothing is desirable to avoid &#8216;blips&#8217; from occurring, but it may be preferable for environmental alarms (temperature, smoke, etc) to compare their average sampling to a constant value that reflects the preferred conditions &#8211; not only a rapid change in values. As observed in the paragraph above, when using this technique, the &#8216;alarm&#8217; turns off if the temperature change falls back below the threshold of acceptable change. This may or may not be desirable, depending on the response needed.<\/p>\n<p>Final Code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;DHT.h&gt;\r\n#include &lt;DHT_U.h&gt;\r\n#define DHTPIN 2        \/\/ sensor DHT22 data connected to Arduino pin 2\r\n#define DHTTYPE DHT22  \/\/ using sensor DHT22\r\n\r\n\r\nDHT dht(DHTPIN, DHTTYPE);  \/\/dht library \r\nint chk;\r\nint humid; \/\/stores humidity\r\nint tempC; \/\/stores temperature\r\nint tempF; \/\/stores temp in F\r\n\r\n\r\n\/\/data smoothing\r\n\/\/based on https:\/\/docs.arduino.cc\/built-in-examples\/analog\/Smoothing \r\n\/\/include dht library \r\n\/\/note this tutorial is for analog sensors not digital like the dht22\r\n\r\nconst int numReadings = 10;\r\nint tempReadings [numReadings];\r\nint tempReadIndex = 0;\r\nint tempTotal = 0;\r\nint tempAverage = 0; \r\n\r\n\r\nint humReadings [numReadings];\r\nint humReadIndex=0;\r\nint humTotal = 0;\r\nint humAverage = 0; \r\n\r\n\r\nint inputPin= 2; \/\/put sensor pin here\r\n\r\n\/\/const char tNow=\"Temperature: \";\r\n\/\/const char tAvg=\"Average Temperature: \"; \r\n\r\n\/\/4 lines below only print T, A, H, and v\r\nconst char*tempNow=\"Temperature: \";\r\nconst char*tempAvg=\"Average Temperature: \";\r\nconst char *huNow=\"Humidity: \";\r\nconst char *huAvg=\"verage Humidity: \";\r\n\r\n\/\/detecting sensor change\r\nint last; \r\n\r\n\/\/response to sensor change\r\nint blueled=10;\r\n\r\nvoid setup() {\r\n\r\n  \/\/ put your setup code here, to run once:\r\n  Serial.begin(9600);\r\n  dht.begin(); \r\n\r\n  \/\/for response to sensor change\r\n  pinMode(blueled,OUTPUT);\r\n\r\n  for (int thisReading = 0; thisReading &lt; numReadings; thisReading++) {\r\n    tempReadings[thisReading]= 0;\r\n    humReadings[thisReading]=0;\r\n    \/\/placing humreadings below temp readings does not appear to work\r\n    \/\/humReadings[thisReading]=0;\r\n  \/\/for (int hThisReading = 0; hThisReading &lt; numReadings; hThisReading++) {\r\n    \/\/humReadings[hThisReading]= 0;\r\n\r\n  }\r\n\r\n}\r\n\r\nvoid loop() {\r\n  \/\/ put your main code here, to run repeatedly:\r\n\r\nhumid=dht.readHumidity();\r\ntempC=dht.readTemperature();\r\n\/\/tempF=(tempC*1.8)+32;\r\n\/\/Serial.write(tempC);\r\ndelay(2000);\r\n\/\/delay needed for p5js to recieve 2 values separately\r\n\/\/Serial.write(humid);\r\n\/\/delay(1000);\r\n\r\n  tempTotal = tempTotal - tempReadings[tempReadIndex];\r\n  \/\/need 2 reads start with one use temp\r\n  tempReadings[tempReadIndex] = tempC;\r\n  tempTotal = tempTotal + tempReadings [tempReadIndex];\r\n  tempReadIndex = tempReadIndex + 1; \r\n\r\n  if (tempReadIndex &gt;= numReadings) {\r\n    tempReadIndex = 0; \r\n   \r\n  }\r\n\r\n  tempAverage = tempTotal \/ numReadings;\r\n\r\n  humTotal = humTotal - humReadings[humReadIndex];\r\n  humReadings[humReadIndex] = humid;\r\n  humTotal = humTotal + humReadings [humReadIndex];\r\n  humReadIndex = humReadIndex + 1;\r\n\r\n\r\n  if (humReadIndex &gt;= numReadings) {\r\n    humReadIndex=0;\r\n  }\r\n\r\n  humAverage = humTotal \/ numReadings;\r\n\r\n\r\n\/\/printing to serial monitor\r\n\r\n  \/\/const char* pNowTemp = tNow + tempC;\r\n  \/\/const char* pAvgTemp = tAvg + tempAverage;\r\n  \/\/setDisplayToString(pNowTemp);\r\n  \/\/tempAverage.setDisplayToString(pAvgTemp);\r\n  \/\/Serial.print(f'Avg: ');\r\n  \r\n  \/\/one line below printed just A\r\n  Serial.print(*tempAvg);\r\n  Serial.println(tempAverage);\r\n \r\n  \/\/Serial.print(F('Average temperature {tempAverage}'));\r\n  \/\/Serial.print(F('Average temperature:'));\r\n  \/\/Serial.println(tempAverage);\r\n\r\n  \/\/Serial.println(pNowTemp);\r\n  delay(5);\r\n\r\n  \/\/two lines below printed just T \r\n  Serial.print(*tempNow);\r\n  Serial.println(tempC);\r\n\r\n  \/\/Serial.println(pAvgTemp);\r\n\r\n  delay(5);\r\n\r\n  Serial.print(*huAvg);\r\n  Serial.println(humAverage);\r\n\r\n  Serial.print(*huNow);\r\n  Serial.println(humid);\r\n\r\n  delay(5);\r\n\r\n\/\/measuring change in temperature average\r\n  int t = tempAverage;\r\n  int tdiff = t-last;\r\n  int thresh = 1;\r\n  Serial.println(tdiff);\r\n  \/\/this statement could be simplified, need to do it for all tdiff values \r\n  if(tdiff&gt;thresh||tdiff&lt;=thresh){\r\n    Serial.println(t);\r\n    last=t;\r\n\r\n\r\n\r\n\/\/led response to rate of change in temperature average\r\n if (tdiff&gt;=3)\r\n  {\r\n    digitalWrite(blueled,HIGH);\r\n\r\n  }\r\nif(tdiff&lt;3)\r\n  {\r\n  digitalWrite(blueled,LOW);\r\n }\r\n  }\r\n\r\n\r\n  \r\n  \r\n\r\n}<\/pre>\n<p>Process Notes:<\/p>\n<p>Identifying Change in Average Temperature Value<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-2252\" src=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/serial-monitor-and-code_notes_01-255x300.png\" alt=\"\" width=\"255\" height=\"300\" srcset=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/serial-monitor-and-code_notes_01-255x300.png 255w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/serial-monitor-and-code_notes_01-871x1024.png 871w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/serial-monitor-and-code_notes_01-768x903.png 768w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/serial-monitor-and-code_notes_01-1306x1536.png 1306w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/serial-monitor-and-code_notes_01-1200x1411.png 1200w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/serial-monitor-and-code_notes_01.png 1464w\" sizes=\"(max-width: 255px) 85vw, 255px\" \/><\/p>\n<p>Printing\u00a0Change in Average Temperature Value<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-2253\" src=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/tempsnip_02-235x300.png\" alt=\"\" width=\"235\" height=\"300\" srcset=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/tempsnip_02-235x300.png 235w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/tempsnip_02-803x1024.png 803w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/tempsnip_02-768x980.png 768w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/tempsnip_02-1204x1536.png 1204w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/tempsnip_02-1200x1531.png 1200w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/tempsnip_02.png 1269w\" sizes=\"(max-width: 235px) 85vw, 235px\" \/><\/p>\n<p>LED on\/off with Changes in Average Temperature Value<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-2254\" src=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_03-298x300.png\" alt=\"\" width=\"298\" height=\"300\" srcset=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_03-298x300.png 298w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_03-1016x1024.png 1016w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_03-150x150.png 150w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_03-768x774.png 768w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_03-1524x1536.png 1524w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_03-1200x1209.png 1200w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_03.png 1698w\" sizes=\"(max-width: 298px) 85vw, 298px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>LED doesn&#8217;t turn off when Change in Average Temperature = 0<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-2255\" src=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_LED-troubleshooting_05-258x300.png\" alt=\"\" width=\"258\" height=\"300\" srcset=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_LED-troubleshooting_05-258x300.png 258w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_LED-troubleshooting_05-881x1024.png 881w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_LED-troubleshooting_05-768x893.png 768w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_LED-troubleshooting_05-1321x1536.png 1321w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_LED-troubleshooting_05-1200x1395.png 1200w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data-smoothing-notes_changes-in-average-temp-over-time_LED-troubleshooting_05.png 1554w\" sizes=\"(max-width: 258px) 85vw, 258px\" \/><\/p>\n<p>Solution: Turning Off LED when Change = 0<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-2256\" src=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data_smoothing_final_07_solved-285x300.png\" alt=\"\" width=\"285\" height=\"300\" srcset=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data_smoothing_final_07_solved-285x300.png 285w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data_smoothing_final_07_solved-974x1024.png 974w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data_smoothing_final_07_solved-768x807.png 768w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data_smoothing_final_07_solved-1462x1536.png 1462w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data_smoothing_final_07_solved-1200x1261.png 1200w, https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/wp-content\/uploads\/2022\/02\/data_smoothing_final_07_solved.png 1634w\" sizes=\"(max-width: 285px) 85vw, 285px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For assignment 5, I sampled temperature and humidity data points from the DHT22 sensor to create average temperature and humidity values. I used the serial monitor within Arduino IDE to follow changes in values and added characters to distinguish visually between temperature, temperature average, humidity, and humidity averages within the serial monitor. Then I used &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2249\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Assignment 5: Smoothing Sensor Data&#8221;<\/span><\/a><\/p>\n","protected":false},"author":28,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2249"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2249"}],"version-history":[{"count":4,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2249\/revisions"}],"predecessor-version":[{"id":2259,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2249\/revisions\/2259"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}