{"id":196,"date":"2014-09-29T21:09:10","date_gmt":"2014-09-29T21:09:10","guid":{"rendered":"http:\/\/itp.fromjia.com\/?p=196"},"modified":"2014-09-29T21:09:10","modified_gmt":"2014-09-29T21:09:10","slug":"lab-4-two-way-serial-communication","status":"publish","type":"post","link":"http:\/\/itp.fromjia.com\/?p=196","title":{"rendered":"Lab 4: Two-way serial communication"},"content":{"rendered":"<p>When reading multiple sensors a question that comes up is &#8220;which value corresponds with which sensor?&#8221; There are two ways to set sensor values in order: <em>punctuation method<\/em>, which involves punctuating each set of data uniquely, and <em>call-and-response (handshake) method<\/em>, which works very much like it sounds, sending one set of values at a time by setting the programs to call and respond to each other.<\/p>\n<p>We tried each method in this lab, neither worked very well for me. And I have a lot of questions regarding the additional code.<\/p>\n<p>This is what I&#8217;ve learned so far:<\/p>\n<p><strong>Punctuation method<\/strong><\/p>\n<p>To send data from the Arduino, we use <strong>Serial.print()<\/strong> and <strong>Serial.println()<\/strong> to send\u00a0out each set of values, separated by commas <strong>Serial.print(&#8220;,&#8221;);<\/strong>.<\/p>\n<p>To receive the values in Processing, we add <strong>myPort.bufferUntil(&#8216;\\n&#8217;);<\/strong>\u00a0in setup() so that the string of values would be saved to the buffer and read together. In the\u00a0<strong>serialEvent()<\/strong>\u00a0function, the string is read with <strong>myPort.readStringUntil(&#8216;\\n&#8217;);<\/strong>.\u00a0Then\u00a0trim out any whitespace character (newline, carriage returns, spaces, tabs) with <strong>trim(stringx)<\/strong>, split the values at each comma with <strong>split(stringx, &#8216;,&#8217;);<\/strong>, convert the values into integers and store them in an array, for instance <strong>int<\/strong> <strong>sensor[] = int(split(stringx, &#8216;,&#8217;));<\/strong>.<\/p>\n<p>There are a few downsides to this method, namely the program may slow down if the buffer is full and that we can&#8217;t easily send binary values.<\/p>\n<p><strong>Call-and-response\u00a0method<\/strong><\/p>\n<p>In the Arduino IDE, we setup a new function\u00a0<strong>establishContact()<\/strong>. This function sends out a message until receives data from Processing. We check for data by checking whether\u00a0<strong>Serial.available()<\/strong> is greater than 0 (true) or less then\/equal to 0 (false). We call for this new function in setup(). Then, with an <strong>if<\/strong>\u00a0statement, make sure that the strings of values in\u00a0loop() would only send when Serial.available() is greater than 0.<\/p>\n<p>In Processing, we check for contact with a new variable <strong>boolean firstContact = false;<\/strong>. In serialEvent() we let the program first check if the message from the Arduino is the right one with <strong>myString.equals()<\/strong>,\u00a0then send back a character with <strong>myPort.write()<\/strong> to start receiving actual data. If firstContact is true, the code from the rest of serialEvent() would run, trimming, splitting, converting, and storing the values.<\/p>\n<p><strong>Issues:\u00a0<\/strong><\/p>\n<ul>\n<li>With the lab (make a simple DIY mouse), my digital input worked well, allowing the ball to appear and disappear from the screen. But with the ball fluttered about when I moved the accelerometer. Instead of moving on the x and y axis smoothly, the ball was all over the place.<\/li>\n<li>Often the program didn&#8217;t even run. It displayed an error message.<\/li>\n<\/ul>\n<div id=\"attachment_200\" style=\"width: 740px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/itp.fromjia.com\/wp-content\/uploads\/2014\/09\/Screen-Shot-2014-09-28-at-10.09.14-PM.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-200\" class=\"wp-image-200 size-large\" src=\"http:\/\/itp.fromjia.com\/wp-content\/uploads\/2014\/09\/Screen-Shot-2014-09-28-at-10.09.14-PM-1003x1024.png\" alt=\"Error message in Processing.\" width=\"730\" height=\"745\" srcset=\"http:\/\/itp.fromjia.com\/wp-content\/uploads\/2014\/09\/Screen-Shot-2014-09-28-at-10.09.14-PM-1003x1024.png 1003w, http:\/\/itp.fromjia.com\/wp-content\/uploads\/2014\/09\/Screen-Shot-2014-09-28-at-10.09.14-PM-293x300.png 293w, http:\/\/itp.fromjia.com\/wp-content\/uploads\/2014\/09\/Screen-Shot-2014-09-28-at-10.09.14-PM-1200x1224.png 1200w, http:\/\/itp.fromjia.com\/wp-content\/uploads\/2014\/09\/Screen-Shot-2014-09-28-at-10.09.14-PM.png 1360w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><\/a><p id=\"caption-attachment-200\" class=\"wp-caption-text\">Error message in Processing.<\/p><\/div>\n<p><strong>Questions:<\/strong><\/p>\n<ul>\n<li>Why is the call-and-response method better for sending binary data, even though it&#8217;s also sending and receiving strings with commas, then trimming, splitting, and storing them in a buffer? Also, wouldn&#8217;t this also slow down the program if the buffer gets full?<\/li>\n<li>Why do we need to call-and-respond between the programs?<\/li>\n<li>Explain more about\u00a0the new functions in Arduino IDE and Processing. e.g. Serial.available(), trim(), split(), myPort.bufferUntil(), myPort.readStringUntil(), myString.equals(), etc.<\/li>\n<\/ul>\n<div id=\"attachment_201\" style=\"width: 510px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/itp.fromjia.com\/wp-content\/uploads\/2014\/09\/serial_multiple.gif\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-201\" class=\"size-full wp-image-201\" src=\"http:\/\/itp.fromjia.com\/wp-content\/uploads\/2014\/09\/serial_multiple.gif\" alt=\"Values show up properly in the Arduino IDE serial monitor.\" width=\"500\" height=\"281\" \/><\/a><p id=\"caption-attachment-201\" class=\"wp-caption-text\">Values show up properly in the Arduino IDE serial monitor.<\/p><\/div>\n<div id=\"attachment_203\" style=\"width: 510px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/itp.fromjia.com\/wp-content\/uploads\/2014\/09\/serial_multiple2.gif\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-203\" class=\"size-full wp-image-203\" src=\"http:\/\/itp.fromjia.com\/wp-content\/uploads\/2014\/09\/serial_multiple2.gif\" alt=\"Digital input works. Issues with analog input values. \" width=\"500\" height=\"281\" \/><\/a><p id=\"caption-attachment-203\" class=\"wp-caption-text\">Digital input works. Issues with analog input values.<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>When reading multiple sensors a question that comes up is &#8220;which value corresponds with which sensor?&#8221; There are two ways to set sensor values in order: punctuation method, which involves punctuating each set of data uniquely, and call-and-response (handshake) method, which works very much like it sounds, sending one set of values at a time [&hellip;]<\/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":[],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-physical-computing"],"_links":{"self":[{"href":"http:\/\/itp.fromjia.com\/index.php?rest_route=\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/itp.fromjia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/itp.fromjia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/itp.fromjia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/itp.fromjia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=196"}],"version-history":[{"count":4,"href":"http:\/\/itp.fromjia.com\/index.php?rest_route=\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":204,"href":"http:\/\/itp.fromjia.com\/index.php?rest_route=\/wp\/v2\/posts\/196\/revisions\/204"}],"wp:attachment":[{"href":"http:\/\/itp.fromjia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/itp.fromjia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/itp.fromjia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}