{"id":2444,"date":"2022-03-31T19:51:41","date_gmt":"2022-03-31T23:51:41","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2444"},"modified":"2022-03-31T19:51:41","modified_gmt":"2022-03-31T23:51:41","slug":"crit-2-sound","status":"publish","type":"post","link":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2444","title":{"rendered":"Crit 2: Sound"},"content":{"rendered":"<p>This project uses a color sensor (TCS34725) to determine whether or not a banana is ripe. The sketch includes a button for the user to press when ready to evaluate the banana. One series of beeps indicates the banana is ready to eat, another series indicates the banana is not ready to eat. Since there is a data smoothing component, there is also a noise that identifies when new readings are dramatically different from the average reading, and notifies the user that the sensor is still calculating.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;Wire.h&gt;\r\n#include \"Adafruit_TCS34725.h\"\r\n\r\n\/* Example code for the Adafruit TCS34725 breakout library *\/\r\n\r\n\/* Connect SCL    to analog 5\r\n   Connect SDA    to analog 4\r\n   Connect VDD    to 3.3V DC\r\n   Connect GROUND to common ground *\/\r\n\r\n\/* Initialise with default values (int time = 2.4ms, gain = 1x) *\/\r\nAdafruit_TCS34725 tcs = Adafruit_TCS34725();\r\n\r\n\/\/* Initialise with specific int time and gain values *\r\n\/\/Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_500MS, TCS34725_GAIN_1X);\r\n\r\nint speakerpin=8;\r\nint buttonpin=2;\r\n\r\n\r\n\/\/ data smoothing\r\nconst int numReadings = 10;\r\n\r\nint readings[numReadings];      \/\/ the readings from the analog input\r\nint readIndex = 0;              \/\/ the index of the current reading\r\nint total = 0;                  \/\/ the running total\r\nint averageb = 0;                \/\/ the average\r\n\r\n\r\nvoid setup(void) {\r\n  Serial.begin(9600);\r\n  for (int thisReading = 0; thisReading &lt; numReadings; thisReading++) {\r\n    readings[thisReading] = 0;\r\n  }\r\n\r\n  pinMode(buttonpin,INPUT_PULLUP);\r\n\r\n  if (tcs.begin()) {\r\n    Serial.println(\"Found sensor\");\r\n  } else {\r\n    Serial.println(\"No TCS34725 found ... check your connections\");\r\n    while (1);\r\n  }\r\n\r\n  \/\/ Now we're ready to get readings!\r\n}\r\n\r\nvoid loop(void) {\r\n  uint16_t r, g, b, c, colorTemp, lux;\r\n\r\n  tcs.getRawData(&amp;r, &amp;g, &amp;b, &amp;c);\r\n  \/\/ colorTemp = tcs.calculateColorTemperature(r, g, b);\r\n  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);\r\n  lux = tcs.calculateLux(r, g, b);\r\n\r\n\r\n    \/\/ subtract the last reading:\r\n  total = total - readings[readIndex];\r\n  \/\/ read from the sensor:\r\n  readings[readIndex] = b;\r\n  Serial.println(b);\r\n  \/\/ add the reading to the total:\r\n  total = total + readings[readIndex];\r\n  \/\/ advance to the next position in the array:\r\n  readIndex = readIndex + 1;\r\n\r\n  \/\/ if we're at the end of the array...\r\n  if (readIndex &gt;= numReadings) {\r\n    \/\/ ...wrap around to the beginning:\r\n    readIndex = 0;\r\n  }\r\n\r\n  \/\/ calculate the average:\r\n  averageb = total \/ numReadings;\r\n  \/\/ send it to the computer as ASCII digits\r\n  Serial.println(averageb);\r\n  delay(1);        \/\/ delay in between reads for stability\r\n\r\n\r\n\r\n  Serial.print(\"Color Temp: \"); Serial.print(colorTemp, DEC); Serial.print(\" K - \");\r\n  Serial.print(\"Lux: \"); Serial.print(lux, DEC); Serial.print(\" - \");\r\n  Serial.print(\"R: \"); Serial.print(r, DEC); Serial.print(\" \");\r\n  Serial.print(\"G: \"); Serial.print(g, DEC); Serial.print(\" \");\r\n  Serial.print(\"B: \"); Serial.print(b, DEC); Serial.print(\" \");\r\n  Serial.print(\"C: \"); Serial.print(c, DEC); Serial.print(\" \");\r\n  Serial.println(\" \");\r\ndelay(100); delay(100);   delay(100);   \r\n\r\n  int buttonvalue=digitalRead(buttonpin);\r\n  Serial.println(buttonvalue);\r\n\r\n  if (buttonvalue == HIGH) {\r\n    Serial.println(\"button is pressed\");\r\n    if (averageb&gt;=20){\r\n      Serial.println(\"not ready\");\r\n      tone(speakerpin,293.66,1000);\r\n      delay(1000);\r\n      noTone(speakerpin);\r\n      delay(500);\r\n      tone(speakerpin,293.66,1000);\r\n      delay(1000);\r\n      noTone(speakerpin);\r\n    } else if (b-averageb&gt;=10) {\r\n      Serial.println(\"calculating\");\r\n      tone(speakerpin,200,500);\r\n      delay(500);\r\n      noTone(speakerpin);\r\n      delay(500);\r\n    } else {\r\n      Serial.println(\"ready to eat\");\r\n      tone(speakerpin,329.63,250);\r\n      delay(250);\r\n      noTone(speakerpin);\r\n      delay(250);\r\n      tone(speakerpin,311.13,250);\r\n      noTone(speakerpin);\r\n      delay(1000);\r\n      tone(speakerpin,329.63,1000);\r\n      noTone(speakerpin);\r\n      delay(1000);\r\n      }\r\n\r\n  } else {\r\n    Serial.println(\"button is not pressed\");\r\n  }\r\n\r\n\r\n\r\n\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This project uses a color sensor (TCS34725) to determine whether or not a banana is ripe. The sketch includes a button for the user to press when ready to evaluate the banana. One series of beeps indicates the banana is ready to eat, another series indicates the banana is not ready to eat. Since there &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/?p=2444\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Crit 2: Sound&#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":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2444"}],"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=2444"}],"version-history":[{"count":2,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2444\/revisions"}],"predecessor-version":[{"id":2457,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=\/wp\/v2\/posts\/2444\/revisions\/2457"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/48-339\/s2022\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}