{"id":943,"date":"2012-01-16T22:12:01","date_gmt":"2012-01-17T02:12:01","guid":{"rendered":"http:\/\/biosrhythm.com\/?p=943"},"modified":"2013-07-29T02:01:46","modified_gmt":"2013-07-29T02:01:46","slug":"code-for-diy-nes-gamepad-to-arduino-to-ipad-adapter","status":"publish","type":"post","link":"https:\/\/biosrhythm.com\/?p=943","title":{"rendered":"Code for DIY NES Game Pad to Arduino to iPad Adapter"},"content":{"rendered":"<p>As promised, here&#8217;s the code to connect your NES game pad to an Arduino via USB to an iPad emulating an iCade controller (whew!).<\/p>\n<p><code><\/p>\n<pre>\/\/ NES to USB iCade emulation to iPad\n\/\/ Paul Rickards\n\/\/ rickards@gmail.com\n\/\/ January 15, 2012\n\/\/ Tested with Arduino 1.0 IDE\n\/\/ Add V-USB for Arduino to your Libraries folder\n\/\/ http:\/\/code.google.com\/p\/vusb-for-arduino\/\n\/\/\n\/\/ Changes I made to the library (usbconfig.h):\n\/\/ * changed USB_CFG_DMINUS_BIT to 3\n\/\/ * commented out defintions of USB_CFG_PULLUP_IOPORTNAME and USB_CFG_PULLUP_BIT\n\/\/\n\/\/ Arduino pin 2 = USB+\n\/\/ Arduino pin 3 = USB-\n\/\/ Don't forget the small circuit (3 resistors and 2 diodes) needed to connect\n\/\/ USB to the Arduino. See the library for more info.\n\/\/\n\/\/ Other controllers are possible. I first designed it for use with an Atari\/C64 joystick\n\/\/ The pins needed to be debounced (the bounce.h library will not work with the V-USB stack)\n\/\/ and I needed more buttons on the joystick so I scrapped the idea.\n\n#include \"UsbKeyboard.h\"\n\n\/\/ Define NES pins\nint latch=8;\nint clock=9;\nint datin=10;\n\nbool t = 1, prev_nes[8] = { 1,1,1,1,1,1,1,1 };\n\n\/\/ iCade key mapping http:\/\/www.ionaudio.com\/downloads\/iCade_Dev_Resource_v1.3.pdf\n\/\/ NES controller mapping      A      B    select start   up    down   left   right\nconst int key_press[8]   = { KEY_O, KEY_L, KEY_Y, KEY_H, KEY_W, KEY_X, KEY_A, KEY_D };\nconst int key_release[8] = { KEY_G, KEY_V, KEY_T, KEY_R, KEY_E, KEY_Z, KEY_Q, KEY_C };\n\nvoid setup() {\n  \/\/ Setup NES controller\n  pinMode(latch,OUTPUT);\n  pinMode(clock,OUTPUT);\n  pinMode(datin,INPUT);\n  digitalWrite(latch,HIGH);\n  digitalWrite(clock,HIGH);\n}\n\nvoid loop() {\n  UsbKeyboard.update();\n\n  digitalWrite(latch,LOW);\n  digitalWrite(clock,LOW);\n  digitalWrite(latch,HIGH);\n  delayMicroseconds(2);\n  digitalWrite(latch,LOW);\n\n  for (int i=0; i<8; i++) {\n    t = digitalRead(datin);\n    if (prev_nes[i] != t) { \/\/ button changed\n      if (t) UsbKeyboard.sendKeyStroke(key_release[i]);\n      else   UsbKeyboard.sendKeyStroke(key_press[i]);\n      prev_nes[i] = t;\n    }\n    digitalWrite(clock,LOW);\n    delayMicroseconds(2);\n    digitalWrite(clock,HIGH);\n    delayMicroseconds(2);\n  } \n}\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As promised, here&#8217;s the code to connect your NES game pad to an Arduino via USB to an iPad emulating an iCade controller (whew!). \/\/ NES to USB iCade emulation to iPad \/\/ Paul Rickards \/\/ rickards@gmail.com \/\/ January 15, 2012 \/\/ Tested with Arduino 1.0 IDE \/\/ Add V-USB for Arduino to your Libraries [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,15],"tags":[],"_links":{"self":[{"href":"https:\/\/biosrhythm.com\/index.php?rest_route=\/wp\/v2\/posts\/943"}],"collection":[{"href":"https:\/\/biosrhythm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/biosrhythm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/biosrhythm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/biosrhythm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=943"}],"version-history":[{"count":2,"href":"https:\/\/biosrhythm.com\/index.php?rest_route=\/wp\/v2\/posts\/943\/revisions"}],"predecessor-version":[{"id":1048,"href":"https:\/\/biosrhythm.com\/index.php?rest_route=\/wp\/v2\/posts\/943\/revisions\/1048"}],"wp:attachment":[{"href":"https:\/\/biosrhythm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/biosrhythm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/biosrhythm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}