{"id":18,"date":"2006-10-29T06:15:48","date_gmt":"2006-10-29T11:15:48","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/processing\/18"},"modified":"2007-08-06T23:22:11","modified_gmt":"2007-08-07T04:22:11","slug":"sudden-motion-sensor-on-the-macbook","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/Processing\/18\/","title":{"rendered":"Sudden Motion Sensor on the Macbook"},"content":{"rendered":"<p>Dan Shiffman&#8217;s written a nice <a href=\"http:\/\/www.shiffman.net\/2006\/10\/28\/processingsms\/\">library to access the sudden motion sensor<\/a> on the macbook and macbook pro. He based it on a couple other open source libraries. Below is a code sample for it that draws a plane on the screen and moves it as you tilt the computer.<\/p>\n<p><!--more--><\/p>\n<pre>\n\/*\n  smsTilt\n \n Takes the values from the sudden motion sensor on a powerbook\n (assuming the machine has the SMS in it: macBookPro, macBook, later Powerbooks)\n and uses it to set the position, attitude, and color of a rectangle on the screen.\n \n To use it, tilt your powerbook around.\n \n created 28 October 2006\n by Tom Igoe\n \n *\/\n\n\nimport sms.*;\nint graphPosition = 0;                \/\/ horizontal position of the graph\nint[] vals = new int[3];              \/\/ raw values from the sensor\nint[] maximum = new int[3];           \/\/ maximum value sensed\nint[] minimum = new int[3];           \/\/ minimum value sensed\nint[] range = new int[3];             \/\/ total range sensed\nfloat[] attitude = new float[3];      \/\/ current value as a attitude of window height\nfloat[] position = new float[3];      \/\/ current value as a attitude of window height\nfloat rectSize;                       \/\/ rectangle size\n\n\nvoid setup () {\n  \/\/ draw the window:\n  size(400, 400, P3D); \n  \/\/ set the size of the rectangle:\n  rectSize = width\/4;  \n  \/\/ set the background color:\n  background(0);\n  \/\/ set the maximum and minimum values:\n  for (int i = 0; i < 3; i++) {\n    maximum[i] = 400;\n    minimum[i] = -400;\n    \/\/ calculate the total current range:\n    range[i] = maximum[i] - minimum[i];\n  }\n}\n\nvoid draw () {\n  background(0);\n  \/\/ get the values:\n  vals = Unimotion.getSMSArray();\n  \/\/ check to see if you have new peak values:\n  checkLimits();\n  \/\/ draw the plane:\n  tilt();\n\n\n}\nvoid checkLimits() {\n  for (int i = 0; i < 3; i++) {\n    \/\/ calculate the current attitude as a percentage of 2*PI, \n    \/\/ based on the current range:\n    attitude[i] = PI * float(vals[i] - minimum[i]) \/float(range[i]) ;\n  }\n  \n  \/\/ calculate the current position as a percentage of the window height, \n  \/\/ based on the current range:\n  position[0] = width * float(vals[0] - minimum[0]) \/float(range[0]);\n  position[1] = height * float(vals[1] - minimum[1]) \/float(range[1]) + minimum[1];\n  position[2] = height * float(vals[2] - minimum[2]) \/float(range[2]);\n}\n\nvoid tilt() {\n  \/\/ position:\n  translate(position[0], position[2], position[1] );\n  \/\/ pi\/2, 0, 0 is level plane\n  \/\/ X is front-to-back\n  rotateX(attitude[1]);\n  \/\/ Y is left-to-right\n  rotateY(attitude[0] + PI\/2);\n  \/\/ Z is pivot\n  rotateZ(attitude[2]+PI\/4);\n  \/\/ draw the rect:\n  fill(position[0],position[1],position[2]);\n  rect(-rectSize, -rectSize, rectSize*2, rectSize*2);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dan Shiffman&#8217;s written a nice library to access the sudden motion sensor on the macbook and macbook pro&#8230;.  Below is a code sample for it that draws a plane on the screen and moves it as you tilt the computer.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-Processing"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}