{"id":697,"date":"2009-10-10T13:02:38","date_gmt":"2009-10-10T18:02:38","guid":{"rendered":"http:\/\/itp.indiamos.com\/blog\/?p=697"},"modified":"2017-12-28T01:01:01","modified_gmt":"2017-12-28T06:01:01","slug":"pongbox","status":"publish","type":"post","link":"https:\/\/itp.indiamos.com\/blog\/2009\/10\/10\/pongbox\/","title":{"rendered":"Pongbox"},"content":{"rendered":"<p>For week 2 of <a href=\"http:\/\/itp.nyu.edu\/understandingnetworks\/\">Understanding Networks<\/a>, a newly created class that keeps shifting between abstraction and application, leaving me sadly uncertain as to whether I understand what Tom&#8217;s talking about at any given moment, our terrifying assignment was as follows:<\/p>\n<blockquote><p>Socket exercise: communicating in realtime. I will give you a game platform and the protocols to log in and communicate with it. Make a client to log in and play. Work in Processing, Flash, Arduino or whatever environment is comfortable to you. You will not play with your own client, but you&#8217;ll show someone else how to use it.<\/p><\/blockquote>\n<p><!--more-->As Tom apprised us of this mission, I felt a panicky fit of rage welling up, and in my head I started to whine. <em>This is such a pure ITP experience: spend two and a half hours trying to follow a discussion about something totally vague, and in the last two minutes get an assignment that&#8217;s way over my head, with absolutely no instruction in how to do it. Fucking <a href=\"http:\/\/itp.indiamos.com\/blog\/category\/physcomp\/\">PhysComp<\/a> all over again.<\/em><\/p>\n<p>I stomped out of the room and headed to my next activity alternately fuming and telling myself, <em>Shut up, shut up, shut up! Stop bitching about how you can&#8217;t do this, and instead think about what you&#8217;re going to do! Because at midnight on the night before the thing is due, you know you&#8217;re going to bite the bullet and start doing something. How about starting early, for once, instead of wasting time being indignant?<\/em><\/p>\n<p>Yeah, this is how things are in my head these days.<\/p>\n<p>So I thought about it, and I decided not to overthink it. I wanted to avoid fucking with <a href=\"http:\/\/www.processing.org\/\">Processing<\/a> (an ongoing aversion, apparently developed sometime in the first semester, although I enjoyed <a href=\"http:\/\/itp.indiamos.com\/blog\/category\/icm\/\"><acronym title=\"Introduction to Computational Media\">ICM<\/acronym><\/a>), so I would just make a simple device that would have an obvious physical mapping: an accelerometer-based controller that would move the Ping-Pong paddle right when you tilted your hand to the right, left when you tilted your hand to the left, up and down blah blah, with a switch to restart the game or whatever. I thought I&#8217;d make it as small and light as possible, wearable on the back of the hand. Tr\u00e8s elegant.<\/p>\n<p>Well. Once I&#8217;d figured that out, I panicked less. So much less that I still didn&#8217;t feel a need to start the project until the afternoon before it was due. Curses! Foiled again!<\/p>\n<p>I gathered all my electronics crap on Wednesday afternoon and took it to school, which I&#8217;d heard was a place where some people do their homework. On the way, I picked up a <a href=\"http:\/\/www.sparkfun.com\/commerce\/product_info.php?products_id=848\">peppy red accelerometer<\/a> at the NYU computer store. I set up in one of the least comfortable and work-friendly parts of the lounge and started casually wiring stuff together. I have conductive thread and enjoyed sewing the circuit for the drawing glove last fall, but I also wanted to follow the very wise advice that <a href=\"http:\/\/www.linkedin.com\/in\/petereraymond\">Peter Raymond<\/a> had given in <a href=\"http:\/\/itp.nyu.edu\/wearables\/Class\/Syllabus\">Wearables<\/a> this spring\u2014which is the same advice a lot of people give, but for some reason the way he said it sunk in\u2014and focus on knocking together a prototype as quickly and dirtily as possible, so that I could get a sense of whether it worked <em>before<\/em> I started to obsess about making it look nice.<\/p>\n<p>So I took the LilyPad and USB-mini cable I&#8217;d bought last spring out of their plastic baggies, dug up the <a href=\"http:\/\/www.sparkfun.com\/commerce\/product_info.php?products_id=8772\">Sparkfun FTDI Basic Breakout board<\/a> I&#8217;d bought a year ago, and\u2026noticed that the pins on the breakout board and the LilyPad didn&#8217;t match. Because although the LilyPad, which I&#8217;d gotten at the NYU store, was in a baggie labeled with <a href=\"http:\/\/www.sparkfun.com\/commerce\/product_info.php?products_id=8465\">the current Sparkfun part number<\/a>, it was, in fact, the <a href=\"http:\/\/arduino.cc\/en\/Main\/Boards\">LilyPad Arduino 02<\/a>, which has four pins to the breakout board&#8217;s six.<\/p>\n<p><a href=\"http:\/\/arduino.cc\/en\/Main\/Boards\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/itp.indiamos.com\/blog\/wp-content\/uploads\/2009\/10\/LilyPad02+LilyPad04+breakout.png?resize=474%2C180\" alt=\"LilyPad 02 + LilyPad 04 + FTDI Basic Breakout\" title=\"LilyPad 02 + LilyPad 04 + FTDI Basic Breakout\" width=\"474\" height=\"180\" class=\"alignnone size-full wp-image-704\" srcset=\"https:\/\/i2.wp.com\/itp.indiamos.com\/blog\/wp-content\/uploads\/2009\/10\/LilyPad02+LilyPad04+breakout.png?w=500&amp;ssl=1 500w, https:\/\/i2.wp.com\/itp.indiamos.com\/blog\/wp-content\/uploads\/2009\/10\/LilyPad02+LilyPad04+breakout.png?w=400&amp;ssl=1 400w\" sizes=\"(max-width: 474px) 100vw, 474px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>That Arduino was old when I <em>started<\/em> at ITP, and I&#8217;d deliberately avoided buying one from the NYU store last fall, because I&#8217;d read about the new six-pin version on Sparkfun and noticed that the local store still had old stock. By the time I bought LilyPad one the following spring, though, they should have been through with them\u2014and perhaps they didn&#8217;t realize that they weren&#8217;t, because the thing was in a baggie labeled with the wrong part number. The result was a pain in my ass, leaving me with a device I couldn&#8217;t easily plug into a USB cable. I stared at the schematic, and at the breakout board, and the LilyPad itself, using a loupe so that I could read the labels on the pins in the by the lounge&#8217;s dingy evening light. Then <a href=\"http:\/\/zachtayloriv.net\/\">Zach Taylor<\/a> looked at it with me and confirmed my hope that I could use wires to connect the pins on the Arduino to the relevant pins on the breakout board. Then I found a packet of female-to-female jumper wires among my electronics junk, and I thought my problem was solved. I hooked up the accelerometer using pliers and wire, plugged everything in, and . . .<\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/indiamos\/3959341839\/\" title=\"Ghetto LilyPad adapter by indiamos, on Flickr\"><img loading=\"lazy\" src=\"https:\/\/i2.wp.com\/farm4.static.flickr.com\/3530\/3959341839_1911dda9e3.jpg?resize=474%2C315\" width=\"474\" height=\"315\" alt=\"Ghetto LilyPad adapter\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>It didn&#8217;t work.<\/p>\n<p>I couldn&#8217;t get Arduino-the-app to talk to Arduino-the-board. The LED on the board lit up, and it did a bit of blinking, but it didn&#8217;t seem to be listening. Having had similar issues with the Arduino NG which the NYU store had sold me\u2014another instance of their selling outdated hardware, and the current board (and the one they sold to everyone else in my PhysComp class) was then the Diecimila\u2014I tried pressing the reset button at various times before and after clicking the upload button in the app. Meanwhile, Snow Leopard was giving me a message about a new something-something being installed everytime I plugged the Arduino in, and I wasn&#8217;t sure what that was about. I spent some time looking at posts on <a href=\"http:\/\/www.arduino.cc\/cgi-bin\/yabb2\/YaBB.pl?board=swtrouble\">arduino.cc troubleshooting forum<\/a>, but without another board to use for testing, I couldn&#8217;t tell where the problem might lie\u2014was it my ad-hoc cable adapter, or Snow Leopard, or the finicky LilyPad? So I took the whole mess home, so I could try again with my known-to-be-functional Duemilanove.<\/p>\n<p>The Duemilanove worked eventually, of course, but it&#8217;s too big to wear on the back of your hand, so I switched from a wearable model to a handheld device. So I ended up cutting some holes in a Sparkfun box, taping the board in place inside, and printing a label for the top. Simple cue: if the text is upside-down when you&#8217;re holding the box, you&#8217;ve got it facing the wrong way.<\/p>\n<p><a href=\"http:\/\/www.flickr.com\/photos\/indiamos\/3960120366\/\" title=\"Pong controller by indiamos, on Flickr\"><img loading=\"lazy\" src=\"https:\/\/i1.wp.com\/farm3.static.flickr.com\/2435\/3960120366_d4e231b6ab.jpg?resize=375%2C500\" width=\"375\" height=\"500\" alt=\"Pong controller\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>The final Processing code, infinitesimally modified from <a href=\"http:\/\/itp.nyu.edu\/understandingnetworks\/Code\/ProcessingNetClient.\">what Tom supplied to us<\/a>:<br \/>\n[java]<br \/>\n\/*<br \/>\n Serial String Reader + Simple net Client<br \/>\n Language: Processing<\/p>\n<p> Reads in a string of characters from a serial port until<br \/>\n it gets a linefeed (ASCII 10).  Then splits the string into<br \/>\n sections separated by commas. Then converts the sections to ints,<br \/>\n and prints them out.<\/p>\n<p> * Starts a network client that connects to a server on port 8080,<br \/>\n * sends any keystrokes pressed.<\/p>\n<p> created 2 Jun 2005<br \/>\n modified 6 Aug 2008<br \/>\n by Tom Igoe<br \/>\n modified 24 September 2009 by India Amos<br \/>\n *\/<\/p>\n<p>import processing.serial.*;     \/\/ import the Processing serial library<br \/>\nimport processing.net.*;        \/\/ import the Processing networking library<\/p>\n<p>Serial myPort;                  \/\/ The serial port<\/p>\n<p>Client myClient;                   \/\/ instance of the net Client<br \/>\nString data;                       \/\/ string to hold incoming data<br \/>\nString ipAddress = &#8220;128.122.151.178&#8221;;    \/\/ address of the server<br \/>\nString myKey1;                   \/\/ the virtual keypress generated by the x-pin of the accelerometer<br \/>\nString myKey2;                   \/\/ the virtual keypress generated by the y-pin of the accelerometer<br \/>\nString myKey3;                   \/\/ the virtual keypress generated by the switch<br \/>\nString myKeyBuffer;              \/\/ all the virtual keys pressed by a single bleat from the Arduino <\/p>\n<p>float bgcolor;\t\t        \/\/ Background color<\/p>\n<p>void setup() {<br \/>\n  \/\/ establish the background and foreground:<br \/>\n  size(200, 200);<br \/>\n  background(50);<br \/>\n  fill(200);<\/p>\n<p>  \/\/ Connect to server on port 8080<br \/>\n  myClient = new Client(this, ipAddress, 8080);<\/p>\n<p>  \/\/ List all the available serial ports<br \/>\n  println(Serial.list());<\/p>\n<p>  \/\/ I know that the first port in the serial list on my mac<br \/>\n  \/\/ is always my  Arduino module, so I open Serial.list()[0].<br \/>\n  \/\/ Change the 0 to the appropriate number of the serial port<br \/>\n  \/\/ that your microcontroller is attached to.<br \/>\n  myPort = new Serial(this, Serial.list()[0], 9600);<\/p>\n<p>  \/\/ read bytes into a buffer until you get a linefeed (ASCII 10):<br \/>\n  myPort.bufferUntil(&#8216;\\n&#8217;);<br \/>\n}<\/p>\n<p>void draw() {<br \/>\n}<\/p>\n<p>\/\/ serialEvent  method is run automatically by the Processing applet<br \/>\n\/\/ whenever the buffer reaches the  byte value set in the bufferUntil()<br \/>\n\/\/ method in the setup():<\/p>\n<p>void serialEvent(Serial myPort) {<br \/>\n  \/\/ read the serial buffer:<br \/>\n  String myString = myPort.readStringUntil(&#8216;\\n&#8217;);<br \/>\n  \/\/ if you got any bytes other than the linefeed:<br \/>\n  if (myString != null) {<\/p>\n<p>    myString = trim(myString);<\/p>\n<p>    \/\/ split the string at the commas<br \/>\n    \/\/ and convert the sections into integers:<br \/>\n    int sensors[] = int(split(myString, &#8216;\\t&#8217;));<\/p>\n<p>    \/\/ print out the values you got:<br \/>\n    print(sensors[0] + &#8220;,&#8221;); <\/p>\n<p>    \/\/ If there&#8217;s incoming data from the client:<\/p>\n<p>    if (myClient.available() > 0) {<br \/>\n      \/\/ get the data:<br \/>\n      data = myClient.readString();<br \/>\n      println(data);<br \/>\n    }<\/p>\n<p>    if (myKeyBuffer != null) {<br \/>\n      \/\/ send out anything that&#8217;s typed:<br \/>\n      myClient.write(myKeyBuffer);<br \/>\n    }<br \/>\n    if (keyPressed) {<br \/>\n      \/\/ send out anything that&#8217;s typed:<br \/>\n      myClient.write(key);<br \/>\n    }<br \/>\n  }<br \/>\n}[\/java]<\/p>\n<p>The final Arduino code, cribbed from <a href=\"http:\/\/www.arduino.cc\/en\/Tutorial\/ADXL3xx\">Arduino.cc<\/a>:<br \/>\n[csharp]<br \/>\n\/*<br \/>\n  ADXL3xx<\/p>\n<p>  Reads an Analog Devices ADXL3xx accelerometer and communicates the<br \/>\n  acceleration to the computer.  The pins used are designed to be easily<br \/>\n  compatible with the breakout boards from Sparkfun, available from:<br \/>\n  http:\/\/www.sparkfun.com\/commerce\/categories.php?c=80<\/p>\n<p>  http:\/\/www.arduino.cc\/en\/Tutorial\/ADXL3xx<\/p>\n<p>  The circuit:<br \/>\n  analog 0: accelerometer self test<br \/>\n  analog 1: z-axis<br \/>\n  analog 2: y-axis<br \/>\n  analog 3: x-axis<br \/>\n  analog 4: ground<br \/>\n  analog 5: vcc<br \/>\n  digital 2: push-button switch<\/p>\n<p>  created 2 July 2008 by David A. Mellis<br \/>\n  modified 26 June 2009 by Tom Igoe<br \/>\n  modified 24 September 2009 by India Amos<\/p>\n<p> *\/<\/p>\n<p> \/\/ these constants describe the pins. They won&#8217;t change:<br \/>\n const int groundpin = 18;             \/\/ analog input pin 4 &#8212; ground<br \/>\n const int powerpin = 19;              \/\/ analog input pin 5 &#8212; voltage<br \/>\n const int xpin = 3;                   \/\/ x-axis of the accelerometer<br \/>\n const int ypin = 2;                   \/\/ y-axis<br \/>\n const int zpin = 1;                   \/\/ z-axis (only on 3-axis models)<br \/>\n const int pushbuttonpin = 2;          \/\/ digital input <\/p>\n<p> void setup()<br \/>\n {<br \/>\n   \/\/ initialize the serial communications:<br \/>\n   Serial.begin(9600);<\/p>\n<p>   \/\/ Provide ground and power by using the analog inputs as normal<br \/>\n   \/\/ digital pins.  This makes it possible to directly connect the<br \/>\n   \/\/ breakout board to the Arduino.  If you use the normal 5V and<br \/>\n   \/\/ GND pins on the Arduino, you can remove these lines.<br \/>\n   pinMode(groundpin, OUTPUT);<br \/>\n   pinMode(powerpin, OUTPUT);<br \/>\n   pinMode(pushbuttonpin, INPUT);<br \/>\n   digitalWrite(groundpin, LOW);<br \/>\n   digitalWrite(powerpin, HIGH);<br \/>\n }<\/p>\n<p> void loop()<br \/>\n {<br \/>\n   \/\/ print the sensor values:<br \/>\n   Serial.print(analogRead(xpin));<br \/>\n   \/\/ print a tab between values:<br \/>\n   Serial.print(&#8220;\\t&#8221;);<br \/>\n   Serial.print(analogRead(ypin));<br \/>\n   \/\/ print a tab between values:<br \/>\n   Serial.print(&#8220;\\t&#8221;);<br \/>\n   Serial.print(analogRead(zpin));<br \/>\n   \/\/ print a tab between values:<br \/>\n   Serial.print(&#8220;\\t&#8221;);<br \/>\n   Serial.print(digitalRead(pushbuttonpin));<br \/>\n   Serial.println();<br \/>\n   \/\/ delay before next reading:<br \/>\n   delay(50);<br \/>\n }<br \/>\n[\/csharp]<\/p>\n<p>And here&#8217;s <a href=\"http:\/\/www.tigoe.net\/pcomp\/code\/category\/Processing\/592\">the software we played against<\/a>, more or less.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For week 2 of Understanding Networks, a newly created class that keeps shifting between abstraction and application, leaving me sadly uncertain as to whether I understand what Tom&#8217;s talking about at any given moment, our terrifying assignment was as follows: Socket exercise: communicating in realtime. I will give you a game platform and the protocols &hellip; <a href=\"https:\/\/itp.indiamos.com\/blog\/2009\/10\/10\/pongbox\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Pongbox<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[12,11,4,6,45],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s3qY10-pongbox","_links":{"self":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts\/697"}],"collection":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/comments?post=697"}],"version-history":[{"count":18,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts\/697\/revisions"}],"predecessor-version":[{"id":944,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/posts\/697\/revisions\/944"}],"wp:attachment":[{"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/media?parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/categories?post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itp.indiamos.com\/blog\/wp-json\/wp\/v2\/tags?post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}