Connecting 2 NES controllers to arduino to route into max/msp

Hello! First post, crazy new at this. Didn't see any similar questions like this so forgive me if this has been answered and I didn't see it.

I found this code to connect an NES controller to my arduino:

/*
Description: Interfacing a NES controller with a PC with an Arduino.
Coded by: Prodigity
Date: 1 December 2011
Revision: V0.93 (beta)
Modified by: Matt Booth (20 December 2014)
*/

const int latch = 2;
const int clock = 3;
const int data = 4;

#define latchlow digitalWrite(latch, LOW)
#define latchhigh digitalWrite(latch, HIGH)
#define clocklow digitalWrite(clock, LOW)
#define clockhigh digitalWrite(clock, HIGH)
#define dataread digitalRead(data)

// http://www.mit.edu/~tarvizo/nes-controller.html
#define wait delayMicroseconds(12)

byte output;

void setup() {
Serial.begin(9600);
pinMode(latch, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(data, INPUT);
}

void loop() {
output = 0;
ReadNESjoy();
Serial.write(output);
}

void ReadNESjoy() {
latchlow;
clocklow;
latchhigh;
wait;
latchlow;

for (int i = 0; i < 8; i++) {
output += dataread * (1 << i);
clockhigh;
wait;
clocklow;
wait;
}
}

and I was able to get it to send serial data of numbers from 0-255 into max/msp which I was then able to route how I wanted. It works just fine with one controller.

Though, for a school project, I'm trying to connect a second NES controller to the same arduino, using pins 5-6-7 for the second controller's latch, data, and clock, if possible. With my very minimal knowledge of this coding, I fiddled with it and came up with this, but I can't seem to get both to work at once.

/*
Description: Interfacing a NES controller with a PC with an Arduino.
Coded by: Prodigity
Date: 1 December 2011
Revision: V0.93 (beta)
Modified by: Matt Booth (20 December 2014)
*/

const int latch1 = 2;
const int clock1 = 3;
const int data1 = 4;
const int latch2 = 5;
const int clock2 = 6;
const int data2 = 7;

#define latchlow1 digitalWrite(latch1, LOW)
#define latchhigh1 digitalWrite(latch1, HIGH)
#define clocklow1 digitalWrite(clock1, LOW)
#define clockhigh1 digitalWrite(clock1, HIGH)
#define dataread1 digitalRead(data1)
#define latchlow2 digitalWrite(latch2, LOW)
#define latchhigh2 digitalWrite(latch2, HIGH)
#define clocklow2 digitalWrite(clock2, LOW)
#define clockhigh2 digitalWrite(clock2, HIGH)
#define dataread2 digitalRead(data2)

// http://www.mit.edu/~tarvizo/nes-controller.html
#define wait delayMicroseconds(12)

byte output;

void setup() {
Serial.begin(9600);
pinMode(latch1, OUTPUT);
pinMode(clock1, OUTPUT);
pinMode(data1, INPUT);
Serial.begin(9700);
pinMode(latch2, OUTPUT);
pinMode(clock2, OUTPUT);
pinMode(data2, INPUT);
}

void loop() {
output = 0;
ReadNESjoy();
Serial.write(output);
}

void ReadNESjoy() {
latchlow1;
clocklow1;
latchhigh1;
wait;
latchlow1;
latchlow2;
clocklow2;
latchhigh2;
wait;
latchlow2;

for (int i = 0; i < 8; i++) {
output += dataread1 * (1 << i);
clockhigh1;
wait;
clocklow1;
wait;

}
}

I'm sure I've FUBARed the code, but I'm at a loss. Basically, I'd like the second controller to send the exact same data through a different port which I can separately receive and route in max/msp. Only solution I can think of is getting a second arduino uno to independently handle the second controller.

Does anyone have a fix for this code, or a reference I have missed? Or do I actually need the second arduino?

Thank you so much for the help!

:slight_smile:

Basically, I'd like the second controller to send the exact same data through a different port

How? There is only one serial port that the Arduino can use to talk to the PC.

Ah... is there a way for me to send the data through a single port, but prepend controller 1 and 2's individual data so that I can sort it accordingly in max?

..Like have controller 1 come in as "A xxx" and 2 as "B xxx"?

is there a way for me to send the data through a single port, but prepend controller 1 and 2's individual data so that I can sort it accordingly in max?

Yes, but you won't be able to send data near as fast, because you won't be able to send data in binary.

Would hooking up a second arduino through another USB port and sending that data through a different serial port remedy that problem?

Would hooking up a second arduino through another USB port and sending that data through a different serial port remedy that problem?

If the receiving application is able to read from two different ports, yes.