daily pastebin goal
33%
SHARE
TWEET

Arduino_NES_adapter_simple

a guest Sep 3rd, 2016 107 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. uint8_t lastStatusPort1 = 0xff;
  13. uint8_t newStatusPort1 = 0xff;
  14.  
  15.  
  16. const int latch = 8;
  17. const int clock = 9;
  18. const int data  = 7;
  19.  
  20. #define latchlow digitalWrite(latch, LOW)
  21. #define latchhigh digitalWrite(latch, HIGH)
  22. #define clocklow digitalWrite(clock, LOW)
  23. #define clockhigh digitalWrite(clock, HIGH)
  24. #define dataread digitalRead(data)
  25.  
  26. // http://www.mit.edu/~tarvizo/nes-controller.html
  27. #define wait delayMicroseconds(12)
  28.  
  29. byte output;
  30.  
  31. void setup() {
  32.  
  33. //  Serial.begin(9600);
  34.   pinMode(latch, OUTPUT);
  35.   pinMode(clock, OUTPUT);
  36.   pinMode(data, INPUT);
  37.  
  38.   Joystick[0].begin(false);
  39. }
  40.  
  41. void loop() {
  42.   output = 0;
  43.   ReadNESjoy();
  44.   //Serial.println(output,BIN);
  45.   newStatusPort1 = output;
  46.   if (lastStatusPort1 != newStatusPort1) {
  47.     Joystick[0].setYAxis(0);
  48.     Joystick[0].setXAxis(0);
  49.     if (!bitRead(newStatusPort1,4)) Joystick[0].setYAxis(-127); //UP
  50.     if (!bitRead(newStatusPort1,5)) Joystick[0].setYAxis(127); //DOWN
  51.     if (!bitRead(newStatusPort1,6)) Joystick[0].setXAxis(-127); //LEFT
  52.     if (!bitRead(newStatusPort1,7)) Joystick[0].setXAxis(127); //RIGHT
  53.     Joystick[0].setButton(0, !bitRead(newStatusPort1,0)); //BUTTON1 (A)
  54.     Joystick[0].setButton(1, !bitRead(newStatusPort1,1)); //BUTTON2 (B)
  55.     Joystick[0].setButton(2, !bitRead(newStatusPort1,2)); //BUTTON3 (Select)
  56.     Joystick[0].setButton(3, !bitRead(newStatusPort1,3)); //BUTTON4 (Start)
  57.     Joystick[0].sendState();
  58.     lastStatusPort1 = newStatusPort1;
  59.   }
  60.  
  61. }
  62.  
  63.  
  64. void ReadNESjoy() {
  65.   latchlow;
  66.   clocklow;
  67.   latchhigh;
  68.   wait;
  69.   latchlow;
  70.  
  71.   for (int i = 0; i < 8; i++) {
  72.      output += dataread * (1 << i);
  73.      clockhigh;
  74.      wait;
  75.      clocklow;
  76.      wait;
  77.   }
  78. }
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