daily pastebin goal
32%
SHARE
TWEET

Arduino_Genesis_adapter_(with Konamicode)

a guest Sep 2nd, 2016 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Sega Megadrive/Genesis 6-button controller adapter by Kurg 2.9.2016
  2. //Tested with Arduino Pro Micro (ATmega32U4, 5V) from Ebay ($4)
  3. //Selected board from Arduino IDE: Arduino Leonardo
  4. //Joystick2 library from https://github.com/MHeironimus/ArduinoJoystickLibrary
  5. //https://www.cs.cmu.edu/~chuck/infopg/segasix.txt
  6.  
  7. #include "Joystick2.h"
  8.  
  9. //DB9 pin (9-pin D-connector)
  10. //Select low  Select high   3rd pulse
  11. //1=Up        Up            Z
  12. //2=Down      Down          Y
  13. //3=Ground    Left          X
  14. //4=Ground    Right
  15. //5=+5V
  16. //6=A         B
  17. //7=Control("Select")
  18. //8=Ground
  19. //9=Start     C
  20.  
  21. #define EVENTS_TOTAL 4+6+1 //4 directions, 6 fire-buttons and Start
  22. #define INPUT_PINS_TOTAL 6
  23.  
  24. //DB9 (port1):                      1   2   3   4   6   9
  25. const uint8_t inputPinsPort1[] =  {10, 16, 14, 15,  3, A1};
  26. #define VCC_PORT1 A0 //DB9 (port1) pin 5 //comment out if not connected to IO-pin
  27. #define MODE_SELECT_PORT1 A3 //DB9 (port1) pin 7
  28. //DB9 (port 2) pin 8 = GND
  29.  
  30. //DB9 (port2):                      1   2   3   4   6   9
  31. const uint8_t inputPinsPort2[] =  { 5,  6,  7,  8,  4, A2};
  32. #define VCC_PORT2 9 //DB9 (port 2) pin 5 //comment out if not connected to IO-pin
  33. #define MODE_SELECT_PORT2 2 //DB9 (port2) pin 7
  34. //DB9 (port 2) pin 8 = GND
  35.  
  36. uint8_t lastStatusPort1[EVENTS_TOTAL];
  37. uint8_t newStatusPort1[EVENTS_TOTAL];
  38. uint8_t lastStatusPort2[EVENTS_TOTAL];
  39. uint8_t newStatusPort2[EVENTS_TOTAL];
  40.  
  41.  
  42. void modeSelect(uint8_t m) {
  43.   digitalWrite(MODE_SELECT_PORT1, m);
  44.   digitalWrite(MODE_SELECT_PORT2, m);
  45.   delayMicroseconds(20);
  46. }
  47.  
  48. void releaseAll(uint8_t j) {
  49.   delay(20);
  50.   Joystick[j].setButton(0, 0); //BUTTON1
  51.   Joystick[j].setButton(1, 0); //BUTTON2
  52.   Joystick[j].setButton(2, 0); //BUTTON3
  53.   Joystick[j].setButton(3, 0); //BUTTON4
  54.   Joystick[j].setButton(4, 0); //BUTTON5
  55.   Joystick[j].setButton(5, 0); //BUTTON6
  56.   Joystick[j].setButton(6, 0); //BUTTON7
  57.   Joystick[j].setYAxis(0);
  58.   Joystick[j].setXAxis(0);
  59.   Joystick[j].sendState();
  60.   delay(20);
  61. }
  62.  
  63. void KonamiCode(uint8_t j, uint8_t swap_ab = 0) {
  64.   //https://en.wikipedia.org/wiki/Konami_Code
  65.   //https://en.wikipedia.org/wiki/List_of_Konami_code_games
  66.   //UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT, B, A
  67.   releaseAll(j);
  68.   delay(200);
  69.   Joystick[j].setYAxis(-127); Joystick[j].sendState(); //UP
  70.   releaseAll(j);
  71.   Joystick[j].setYAxis(-127); Joystick[j].sendState(); //UP
  72.   releaseAll(j);
  73.   Joystick[j].setYAxis(127); Joystick[j].sendState(); //DOWN
  74.   releaseAll(j);
  75.   Joystick[j].setYAxis(127); Joystick[j].sendState(); //DOWN
  76.   releaseAll(j);
  77.   Joystick[j].setXAxis(-127); Joystick[j].sendState(); //LEFT
  78.   releaseAll(j);
  79.   Joystick[j].setXAxis(127); Joystick[j].sendState(); //RIGHT
  80.   releaseAll(j);
  81.   Joystick[j].setXAxis(-127); Joystick[j].sendState(); //LEFT
  82.   releaseAll(j);
  83.   Joystick[j].setXAxis(127); Joystick[j].sendState(); //RIGHT
  84.   releaseAll(j);
  85.   if (swap_ab) {
  86.     Joystick[j].setButton(0, 1); Joystick[j].sendState(); //BUTTON1 (A)
  87.     releaseAll(j);
  88.     Joystick[j].setButton(1, 1); Joystick[j].sendState(); //BUTTON2 (B)
  89.     releaseAll(j);
  90.   } else {
  91.     Joystick[j].setButton(1, 1); Joystick[j].sendState(); //BUTTON2 (B)
  92.     releaseAll(j);
  93.     Joystick[j].setButton(0, 1); Joystick[j].sendState(); //BUTTON1 (A)
  94.     releaseAll(j);
  95.   }
  96.   delay(100);
  97. }
  98.  
  99.  
  100. void setup() {
  101.  
  102.   //clear statusarrays (1=OFF, 0=ON)
  103.   for (uint8_t i = 0; i < EVENTS_TOTAL; i++) {
  104.     lastStatusPort1[i] = 1;
  105.     newStatusPort1[i] = 1;
  106.     lastStatusPort2[i] = 1;
  107.     newStatusPort2[i] = 1;
  108.   }
  109.  
  110.   #if defined(VCC_PORT1) && defined(VCC_PORT2)
  111.   pinMode(VCC_PORT1, OUTPUT);
  112.   pinMode(VCC_PORT2, OUTPUT);
  113.   digitalWrite(VCC_PORT1, HIGH);
  114.   digitalWrite(VCC_PORT2, HIGH);
  115.   #endif
  116.  
  117.   for (int i=0; i < INPUT_PINS_TOTAL; i++) {
  118.     pinMode(inputPinsPort1[i], INPUT_PULLUP);
  119.     pinMode(inputPinsPort2[i], INPUT_PULLUP);
  120.   }
  121.  
  122.   pinMode(MODE_SELECT_PORT1, OUTPUT);
  123.   pinMode(MODE_SELECT_PORT2, OUTPUT);
  124.   modeSelect(HIGH);
  125.  
  126.   Joystick[0].begin(false);
  127.   Joystick[1].begin(false);
  128.  
  129. }
  130.  
  131.  
  132. void read3buttons() {
  133.  
  134.   modeSelect(LOW);
  135.  
  136.   newStatusPort1[4] = digitalRead(inputPinsPort1[4]); //A1
  137.   newStatusPort1[7] = digitalRead(inputPinsPort1[5]); //Start1
  138.   newStatusPort2[4] = digitalRead(inputPinsPort2[4]); //A2
  139.   newStatusPort2[7] = digitalRead(inputPinsPort2[5]); //Start2  
  140.  
  141.   modeSelect(HIGH);
  142.  
  143.   for (uint8_t i=0; i < 4; i++) {
  144.     newStatusPort1[i] = digitalRead(inputPinsPort1[i]); //AXES1
  145.     newStatusPort2[i] = digitalRead(inputPinsPort2[i]); //AXES2
  146.   }
  147.   newStatusPort1[5] = digitalRead(inputPinsPort1[4]); //B1
  148.   newStatusPort1[6] = digitalRead(inputPinsPort1[5]); //C1
  149.   newStatusPort2[5] = digitalRead(inputPinsPort2[4]); //B2
  150.   newStatusPort2[6] = digitalRead(inputPinsPort2[5]); //C2
  151.  
  152. }
  153.  
  154.  
  155. uint8_t flag1 = 0;
  156. uint8_t flag2 = 0;
  157.  
  158. void loop() {
  159.  
  160.   read3buttons();
  161.  
  162.   //read X,Y,Z
  163.   modeSelect(LOW);
  164.   modeSelect(HIGH);
  165.   modeSelect(LOW);
  166.   modeSelect(HIGH);
  167.   newStatusPort1[8] = digitalRead(inputPinsPort1[2]); //X1
  168.   newStatusPort1[9] = digitalRead(inputPinsPort1[1]); //Y1
  169.   newStatusPort1[10] = digitalRead(inputPinsPort1[0]); //Z1
  170.   newStatusPort2[8] = digitalRead(inputPinsPort2[2]); //X2
  171.   newStatusPort2[9] = digitalRead(inputPinsPort2[1]); //Y2
  172.   newStatusPort2[10] = digitalRead(inputPinsPort2[0]); //Z2
  173.   delayMicroseconds(1000);
  174.  
  175.   //check for changes - do not raise a flag if nothing changes
  176.   for (uint8_t i=0; i < EVENTS_TOTAL; i++) {
  177.     if (newStatusPort1[i] != lastStatusPort1[i]) {
  178.       lastStatusPort1[i] = newStatusPort1[i];
  179.       flag1 = 1;
  180.     }
  181.     if (newStatusPort2[i] != lastStatusPort2[i]) {
  182.       lastStatusPort2[i] = newStatusPort2[i];
  183.       flag2 = 1;
  184.     }
  185.   }
  186.  
  187.   if (flag1) {
  188.     Joystick[0].setYAxis(0);
  189.     Joystick[0].setXAxis(0);
  190.     if (!newStatusPort1[0]) Joystick[0].setYAxis(-127); //UP
  191.     if (!newStatusPort1[1]) Joystick[0].setYAxis(127); //DOWN
  192.     if (!newStatusPort1[2]) Joystick[0].setXAxis(-127); //LEFT
  193.     if (!newStatusPort1[3]) Joystick[0].setXAxis(127); //RIGHT
  194.     Joystick[0].setButton(0, !newStatusPort1[4]); //BUTTON1
  195.     Joystick[0].setButton(1, !newStatusPort1[5]); //BUTTON2
  196.     Joystick[0].setButton(2, !newStatusPort1[6]); //BUTTON3
  197.     Joystick[0].setButton(3, !newStatusPort1[7]); //BUTTON4
  198.     Joystick[0].setButton(4, !newStatusPort1[8]); //BUTTON5
  199.     Joystick[0].setButton(5, !newStatusPort1[9]); //BUTTON6
  200.     Joystick[0].setButton(6, !newStatusPort1[10]); //BUTTON7
  201.     Joystick[0].sendState();
  202.     flag1 = 0;
  203.     if (!newStatusPort1[4] && !newStatusPort1[5] && !newStatusPort1[6] && !newStatusPort1[0]) KonamiCode(0,0);
  204.     if (!newStatusPort1[4] && !newStatusPort1[5] && !newStatusPort1[6] && !newStatusPort1[1]) KonamiCode(0,1);
  205.   }
  206.    
  207.   if (flag2) {
  208.     Joystick[1].setYAxis(0);
  209.     Joystick[1].setXAxis(0);
  210.     if (!newStatusPort2[0]) Joystick[1].setYAxis(-127); //UP
  211.     if (!newStatusPort2[1]) Joystick[1].setYAxis(127); //DOWN
  212.     if (!newStatusPort2[2]) Joystick[1].setXAxis(-127); //LEFT
  213.     if (!newStatusPort2[3]) Joystick[1].setXAxis(127); //RIGHT
  214.     Joystick[1].setButton(0, !newStatusPort2[4]); //BUTTON1
  215.     Joystick[1].setButton(1, !newStatusPort2[5]); //BUTTON2
  216.     Joystick[1].setButton(2, !newStatusPort2[6]); //BUTTON3
  217.     Joystick[1].setButton(3, !newStatusPort2[7]); //BUTTON4
  218.     Joystick[1].setButton(4, !newStatusPort2[8]); //BUTTON5
  219.     Joystick[1].setButton(5, !newStatusPort2[9]); //BUTTON6
  220.     Joystick[1].setButton(6, !newStatusPort2[10]); //BUTTON7
  221.     Joystick[1].sendState();
  222.     flag2 = 0;
  223.     if (!newStatusPort2[4] && !newStatusPort2[5] && !newStatusPort2[6] && !newStatusPort2[0]) KonamiCode(0,0);
  224.     if (!newStatusPort2[4] && !newStatusPort2[5] && !newStatusPort2[6] && !newStatusPort2[1]) KonamiCode(0,1);
  225.   }
  226.  
  227.  
  228. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top