{"componentChunkName":"component---src-templates-page-js","path":"/notes/drinking-buddy/","result":{"data":{"markdownRemark":{"html":"<p>For the audio lab in PComp, I built the Drinking Buddy. He just wants to sing <a href=\"http://www.youtube.com/watch?v=Y5ryo-cd-EU\">German drinking songs</a> with you! And even though he probably thinks the more he drinks, the better he gets… that just isn’t true. As your breath alcohol increases, more error is introduced into the playback of the song. To wit:</p>\n<div class=\"video-embed\"><iframe src=\"https://player.vimeo.com/video/50483324\" frameborder=\"0\" allow=\"autoplay; fullscreen\" allowfullscreen></iframe></div>\n<p>The alcohol sensor (<a href=\"https://www.sparkfun.com/products/8880\">MQ-3 from SparkFun</a>) was fun to play with, once I got it working. Every MQ-3-related blog post you come across will mention its misleading datasheet, and it’s true, it was a pain to hook up. I nearly gave up after trying many different configurations, but then I found <a href=\"http://sensorworkshop.blogspot.com/2008/04/sensor-report-mq3-gas-sensor.html\">this blog post</a> that set me in the right direction. First, I was confused about which direction the sensor needed to be installed in, since the diagram shows the left and right pins serving different functions despite the casing having no indication of pin name, however it seems that this is a nonissue. Second, the device has a recommended 48 hour break-in period, so it was hard to tell if I had everything wired correctly but was just getting noisy data, or if my wiring was off.</p>\n<p>Here’s how I ended up wiring it: I pass 5 volts through the “H” pins. They are the middle ones on either side. Various blogs recommended that you do not use the Arduino’s 5v output for this, so I hooked up a 9v battery with a voltage regulator. I also put 5 volts into the “A” pin, which can be either of the pins to the side of the +5 “H” pin. The “B” pin, located opposite of the “A” pin, gets grounded via a 220 ohm resistor and then sent to the Arduino’s analog in (I believe this is called a pull-up resistor?). The Sensor Workshop blog post I linked before has the analog out coming from the “H” pin, however this didn’t work for me (nor would I expect it to, I’m not sure why they have it that way).</p>\n<p>At that point it was clear that when I blew alcohol across the sensor (using hand sanitizer as my “test booze”), the Arduino registered a higher output value. After leaving it on for 5 or so hours today, it works even better. One funny aspect of the sensor is its “hangover” period: it will continue to register alcohol in its sensor even after you’ve blown it over.</p>\n<p>The audio aspect of the program was a lot of fun. First, I had to transcribe the song into a format that the Arduino would understand. I downloaded a midi file of the song and opened it in <a href=\"http://www.myriad-online.com/en/products/harmony.htm\">Harmony Assistant</a>, which revealed the musical notation. I took those notes and charted them out, as can be seen below.</p>\n<img class=\"aligncenter size-full wp-image-137\" title=\"P9272101\" alt=\"\" src=\"/img/uploads/2012/09/P9272101.jpg\"  />\n<p>Finally, I entered that info into code using the <a href=\"https://code.google.com/p/arduino/source/browse/trunk/build/shared/examples/Digital/toneMultiple/pitches.h?r=943\">Pitches.h</a> library.</p>\n<pre><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">int</span> melody[] = {\n  NOTE_D3, NOTE_B2, NOTE_D3, NOTE_B2,\n  NOTE_C3, NOTE_C3, NOTE_B2, NOTE_C3, NOTE_D3, NOTE_B2, NOTE_G3, NOTE_D3,\n  NOTE_D3, NOTE_B2, NOTE_D3, NOTE_B2,\n  NOTE_C3, NOTE_C3, NOTE_B2, NOTE_C3, NOTE_A2, NOTE_G2,\n  NOTE_A2, NOTE_B2, NOTE_D3, NOTE_A2, NOTE_B2, NOTE_C3, NOTE_D3, NOTE_B2,\n  NOTE_A2, NOTE_B2, NOTE_C3, NOTE_A2, NOTE_B2, NOTE_C3, NOTE_D3, NOTE_B2,\n  NOTE_D3, NOTE_B2, NOTE_D3, NOTE_B2,\n  NOTE_C3, NOTE_C3, NOTE_B2, NOTE_C3, NOTE_A2, NOTE_G2\n};\n\n<span class=\"hljs-keyword\">int</span> durations[] = {\n  <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">4</span>,\n  <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">16</span>, <span class=\"hljs-number\">16</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">4</span>,\n  <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">4</span>,\n  <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">16</span>, <span class=\"hljs-number\">16</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">2</span>,\n  <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>,\n  <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>,\n  <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">4</span>,\n  <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">16</span>, <span class=\"hljs-number\">16</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">2</span>\n};</code></pre>\n<p>To “drunkify” the notes, I wrote a function that randomly changes the pitch and duration values. Since the Pitches.h constants only represent integer values, you can increase or decrease the pitch by increasing or decreasing the value. Same with note duration, except since that uses milliseconds, I just add or subtract a larger number. The drunkenness parameter changes both the likelihood that a pitch will be altered and the intensity with which it will be altered.</p>\n<pre><code class=\"hljs language-cpp\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">void</span> <span class=\"hljs-title\">drunkifyNote</span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">int</span> pitch, <span class=\"hljs-keyword\">int</span> duration, <span class=\"hljs-keyword\">float</span> drunkenness)</span> </span>{\n  boolean drunkifyPitch = binaryRandomWithProbability(drunkenness);\n  boolean drunkifyDuration = binaryRandomWithProbability(drunkenness);\n\n  <span class=\"hljs-keyword\">if</span>(drunkifyPitch) pitch += (random(<span class=\"hljs-number\">20</span>) * drunkenness) – <span class=\"hljs-number\">10</span>;\n  <span class=\"hljs-keyword\">if</span>(drunkifyDuration) duration += (random(<span class=\"hljs-number\">200</span>) * drunkenness) – <span class=\"hljs-number\">100</span>;\n\n  tone(speakerPin, pitch, duration);\n\n  delay(duration + <span class=\"hljs-number\">30</span>);\n}\n\n<span class=\"hljs-function\">boolean <span class=\"hljs-title\">binaryRandomWithProbability</span><span class=\"hljs-params\">(<span class=\"hljs-keyword\">float</span> probability)</span> </span>{\n  <span class=\"hljs-keyword\">int</span> rand = random(<span class=\"hljs-number\">100</span>);\n  boolean returnVal = <span class=\"hljs-literal\">false</span>;\n  <span class=\"hljs-keyword\">if</span>(probability * <span class=\"hljs-number\">100</span> > rand) returnVal = <span class=\"hljs-literal\">true</span>;\n  <span class=\"hljs-keyword\">return</span> returnVal;\n}</code></pre>\n<p>After plugging in the sensor’s value, I was all set! Below, pictures of the complete setup and a close-up of the MQ-3.</p>\n<img class=\"aligncenter size-full wp-image-136\" title=\"P9292122\" alt=\"\" src=\"/img/uploads/2012/09/P9292122.jpg\" />\n<img class=\"aligncenter size-full wp-image-135\" title=\"P9292124\" alt=\"\" src=\"/img/uploads/2012/09/P9292124.jpg\"  />","frontmatter":{"path":"/notes/drinking-buddy/","title":"Drinking buddy","date":"2012-09-30T19:54:55.000Z","categories":["ITP","Physical Computing","Sensors"],"churl":null,"uses":null}}},"pageContext":{"post":true,"prev":{"url":"/notes/pcomp-hits-and-misses/","title":"Reaction to physical computing’s greatest hits (and misses)"},"next":{"url":"/notes/tracking-street-noise/","title":"Tracking street noise"}}}}