Hey ho everyone. I need some help trying to get this code to read two different NES controller presses. But the code I found is giving me a hard time about it. I found the code that someone made here:
https://docs.google.com/a/acidbabies.com/folder/d/0B8X8TmxLLUQiS28wcjBIQXJJaFk/edit
The original code can be found here:
http://www.thehelloworld.info/
I can upload it to the arduino and read one controller like a dream. But I want to be able to run two controllers on the same arduino. I can wire the two controllers easily. But the code is giving me trouble in trying to achieve this. Here is the code that I had altered:
#include <MeetAndroid.h>
MeetAndroid meetAndroid;
int latch = 2; // set the latch pin
int clock = 3; // set the clock pin
int data = 4; // set the data pin
int controllerData[16]; // array to store data from 16 clock cycles
/* SETUP */
void setup()
{
// set baud rate to 57.6k
Serial.begin(57600);
// set output/input pins
pinMode(latch,OUTPUT);
pinMode(clock,OUTPUT);
pinMode(data,INPUT);
// set latch and clock high
digitalWrite(latch,HIGH);
digitalWrite(clock,HIGH);
}
/* THIS READS DATA FROM THE CONTROLLER */
void controllerRead()
{
// set latch and clock low
digitalWrite(latch,LOW);
digitalWrite(clock,LOW);
// set latch high to trigger controller data send
digitalWrite(latch,HIGH);
delayMicroseconds(4);
digitalWrite(latch,LOW);
delayMicroseconds(2);
// Clock Cycle 1
controllerData[0] = digitalRead(data);
// Clock Cycles 2-16
for (int i = 1; i <= 15; i++)
{
digitalWrite(clock,HIGH);
delayMicroseconds(4);
digitalWrite(clock,LOW);
delayMicroseconds(4);
controllerData[i] = digitalRead(data);
}
}
// NES Button Reference
// 01111111 - A [0]
// 10111111 - B [1]
// 11011111 - SELECT [2]
// 11101111 - START [3]
// 11110111 - UP [4]
// 11111011 - DOWN [5]
// 11111101 - LEFT [6]
// 11111110 - RIGHT [7]
// UP+RIGHT
// UP+LEFT
// DOWN+RIGHT
// DOWN+LEFT
/* MAIN LOOP */
void loop()
{
controllerRead();
String Out = "";
int length = 1;
// A
if (controllerData[0] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "QZ";
length = length + 2;
}
// B
if (controllerData[1] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "WL";
length = length + 2;
}
// SELECT
if (controllerData[2] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "EK";
length = length + 2;
}
// START
if (controllerData[3] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "UJ";
length = length + 2;
}
// UP
if (controllerData[4] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "I1";
length = length + 2;
}
// DOWN
else if (controllerData[5] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "AS";
length = length + 2;
}
// LEFT
else if (controllerData[6] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "DA";
length = length + 2;
}
// RIGHT
else if (controllerData[7] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "SI";
length = length + 2;
}
// UP+RIGHT
if (controllerData[4] == 0 && controllerData[7] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "KU";
length = length + 2;
}
// UP+LEFT
else if (controllerData[4] == 0 && controllerData[6] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "JE";
length = length + 2;
}
// DOWN+RIGHT
else if (controllerData[5] == 0 && controllerData[7] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "LQ";
length = length + 2;
}
// DOWN+LEFT
else if (controllerData[5] == 0 && controllerData[6] == 0)
{
if (Out != "")
{
Out = Out + ",";
length = length + 2;
}
Out = Out + "ZW";
length = length + 2;
}
char outData[length];
Out.toCharArray(outData, length);
meetAndroid.send(outData);
Serial.println(outData);
delay(16);
}
Only thing that I did was remove all LED code since I don't have any, tightened the sensitivity of the button presses, and I added two keystrokes to each button press. I had to look at it for a long time, but the code that allows me to do that is right below the keystroke mapping "length = length + 2;" You can set that number to as many keystrokes as you want, which is great. You just set each controller in the emulator to read only one key. The problem is that the code only seems to recognise only certain keystokes. It will recongnise keys like "QWEU" but not keys like "OPH" can anyone look at this code and tell me why that is? The only thing that I can think of is that this code was designed for an SNES controller, therefore can only recgonise 16 keystrokes. But I can't find anywhere what specifies that or how I can change that. Anyone might have an idea? I know very little of arduino coding only but from tutorials I have watched online, so your help would be gratefully appreciated. Thanks guys, later.