daily pastebin goal
32%
SHARE
TWEET

Arduino_Genesis_adapter

a guest Sep 2nd, 2016 121 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.  
  49. void setup() {
  50.  
  51.   //clear statusarrays (1=OFF, 0=ON)
  52.   for (uint8_t i = 0; i < EVENTS_TOTAL; i++) {
  53.     lastStatusPort1[i] = 1;
  54.     newStatusPort1[i] = 1;
  55.     lastStatusPort2[i] = 1;
  56.     newStatusPort2[i] = 1;
  57.   }
  58.  
  59.   #if defined(VCC_PORT1) && defined(VCC_PORT2)
  60.   pinMode(VCC_PORT1, OUTPUT);
  61.   pinMode(VCC_PORT2, OUTPUT);
  62.   digitalWrite(VCC_PORT1, HIGH);
  63.   digitalWrite(VCC_PORT2, HIGH);
  64.   #endif
  65.  
  66.   for (int i=0; i < INPUT_PINS_TOTAL; i++) {
  67.     pinMode(inputPinsPort1[i], INPUT_PULLUP);
  68.     pinMode(inputPinsPort2[i], INPUT_PULLUP);
  69.   }
  70.  
  71.   pinMode(MODE_SELECT_PORT1, OUTPUT);
  72.   pinMode(MODE_SELECT_PORT2, OUTPUT);
  73.   modeSelect(HIGH);
  74.  
  75.   Joystick[0].begin(false);
  76.   Joystick[1].begin(false);
  77.  
  78. }
  79.  
  80.  
  81. void read3buttons() {
  82.  
  83.   modeSelect(LOW);
  84.  
  85.   newStatusPort1[4] = digitalRead(inputPinsPort1[4]); //A1
  86.   newStatusPort1[7] = digitalRead(inputPinsPort1[5]); //Start1
  87.   newStatusPort2[4] = digitalRead(inputPinsPort2[4]); //A2
  88.   newStatusPort2[7] = digitalRead(inputPinsPort2[5]); //Start2  
  89.  
  90.   modeSelect(HIGH);
  91.  
  92.   for (uint8_t i=0; i < 4; i++) {
  93.     newStatusPort1[i] = digitalRead(inputPinsPort1[i]); //AXES1
  94.     newStatusPort2[i] = digitalRead(inputPinsPort2[i]); //AXES2
  95.   }
  96.   newStatusPort1[5] = digitalRead(inputPinsPort1[4]); //B1
  97.   newStatusPort1[6] = digitalRead(inputPinsPort1[5]); //C1
  98.   newStatusPort2[5] = digitalRead(inputPinsPort2[4]); //B2
  99.   newStatusPort2[6] = digitalRead(inputPinsPort2[5]); //C2
  100.  
  101. }
  102.  
  103.  
  104. uint8_t flag1 = 0;
  105. uint8_t flag2 = 0;
  106.  
  107. void loop() {
  108.  
  109.   read3buttons();
  110.  
  111.   //read X,Y,Z
  112.   modeSelect(LOW);
  113.   modeSelect(HIGH);
  114.   modeSelect(LOW);
  115.   modeSelect(HIGH);
  116.   newStatusPort1[8] = digitalRead(inputPinsPort1[2]); //X1
  117.   newStatusPort1[9] = digitalRead(inputPinsPort1[1]); //Y1
  118.   newStatusPort1[10] = digitalRead(inputPinsPort1[0]); //Z1
  119.   newStatusPort2[8] = digitalRead(inputPinsPort2[2]); //X2
  120.   newStatusPort2[9] = digitalRead(inputPinsPort2[1]); //Y2
  121.   newStatusPort2[10] = digitalRead(inputPinsPort2[0]); //Z2
  122.   delayMicroseconds(1000);
  123.  
  124.   //check for changes - do not raise a flag if nothing changes
  125.   for (uint8_t i=0; i < EVENTS_TOTAL; i++) {
  126.     if (newStatusPort1[i] != lastStatusPort1[i]) {
  127.       lastStatusPort1[i] = newStatusPort1[i];
  128.       flag1 = 1;
  129.     }
  130.     if (newStatusPort2[i] != lastStatusPort2[i]) {
  131.       lastStatusPort2[i] = newStatusPort2[i];
  132.       flag2 = 1;
  133.     }
  134.   }
  135.  
  136.   if (flag1) {
  137.     Joystick[0].setYAxis(0);
  138.     Joystick[0].setXAxis(0);
  139.     if (!newStatusPort1[0]) Joystick[0].setYAxis(-127); //UP
  140.     if (!newStatusPort1[1]) Joystick[0].setYAxis(127); //DOWN
  141.     if (!newStatusPort1[2]) Joystick[0].setXAxis(-127); //LEFT
  142.     if (!newStatusPort1[3]) Joystick[0].setXAxis(127); //RIGHT
  143.     Joystick[0].setButton(0, !newStatusPort1[4]); //BUTTON1
  144.     Joystick[0].setButton(1, !newStatusPort1[5]); //BUTTON2
  145.     Joystick[0].setButton(2, !newStatusPort1[6]); //BUTTON3
  146.     Joystick[0].setButton(3, !newStatusPort1[7]); //BUTTON4
  147.     Joystick[0].setButton(4, !newStatusPort1[8]); //BUTTON5
  148.     Joystick[0].setButton(5, !newStatusPort1[9]); //BUTTON6
  149.     Joystick[0].setButton(6, !newStatusPort1[10]); //BUTTON7
  150.     Joystick[0].sendState();
  151.     flag1 = 0;
  152.   }
  153.    
  154.   if (flag2) {
  155.     Joystick[1].setYAxis(0);
  156.     Joystick[1].setXAxis(0);
  157.     if (!newStatusPort2[0]) Joystick[1].setYAxis(-127); //UP
  158.     if (!newStatusPort2[1]) Joystick[1].setYAxis(127); //DOWN
  159.     if (!newStatusPort2[2]) Joystick[1].setXAxis(-127); //LEFT
  160.     if (!newStatusPort2[3]) Joystick[1].setXAxis(127); //RIGHT
  161.     Joystick[1].setButton(0, !newStatusPort2[4]); //BUTTON1
  162.     Joystick[1].setButton(1, !newStatusPort2[5]); //BUTTON2
  163.     Joystick[1].setButton(2, !newStatusPort2[6]); //BUTTON3
  164.     Joystick[1].setButton(3, !newStatusPort2[7]); //BUTTON4
  165.     Joystick[1].setButton(4, !newStatusPort2[8]); //BUTTON5
  166.     Joystick[1].setButton(5, !newStatusPort2[9]); //BUTTON6
  167.     Joystick[1].setButton(6, !newStatusPort2[10]); //BUTTON7
  168.     Joystick[1].sendState();
  169.     flag2 = 0;
  170.   }
  171.  
  172.  
  173. }
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