{"id":111,"date":"2014-09-29T23:00:46","date_gmt":"2014-09-29T14:00:46","guid":{"rendered":"http:\/\/nantoka.filmm.info\/blog\/?p=111"},"modified":"2014-09-30T07:52:15","modified_gmt":"2014-09-29T22:52:15","slug":"unity-uniduino%e3%81%ae%e5%80%a4%e3%82%92%e8%a8%88%e7%ae%97","status":"publish","type":"post","link":"https:\/\/nantoka.filmm.info\/blog\/?p=111","title":{"rendered":"Unity-Uniduino\u306e\u5024\u3092\u8a08\u7b97"},"content":{"rendered":"<p>\u30ef\u30fc\u30af\u30b7\u30e7\u30c3\u30d7\u3067\u4f5c\u3063\u305f\u30b9\u30af\u30ea\u30d7\u30c8\u305d\u306e\uff14\u304f\u3089\u3044<\/p>\n<pre class=\"brush: csharp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\nusing UnityEngine;\r\nusing System.Collections;\r\nusing Uniduino; \/\/aruduino tukau nara hitsu you\r\n\r\npublic class analog_test : MonoBehaviour {\/\/ haifun wa tsukae nai no?\r\n\r\n\r\n\tpublic Arduino arduino;\/\/\r\n\t\r\n\tpublic int pin = 0;\/\/?\r\n\tpublic int pinValue;\/\/pin level get\r\n\tpublic int yValue;\r\n\tpublic int zValue;\r\n\r\n\tpublic int pinValueX;\r\n\tpublic int pinValueY;\r\n\tpublic int pinValueZ;\r\n\t\r\n\r\n\tpublic float spinSpeed;\/\/kaiten sokudo\r\n\tpublic GameObject rotBone;\/\/add my bone here\r\n\r\n\tpublic double rotZ = 0;\r\n\r\n\/\/\u3053\u3053\u3042\u3089\u30cd\u30c3\u30c8\u3067\u62fe\u3063\u305f\u30b9\u30af\u30ea\u30d7\u30c8\u7d44\u307f\u5408\u308f\u305b\u5927\u4f1a\r\n\tArrayList pinValueListX = new ArrayList();\r\n\tArrayList pinValueListY = new ArrayList();\r\n\tArrayList pinValueListZ = new ArrayList();\r\n\t\/\/ smoothing\u56de\u6570 \u591a\u3044\u307b\u3069\u6ed1\u3089\u304b\r\n\tint SMOOTHING_LENGTH = 20; \r\n\t\r\n\r\n\tvoid Start () {\/\/\u521d\u671f\u5316\u306a\u3069\r\n\t\t\r\n\t\tarduino = Arduino.global;\r\n\t\tarduino.Log = (s) =&gt; Debug.Log(&quot;Arduino: &quot; +s);\/\/log dakara huyou?\r\n\t\tarduino.Setup(ConfigurePins); \/\/call ConfigurePins function\r\n\t\t\r\n\t\t\/\/cube = GameObject.Find(&quot;Cube&quot;);\r\n\r\n\t}\r\n\t\r\n\tvoid ConfigurePins( )\r\n\t{\r\n\t\t\/\/arduino.pinMode(pin, PinMode.ANALOG); \/\/\r\n\t\t\/\/arduino.reportAnalog(pin, 1); \/\/pin number 1 is enable(1)\r\n\t\tarduino.pinMode(0, PinMode.ANALOG); \r\n\t\tarduino.reportAnalog(0, 1);\r\n\t\tarduino.pinMode(1, PinMode.ANALOG); \r\n\t\tarduino.reportAnalog(1, 1);\r\n\t\tarduino.pinMode(2, PinMode.ANALOG); \r\n\t\tarduino.reportAnalog(2, 1);\r\n\t\/\/\tspinSpeed = 0.1;\r\n\t}\r\n\r\n\r\n\r\n\t\r\n\tvoid Update () {\r\n\r\n\t\t\/\/\u4ee5\u4e0b\u3082\u8ffd\u52a0\r\n\r\n\t\tupdatePinValueList (\r\n\t\t\tarduino.analogRead (0),\r\n\t\t\tarduino.analogRead (1),\r\n\t\t\tarduino.analogRead (2)\r\n\t\t\t);\r\n\r\n\r\n\t\tpinValueX = getSmoothedValue (pinValueListX);\r\n\t\tpinValueY = getSmoothedValue (pinValueListY);\r\n\t\tpinValueZ = getSmoothedValue (pinValueListZ);\r\n\t\t\/\/\/\u8ffd\u52a0\r\n\r\n\t\tdouble degX = Mathf.Atan2(pinValueX - 536, pinValueZ - 500) \/ 3.14159 * 180.0 ;\/\/\u89d2\u5ea6\u3092\u52a0\u901f\u5ea6\u304b\u3089\r\n\t\tdouble degY = Mathf.Atan2(pinValueY - 530, pinValueZ - 500) \/ 3.14159 * 180.0 ;\/\/climb\r\n\t\t\/\/double degZ = Mathf.Atan2(pinValueZ - 500, pinValueX - 533) \/ 3.14159 * 180.0 ;\/\/\u89d2\u5ea6\u3092\u52a0\u901f\u5ea6\u304b\u3089\r\n\t\tdouble degZ = Mathf.Atan2(pinValueZ - 500, pinValueY - 530) \/ 3.14159 * 180.0 ;\/\/\u89d2\u5ea6\u3092\u52a0\u901f\u5ea6\u304b\u3089\r\n\r\n\t\tif(degX &gt; 1)\r\n\t\t{\r\n\t\t\trotZ = rotZ - (degX * 0.01);\r\n\t\t}if(degX &lt; -1){\r\n\t\t\trotZ = rotZ - (degX * 0.01);\r\n\t\t};\r\n\t}\r\n\r\n\r\n\tvoid updatePinValueList(int x, int y, int z){\r\n\t\tif (pinValueListX.Count &gt; SMOOTHING_LENGTH) {\r\n\t\t\tpinValueListX.RemoveAt(0);\t\r\n\t\t}\r\n\t\tif (pinValueListY.Count &gt; SMOOTHING_LENGTH) {\r\n\t\t\tpinValueListY.RemoveAt(0);\t\r\n\t\t}\r\n\t\tif (pinValueListZ.Count &gt; SMOOTHING_LENGTH) {\r\n\t\t\tpinValueListZ.RemoveAt(0);\t\r\n\t\t}\r\n\t\t\/\/ smoothing\u3057\u305f\u306e\u5024\r\n\t\tpinValueListX.Add (x);\r\n\t\tpinValueListY.Add (y);\r\n\t\tpinValueListZ.Add (z);\r\n\t}\r\n\t\r\n\tint getSmoothedValue(ArrayList list){\r\n\t\tif (list.Count &lt; SMOOTHING_LENGTH) {\r\n\t\t\treturn 0;\r\n\t\t}\r\n\t\t\r\n\t\tint sum = 0;\r\n\t\tfor (int i=0; i&lt;list.Count; i++){\r\n\t\t\tsum += (int)list&#x5B;i];\r\n\t\t}\r\n\t\treturn (int)(sum \/ SMOOTHING_LENGTH);\r\n\t}\r\n\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30ef\u30fc\u30af\u30b7\u30e7\u30c3\u30d7\u3067\u4f5c\u3063\u305f\u30b9\u30af\u30ea\u30d7\u30c8\u305d\u306e\uff14\u304f\u3089\u3044 using UnityEngine; using System.Collections; using Uniduino; \/\/aruduino tukau nara hits &hellip; <a href=\"https:\/\/nantoka.filmm.info\/blog\/?p=111\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">Unity-Uniduino\u306e\u5024\u3092\u8a08\u7b97<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-111","post","type-post","status-publish","format-standard","hentry","category-unity"],"_links":{"self":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=111"}],"version-history":[{"count":3,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":115,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=\/wp\/v2\/posts\/111\/revisions\/115"}],"wp:attachment":[{"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nantoka.filmm.info\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}