{"id":2315,"date":"2022-02-26T21:42:20","date_gmt":"2022-02-27T02:42:20","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2315"},"modified":"2022-02-26T21:42:20","modified_gmt":"2022-02-27T02:42:20","slug":"assignment-6-update","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2315","title":{"rendered":"Assignment 6 Update"},"content":{"rendered":"<p>With the wires back on the fan motor, I confirmed that code works in two ways: 1) when the humidity sensor detects that the average humidity is less than 50% the fan turns on, and 2) when the average humidity is less than 50% the fan turns off. Next, with condition 2 as the base conditions, I needed to test if the fan will turn on when the humidity sensor detects that the average humidity has increased to above 50%.<\/p>\n<p>With my first version of the code, the fan was not responding the way I wanted to the humidity sensor readings.<\/p>\n<p>This second version of the code using millis() was successful:<\/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\nDHT dht(DHTPIN, DHTTYPE);  \/\/dht library\r\nint chk;\r\nint humid; \/\/stores humidity\r\n\r\n\r\n\/\/data smoothing\r\nconst int numReadings = 5;\r\nint humReadings [numReadings];\r\nint humReadIndex = 0;\r\nint humTotal = 0;\r\nint humAverage = 0;\r\nint inputPin = 2; \/\/put dht 222 sensor pin here\r\n\/\/detecting sensor change\r\nint last;\r\n\/\/response to sensor change\r\n\r\n\r\n\/\/for fan and motor\r\nint speedPin = 5;\r\nint dir1 = 4;\r\nint dir2 = 3;\r\nint mSpeed = 0;\r\nunsigned long lastReadTime = 0;\r\n\r\nvoid setup() {\r\n  \/\/ put your setup code here, to run once:\r\n  \/\/pins for fan and motot\r\n  pinMode(speedPin, OUTPUT);\r\n  pinMode(dir1, OUTPUT);\r\n  pinMode(dir2, OUTPUT);\r\n\r\n\r\n  \/\/dht sensor\r\n  dht.begin();\r\n\r\n\/\/data smoothing for humidity\r\n  for (int thisReading = 0; thisReading &lt; numReadings; thisReading++) {\r\n    \/\/tempReadings[thisReading] = 0;\r\n    humReadings[thisReading] = 0;\r\n  }\r\n  Serial.begin(9600);\r\n\r\n  lastReadTime = millis();\r\n}\r\n\r\nvoid loop() {\r\n  \/\/delay(1000);\r\n\r\n  if ((millis() - lastReadTime) &gt;= 10000UL) {\r\n    \/\/Serial.println(lastReadTime); \/\/used to check millis \r\n    \/\/Serial.println(millis());\/\/used to check millis\r\n    lastReadTime = millis();\r\n    humid = dht.readHumidity();\r\n    Serial.println(humid); \/\/prints the current sensor reading\r\n    humTotal = humTotal - humReadings[humReadIndex];\r\n    humReadings[humReadIndex] = humid;\r\n    humTotal = humTotal + humReadings [humReadIndex];\r\n    humReadIndex = humReadIndex + 1;\r\n    if (humReadIndex &gt;= numReadings) {\r\n      humReadIndex = 0;\r\n    }\r\n    humAverage = humTotal \/ numReadings;\r\n    Serial.println(humAverage);\r\n\r\n  }\r\n\r\n\r\n  if (humAverage &gt; 50)\r\n  {\r\n    digitalWrite(dir1, HIGH);\r\n    digitalWrite(dir2, LOW);\r\n    analogWrite(speedPin, 225);\r\n  }else\r\n  {\r\n    digitalWrite(dir1,HIGH);\r\n    digitalWrite(dir2,LOW);\r\n    analogWrite(speedPin, 0);\r\n  }\r\n}<\/pre>\n<p>I started in a room with humidity readings less than 50 (no fan motion). I turned on hot water in the bathroom, and after a few minutes, moved the sensor and fan into that room. As the sensor&#8217;s humidity reading rose, so did the average humidity reading. Once the average humidity reading rose above 50, the fan began to turn. Then I moved the sensor and fan back into the original room, and waited for the average humidity to fall. When the average humidity fell below 50 again, the fan motor turned off.<\/p>\n<p>Note on power and troubleshooting:<\/p>\n<p>I originally had both the fan and the humidity sensor connected to the rail. This worked the first time I tried it, but the sensor began to send back 0 readings after a few test runs. I had to connect the DHT22 sensor to the arduino and the fan to the rail (with 5V power supply connected to 9V battery) for both the sensor and the fan to work properly. My clue was that the LED light on the power supply started bright and dimmed as the code started running.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the wires back on the fan motor, I confirmed that code works in two ways: 1) when the humidity sensor detects that the average humidity is less than 50% the fan turns on, and 2) when the average humidity is less than 50% the fan turns off. Next, with condition 2 as the base &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2315\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Assignment 6 Update&#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":[15],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2315"}],"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=2315"}],"version-history":[{"count":2,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2315\/revisions"}],"predecessor-version":[{"id":2324,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2315\/revisions\/2324"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}