Perhaps this will help someone out there?
I purchased a 2-pack of rip off NES 8bit controllers on Amazon with an “extension” cord so that I had a ready made connector. Cut the extension cord off and wired it to an Arduino Mega (because that’s what was on the bench). I am using this for a game I am working on for the Makerspace… but through maybe you could use a snippet that worked. The first google code I tried to copy and paste didn’t work so hot.. so here is what I came up… I did find some inspiration here and there but this is a good launching point for your next project.
/*
NES Controller
*/
const int latch = 50;
const int clock = 48;
const int data = 49;
#define latchlow digitalWrite(latch, LOW)
#define latchhigh digitalWrite(latch, HIGH)
#define clocklow digitalWrite(clock, LOW)
#define clockhigh digitalWrite(clock, HIGH)
#define dataread digitalRead(data)
#define wait delayMicroseconds(15)
unsigned long previousMillis = 0;
const int wait_interval = 20;
const int A_BUTTON = 0;
const int B_BUTTON = 1;
const int SELECT_BUTTON = 2;
const int START_BUTTON = 3;
const int UP_BUTTON = 4;
const int DOWN_BUTTON = 5;
const int LEFT_BUTTON = 6;
const int RIGHT_BUTTON = 7;
byte output;
void setup() {
Serial.begin(115200);
pinMode(latch, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(data, INPUT);
Serial.println("NES TEST");
}
boolean isBitSet (byte myVar, byte bitNumber) {
bool bitvalue;
bitvalue = myVar & (1 << bitNumber);
return bitvalue;
}
void readNES() {
latchlow;
clocklow;
latchhigh;
wait;
latchlow;
for (int i = 0; i < 8; i++) {
output += dataread * (1 << i);
clockhigh;
wait;
clocklow;
wait;
}
}
void hexprint(byte b) {
Serial.print("0x");
if (b < 10) Serial.print("0");
Serial.println(b, HEX);
}
void loop() {
output = 0;
readNES();
if (!isBitSet(output, 0)) Serial.println("A Button");
if (!isBitSet(output, 1)) Serial.println("B Button");
if (!isBitSet(output, 2)) Serial.println("SELECT Button");
if (!isBitSet(output, 3)) Serial.println("START Button");
if (!isBitSet(output, 4)) Serial.println("UP Button");
if (!isBitSet(output, 5)) Serial.println("DOWN Button");
if (!isBitSet(output, 6)) Serial.println("LEFT Button");
if (!isBitSet(output, 7)) Serial.println("RIGHT Button");
//Serial.print("Read: "); hexprint(output);
}