{"id":265,"date":"2015-10-14T21:28:38","date_gmt":"2015-10-14T21:28:38","guid":{"rendered":"http:\/\/www.zoebachman.net\/itp\/?p=265"},"modified":"2015-12-07T23:50:13","modified_gmt":"2015-12-07T23:50:13","slug":"pcomp-wk-6-serial-input-labs","status":"publish","type":"post","link":"http:\/\/www.zoebachman.net\/itp\/?p=265","title":{"rendered":"PComp, Wk 6: Serial Input Labs"},"content":{"rendered":"<p>One thing I really appreciated about doing these labs was how they illuminated small bits of code that we&#8217;ve been using since the beginning, but honestly I had no idea what exactly it all meant (re: 9600 bauds, especially). Now I feel (slightly) more confident with writing code, or at least with dealing with the serial monitor.<\/p>\n<p><a href=\"https:\/\/itp.nyu.edu\/physcomp\/lab-intro-to-serial-communications\/\">Lab: Intro to Asynchronous Serial Communication<\/a><\/p>\n<p>First thought: &#8220;Oooo I get to try out an accelerometer!&#8221;<\/p>\n<p>No problems with hooking up the circuit. Starting to finally feel like I&#8217;ve got a hang on (some of) this stuff.<\/p>\n<p><a href=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/10\/IMG_4550.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-268 aligncenter\" src=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/10\/IMG_4550-300x225.jpg\" alt=\"IMG_4550\" width=\"300\" height=\"225\" srcset=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/10\/IMG_4550-300x225.jpg 300w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/10\/IMG_4550-1024x768.jpg 1024w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/10\/IMG_4550-1200x900.jpg 1200w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now I also get why we didn&#8217;t do serialWrite like we did digital or analogWrite for output &#8211; get garbage numbers! sends out binary that we can&#8217;t interpret.<\/p>\n<p>Alright! Got serial monitor results for all three sensors, and I can distinguish them using punctuation. Cool. Also got the call and response way to work. Feeling good.<iframe loading=\"lazy\" class=\"giphy-embed\" src=\"\/\/giphy.com\/embed\/CDMz3fckRXXDG\" width=\"480\" height=\"269\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><a href=\"http:\/\/giphy.com\/gifs\/love-television-cartoon-CDMz3fckRXXDG\">via GIPHY<\/a><\/p>\n<p>Moving on to the second lab&#8230;<br \/>\n<a href=\"&quot;https:\/\/itp.nyu.edu\/physcomp\/labs\/labs-serial-communication\/lab-serial-input-to-the-p5-js-ide\/\"> Lab: Serial Input to the p5.js IDE<\/a><\/p>\n<p>This statement confused me, not sure if it&#8217;s a language or concept thing: &#8220;The P5.js serialport library can\u2019t access your serial ports directly when a sketch is running in a browser or in the P5 sketch window.&#8221;<\/p>\n<p>So, I thought the serialport could communicate when the p5 sketch window was open? Reading further&#8230;oh, communicates with server on computer. So it&#8217;s just not a &#8220;direct&#8221; connection? So the sketch isn&#8217;t connected to the device, but the code controlling the sketch\/IDE is. I think that&#8217;s what this drawing means:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-3770 aligncenter\" src=\"https:\/\/itp.nyu.edu\/physcomp\/wp-content\/uploads\/socket-serial-connection.png\" alt=\"socket-serial-connection\" width=\"873\" height=\"240\" \/><\/p>\n<p>When I did my p5 sketch&#8230;only got one port? did my USB need to be plugged in? Plug it in&#8230;and still only one:<br \/>\nws:\/\/localhost:8081 &#8211; which, doesn&#8217;t look right to me.<\/p>\n<p>So hopefully that&#8217;s mine? copy and pasted the code instead of typing and the same thing happened. So, guess that&#8217;s how it goes.<\/p>\n<p>Well, when I started doing the second part of the lab &#8211; I got ther error and its also now listening 3 ports, including a USB that must be mine. I switched it and it worked! Or at least, there were no more errors. Not entirely sure why only one port was showing that first time.<\/p>\n<p>At first I could tell that my potentiometer wasn&#8217;t talking to p5 (my number wasn&#8217;t changing. Did I need to have an arduino sketch uploaded that would read my potentiometer? Oh yes, never uploaded my sketch to the arduino. cool. let&#8217;s try that again. YUP. Detective skillz.<\/p>\n<p>Here&#8217;s my video of the graphing:<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/player.vimeo.com\/video\/142432139\" width=\"500\" height=\"313\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><a href=\"https:\/\/vimeo.com\/142432139\">serialCommunication_graph_movie<\/a> from <a href=\"https:\/\/vimeo.com\/user23159700\">Zoe Bachman<\/a> on <a href=\"https:\/\/vimeo.com\">Vimeo<\/a>.<\/p>\n<p>Tried out the delay &#8211; much choppier, I guess because it&#8217;s not gathering data as frequently so there are bigger jumps.<\/p>\n<p>Then I changed it so it would read a string and now there are spaces between the lines. &lt;&lt; Not sure I entirely get strings. It&#8217;s just a string of bits\/bytes? Of different lengths?<\/p>\n<p>Also, why would it read a string that was greater than zero if it said a complete string was every three to six bytes? Shouldn&#8217;t it be greater than 3?:<\/p>\n<p>&#8220;Remember, the \u2018data\u2019 event occurs every time a new byte comes in the serial port. Now that you\u2019re sending an ASCII-encoded string, every potentiometer reading is several bytes long. So you only get a complete string <b>every three to six bytes<\/b> (three for \u201c0\\r\\n\u201d and six for \u201c1023\\r\\n\u201d). Sometimes, when the serialEvent() function calls serial.readStringUntil(&#8216;\\r\\n&#8217;); it gets nothing. That\u2019s when draw() draws the gaps. You need to change your function to check that the resulting string is actually a valid number before you put the string into inData. First, create a local variable to get the string, then check to see <b>if the string\u2019s length is greater than zero<\/b>. If it is, then put it into inData so that the other functions in the sketch can use the new data. Here\u2019s how you do that:&#8221;<\/p>\n<p>Well, got it all to work! yay! I even changed the numbers in the arduino file to get the full 0-1023 range by taking out the mapping. I have to say though, I preferred the sketch when the ranges were 0-255&#8230;gave me better control over the graph!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing I really appreciated about doing these labs was how they illuminated small bits of code that we&#8217;ve been using since the beginning, but honestly I had no idea what exactly it all meant (re: 9600 bauds, especially). Now &hellip; <a href=\"http:\/\/www.zoebachman.net\/itp\/?p=265\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[35,11,15,71],"class_list":["post-265","post","type-post","status-publish","format-standard","hentry","category-pcomp","tag-arduino","tag-p5","tag-pcomp","tag-serial"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/posts\/265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=265"}],"version-history":[{"count":4,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/posts\/265\/revisions"}],"predecessor-version":[{"id":385,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/posts\/265\/revisions\/385"}],"wp:attachment":[{"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=265"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}