daily pastebin goal
32%
SHARE
TWEET

Arduino_NES_adapter

a guest Sep 3rd, 2016 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Description:  Interfacing a NES controller with a PC with an Arduino.
  3. Coded by: Prodigity
  4. Date:   1 December 2011
  5. Revision: V0.93 (beta)
  6. Modified by:    Matt Booth (20 December 2014)
  7. */
  8. //Adapted for USB (ATmega32U4, 5V) by Kurg 3.9.2016
  9.  
  10. #include "Joystick2.h"
  11.  
  12. #define EVENTS_TOTAL 4+2+2 //4 directions, 2 fire-buttons and Start + Select
  13.  
  14. uint8_t lastStatusPort1[EVENTS_TOTAL];
  15. uint8_t newStatusPort1[EVENTS_TOTAL];
  16. uint8_t lastStatusPort2[EVENTS_TOTAL];
  17. uint8_t newStatusPort2[EVENTS_TOTAL];
  18.  
  19.  
  20. #define CLOCK1 7
  21. #define LATCH1 8
  22. #define DATA1 9
  23.  
  24. #define CLOCK2 2
  25. #define LATCH2 3
  26. #define DATA2 4
  27.  
  28. // http://www.mit.edu/~tarvizo/nes-controller.html
  29.  
  30.  
  31. void setup() {
  32.  
  33.   //clear statusarrays (1=OFF, 0=ON)
  34.   for (uint8_t i = 0; i < EVENTS_TOTAL; i++) {
  35.     lastStatusPort1[i] = 1;
  36.     newStatusPort1[i] = 1;
  37.     lastStatusPort2[i] = 1;
  38.     newStatusPort2[i] = 1;
  39.   }
  40.  
  41.   pinMode(LATCH1, OUTPUT);
  42.   pinMode(CLOCK1, OUTPUT);
  43.   pinMode(DATA1, INPUT);
  44.   pinMode(LATCH2, OUTPUT);
  45.   pinMode(CLOCK2, OUTPUT);
  46.   pinMode(DATA2, INPUT);
  47.  
  48.   Joystick[0].begin(false);
  49.   Joystick[1].begin(false);
  50. }
  51.  
  52.  
  53. uint8_t flag1 = 0;
  54. uint8_t flag2 = 0;
  55.  
  56.  
  57. void loop() {
  58.  
  59.   ReadNESjoy();
  60.  
  61.   //check for changes - do not raise a flag if nothing changes
  62.   for (uint8_t i=0; i < EVENTS_TOTAL; i++) {
  63.     if (newStatusPort1[i] != lastStatusPort1[i]) {
  64.       lastStatusPort1[i] = newStatusPort1[i];
  65.       flag1 = 1;
  66.     }
  67.     if (newStatusPort2[i] != lastStatusPort2[i]) {
  68.       lastStatusPort2[i] = newStatusPort2[i];
  69.       flag2 = 1;
  70.     }
  71.   }
  72.  
  73.   if (flag1) {
  74.     Joystick[0].setYAxis(0);
  75.     Joystick[0].setXAxis(0);
  76.     if (!newStatusPort1[4]) Joystick[0].setYAxis(-127); //UP
  77.     if (!newStatusPort1[5]) Joystick[0].setYAxis(127); //DOWN
  78.     if (!newStatusPort1[6]) Joystick[0].setXAxis(-127); //LEFT
  79.     if (!newStatusPort1[7]) Joystick[0].setXAxis(127); //RIGHT
  80.     Joystick[0].setButton(0, !newStatusPort1[0]); //BUTTON1 (A)
  81.     Joystick[0].setButton(1, !newStatusPort1[1]); //BUTTON2 (B)
  82.     Joystick[0].setButton(2, !newStatusPort1[2]); //BUTTON3 (Select)
  83.     Joystick[0].setButton(3, !newStatusPort1[3]); //BUTTON4 (Start)
  84.   }
  85.  
  86.   if (flag2) {
  87.     Joystick[1].setYAxis(0);
  88.     Joystick[1].setXAxis(0);
  89.     if (!newStatusPort2[4]) Joystick[1].setYAxis(-127); //UP
  90.     if (!newStatusPort2[5]) Joystick[1].setYAxis(127); //DOWN
  91.     if (!newStatusPort2[6]) Joystick[1].setXAxis(-127); //LEFT
  92.     if (!newStatusPort2[7]) Joystick[1].setXAxis(127); //RIGHT
  93.     Joystick[1].setButton(0, !newStatusPort2[0]); //BUTTON1 (A)
  94.     Joystick[1].setButton(1, !newStatusPort2[1]); //BUTTON2 (B)
  95.     Joystick[1].setButton(2, !newStatusPort2[2]); //BUTTON3 (Select)
  96.     Joystick[1].setButton(3, !newStatusPort2[3]); //BUTTON4 (Start)
  97.   }
  98.  
  99.   if (flag1) Joystick[0].sendState();
  100.   if (flag2) Joystick[1].sendState();
  101.   flag1 = 0;
  102.   flag2 = 0;
  103.  
  104. }
  105.  
  106. #define latchlow digitalWrite(LATCH1, LOW); digitalWrite(LATCH2, LOW);
  107. #define latchhigh digitalWrite(LATCH1, HIGH); digitalWrite(LATCH2, HIGH)
  108. #define clocklow digitalWrite(CLOCK1, LOW); digitalWrite(CLOCK2, LOW)
  109. #define clockhigh digitalWrite(CLOCK1, HIGH); digitalWrite(CLOCK2, HIGH)
  110. #define wait delayMicroseconds(12)
  111.  
  112. void ReadNESjoy() {
  113.   latchlow;
  114.   clocklow;
  115.   latchhigh;
  116.   wait;
  117.   latchlow;
  118.  
  119.   for (int i = 0; i < 8; i++) {
  120.      newStatusPort1[i] = digitalRead(DATA1);
  121.      newStatusPort2[i] = digitalRead(DATA2);
  122.      clockhigh;
  123.      wait;
  124.      clocklow;
  125.      wait;
  126.   }
  127. }
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