{"id":68,"date":"2010-10-09T03:06:59","date_gmt":"2010-10-09T03:06:59","guid":{"rendered":"http:\/\/www.mariarabinovich.com\/physcomp\/?p=68"},"modified":"2011-12-20T18:09:26","modified_gmt":"2011-12-20T18:09:26","slug":"bust-a-nut-dirty-joke-squirrel","status":"publish","type":"post","link":"http:\/\/www.mariarabinovich.com\/blog\/archives\/68","title":{"rendered":"Stupid Pet Trick"},"content":{"rendered":"

Allison and I made a squirrel nut cracker that cracks jokes as it cracks nuts. A few things I would like to remember: the funniness of things becomes more complicated when you spend hours working on the code of it (but maybe it would feel the same with the sadness of things, or the profoundness of things, or the importance of things), and that its hard to start with a toy or object without knowing the purpose of the program. We were talking about several toys to use from the beginning, and it became more of an exercise in toy enhancement, which was frustrating. If we had a concept in the beginning, I think it would have been more interesting to find the appropriate toy, rather than the other way around. I was very happy with the idea we settled on though.. I think it was nice to do something ridiculous.<\/p>\n

The most challenging part was the serial communication. I was getting random readings sent from the arduino that seemed unaltered by the goings on of the switch. I finally got the advice to change what the arduino was sending from integers to characters, which eliminated the reading problem. The processing code seemed to be picking up a series of delayed readings at the wrong speed (or something, I don’t really understand), but I wanted a digital “1” or “0” read. So finally, asking arduino to send char’s at switch state changes and processing to look for them, solved the problem.<\/p>\n

CODE for ARDUINO ::<\/p>\n

#include <Boards.h>\r\n\r\n#include <Firmata.h>\r\n\r\n\u00a0int switchPin = 2; \u00a0 \u00a0 \u00a0\/\/ \u00a0digital input pin for a switch\r\n\r\n\u00a0int LED = 13; \u00a0 \/\/ \u00a0digital output pin for an LED\r\n\r\n\u00a0int switchState = 0; \u00a0 \u00a0\/\/ the state of the switch\r\n\r\n\u00a0char sendData = 0;\r\n\r\n\u00a0int pulseWidth = 5;\r\n\r\n\u00a0void setup() {\r\n\r\n\u00a0\u00a0 Serial.begin(9600);\r\n\r\n\u00a0\u00a0 pinMode(switchPin, INPUT);\/\/set the switch pin to be an input\r\n\r\n\u00a0\u00a0 pinMode(LED, OUTPUT);   \/\/ set the s LED pin to be an output\r\n\r\n\u00a0}\r\n\r\n\u00a0void loop() {\r\n\r\n\u00a0\u00a0 \/\/ read the switch input:\r\n\r\n\u00a0\u00a0 switchState = digitalRead(switchPin);\r\n\r\n\u00a0\u00a0 sendData = char(switchState);\r\n\r\n\u00a0\u00a0 Serial.println(sendData, DEC);\r\n\r\n\u00a0\u00a0 if (switchState == 1) {\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 \/\/ if the switch is closed:\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 for (int i = 0; i<100; i++){\u00a0\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 \u00a0 digitalWrite(LED, HIGH); \u00a0 \u00a0\/\/ turn on the \u00a0LED\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0delayMicroseconds(i);\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 \u00a0digitalWrite(LED, LOW);\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 \u00a0delayMicroseconds(i);\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 }\r\n\r\n\u00a0\u00a0 } else {\r\n\r\n\u00a0\u00a0 \u00a0 \/\/ if the switch is open:\r\n\r\n\u00a0\u00a0 \u00a0 digitalWrite(LED, LOW);\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0delayMicroseconds(pulseWidth);\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0digitalWrite(LED, HIGH);\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0delayMicroseconds(pulseWidth);\r\n\r\n\u00a0\u00a0 }\r\n\r\n\u00a0}<\/pre>\n

CODE for PROCESSING ::<\/p>\n

\u00a0\r\n\r\n\u00a0\u00a0import ddf.minim.*;\r\n\r\n\u00a0\u00a0import processing.serial.*;\r\n\r\n\u00a0\u00a0import cc.arduino.*;\r\n\r\n\u00a0\u00a0 Serial port;\u00a0\r\n\r\n\u00a0\u00a0 char val = '0';\u00a0\r\n\r\n\u00a0\u00a0 char oldVal = '0';\u00a0\r\n\r\n\u00a0\u00a0 Minim minim;\r\n\r\n\u00a0\u00a0 AudioPlayer groove;\r\n\r\n\u00a0\u00a0 void setup()\r\n\r\n\u00a0\u00a0 {\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0minim = new Minim(this);\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0groove = minim.loadFile(\"BustANut.aif\", 2048); \u00a0\r\n\r\n\u00a0\u00a0 \u00a0 println(Serial.list());\u00a0\r\n\r\n\u00a0\u00a0 \u00a0 port = new Serial(this, Serial.list()[0], 9600);\u00a0\r\n\r\n\u00a0\u00a0 \u00a0 size(10, 10);\r\n\r\n\u00a0\u00a0 \u00a0 minim = new Minim(this);\r\n\r\n\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0 void draw()\r\n\r\n\u00a0\u00a0 { \u00a0\r\n\r\n\u00a0\u00a0 \u00a0 println(val);\r\n\r\n\u00a0\u00a0 \u00a0 if (port.available() > 0) {\u00a0\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 val = port.lastChar();\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 println(val);\r\n\r\n\u00a0\u00a0 \u00a0 }\r\n\r\n\u00a0\u00a0 \u00a0 if (val == '1' && oldVal == '0') {\u00a0\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 groove.play();\u00a0\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 oldVal = '1';\r\n\r\n\u00a0\u00a0 \u00a0 }\r\n\r\n\u00a0\u00a0 \u00a0 else if (val == '0' && oldVal == '1') {\u00a0\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 \u00a0groove.pause();\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0 \u00a0oldVal = '0';\r\n\r\n\u00a0\u00a0 \u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0 void stop() {\r\n\r\n\u00a0\u00a0 \u00a0 groove.close();\r\n\r\n\u00a0\u00a0 \u00a0 minim.stop();\r\n\r\n\u00a0\u00a0 \u00a0 super.stop();\r\n\r\n\u00a0\u00a0 }<\/pre>\n

I would love to get to the bottom of the benefits and challenges of the different styles of serial communication, particularly char vs. int vs. ASCII character vs. DEC data types. I know this has to do with bytes and bits and data transfer speeds, and its because I still don’t have a solid grasp on this that I am having a hard time with it.
\n[wpvideo joZ6aNAD]<\/p>\n

The squirrel’s switchstate was on when the mouth was shut (nut was cracked) and it said “Eat some nuts.” or “How those nuts taste?” or told a joke like “A guy walks into the psychiatrist’s office with his privates wrapped in saran wrap, and the shrink says, ‘I can clearly see your nuts.'”<\/p>\n

bubum ch<\/p>\n","protected":false},"excerpt":{"rendered":"

Allison and I made a squirrel nut cracker that cracks jokes as it cracks nuts. A few things I would like to remember: the funniness of things becomes more complicated when you spend hours working on the code of it (but maybe it would feel the same with the sadness of things, or the profoundness […]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24,1],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-physical-computing","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/comments?post=68"}],"version-history":[{"count":22,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions"}],"predecessor-version":[{"id":1125,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/posts\/68\/revisions\/1125"}],"wp:attachment":[{"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/media?parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/categories?post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/tags?post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}