{"id":2325,"date":"2022-03-01T19:52:32","date_gmt":"2022-03-02T00:52:32","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2325"},"modified":"2022-03-01T19:52:32","modified_gmt":"2022-03-02T00:52:32","slug":"crit1-development-experiment-with-fan-motor-and-light-sensor-temt6000","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2325","title":{"rendered":"Crit1 Development: Experiment with Fan Motor and Light Sensor TEMT6000"},"content":{"rendered":"<p>With the TEMT6000 light sensor, I can turn on a motor based on input to the light sensor, similar to the one I had with the DHT-22.<\/p>\n<p>With the TEMT6000, I used an analog write (instead of the digital write with the DHT-22) and the data arrives in float form. Several lines of code convert the reading into a percent. Data smoothing and millis () set up are the same as previous DHT-22 code.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#define lightPin A0 \/\/Ambient light sensor reading \r\n\r\n\/\/data smoothing\r\nconst int numReadings = 5;\r\nfloat ligReadings [numReadings];\r\nint ligReadIndex = 0;\r\nfloat ligTotal = 0;\r\nfloat ligAverage = 0;\r\nint inputPin = lightPin; \/\/put sensor pin here\r\n\/\/detecting sensor change\r\nint last;\r\n\/\/response to sensor change\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  pinMode(lightPin, INPUT);\r\n\r\n  for (int thisReading = 0; thisReading &lt; numReadings; thisReading++) {\r\n    ligReadings[thisReading] = 0;\r\n  }\r\n  Serial.begin(9600);\r\n  lastReadTime = millis();\r\n}\r\n\r\nvoid loop() {\r\n  \/\/delay(1000);\r\n\r\n  if ((millis() - lastReadTime) &gt;= 5000UL) {\r\n    Serial.println(lastReadTime);\r\n    Serial.println(millis());\r\n    lastReadTime = millis();\r\n    \/\/light sensor read\r\n    float light = analogRead(lightPin);\r\n    float light_ratio = light \/ 1023.0;\r\n    float light_percent = light_ratio * 100;\r\n\r\n    ligTotal = ligTotal - ligReadings[ligReadIndex];\r\n    ligReadings[ligReadIndex] = light_percent;\r\n    ligTotal = ligTotal + ligReadings [ligReadIndex];\r\n    ligReadIndex = ligReadIndex + 1;\r\n    if (ligReadIndex &gt;= numReadings) {\r\n      ligReadIndex = 0;\r\n    }\r\n    ligAverage = ligTotal \/ numReadings;\r\n    Serial.println(light_percent);\r\n    Serial.println(ligAverage);\r\n\r\n  }\r\n\r\n\r\n\r\n  if (ligAverage &gt; 25)\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>Next, I plan to use the sensor to turn another motor (the stepper motor, which I think is a better fit for turning the plant turntable).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the TEMT6000 light sensor, I can turn on a motor based on input to the light sensor, similar to the one I had with the DHT-22. With the TEMT6000, I used an analog write (instead of the digital write with the DHT-22) and the data arrives in float form. Several lines of code convert &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2325\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Crit1 Development: Experiment with Fan Motor and Light Sensor TEMT6000&#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":[13,1],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2325"}],"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=2325"}],"version-history":[{"count":4,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2325\/revisions"}],"predecessor-version":[{"id":2344,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2325\/revisions\/2344"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}