Using a SEGA Genesis controller to light up LEDs

Okay, so I’ve been messing around trying to advance toward my ultimate goal of using an Arduino as a wireless controller (via a pair of xbee wireless shields and another Arduino board) for a tank bot. I decided to use a Sega Genesis controller due to availability (I’ve had the whole system in a closet for years, so I didn’t have to spend any money acquiring one) and because the controller uses a 9-pin D-SUB connector, making it easily adapted to my purposes without removing the connector, and I have a couple of adapters lying around that I can scavenge.

It is my understanding that the Genesis controller can be wired directly to the Arduino board, so that is exactly what I did, through a Screwshield (I realize that this may not be entirely correct and actually may be the source of at least part of my problems - I remember reading that the Genesis used a parallel protocol to connect the controller, they just reduced it down to a 9-pin connector). Using a pinout I found elsewhere on the internet (here, as a matter of fact), I connected pins 1-4,6,7, and 9 of the female 9-pin connector to Arduino pins 2-8, respectively, with controller pins 5 & 8 going to +5v and ground, also respectively. If you know a better way to do this, I’m all ears. I’d like to get this project off the ground as soon as possible, but the direct approach seems to be failing, so I’m trying to find a workaround.
/Update on this - as of a few moments ago, I uploaded an example program that turns an LED on when a button is pressed, and it seemed to work with the controller (thus seemingly pointing to a software issue being the most prominent problem), but it only worked every third time I pressed the button on both controllers I tried. I am attempting to rule out a short in my homemade 9-pin adapter made from a D-Sub adapter salvaged from an old water cooling system and ribbon cable cut from a 40-pin floppy drive connector cable by replacing it with an entire assembly that came with an old motherboard. Now, would anyone care to speculate as to why the controller was read only every third time the button was pressed?/

I wrote a program to test the functionality of my hardware, based off of the digital button example in the Arduino IDE. Please let me know if you can tell me if something is wrong there as well.

const int UP=2,DOWN=3,LEFT=4,RIGHT=5,AB=6,SELECT=7,STARTC=9;
const int LEDLEFT=9,LEDRIGHT=10,LEDUP=11,LEDDOWN=12;
int upstate=0;
int lastupstate=0;
int downstate=0;
int lastdownstate=0;
int leftstate=0;
int lastleftstate=0;
int rightstate=0;
int lastrightstate=0;
void setup()
{
pinMode(UP,INPUT);
/pinMode(DOWN,INPUT);
pinMode(LEFT,INPUT);
pinMode(RIGHT,INPUT);
pinMode(AB,INPUT);
pinMode(SELECT,INPUT);
pinMode(STARTC,INPUT);
/
pinMode(LEDLEFT,OUTPUT);
pinMode(LEDRIGHT,OUTPUT);
pinMode(LEDUP,OUTPUT);
pinMode(LEDDOWN,OUTPUT);
digitalWrite(LEDUP,HIGH);
delay(1000);
digitalWrite(LEDUP,LOW);
digitalWrite(LEDDOWN,HIGH);
delay(1000);
digitalWrite(LEDDOWN,LOW);
digitalWrite(LEDLEFT,HIGH);
delay(1000);
digitalWrite(LEDLEFT,LOW);
digitalWrite(LEDRIGHT,HIGH);
delay(1000);
digitalWrite(LEDRIGHT,LOW);
//Serial.begin(
}

void loop()
{
upstate=digitalRead(UP);
downstate=digitalRead(DOWN);
leftstate=digitalRead(LEFT);
rightstate=digitalRead(RIGHT);
//if(upstate!=lastupstate)
//{
/* Serial.println(“on”);*/
if(upstate==HIGH)
{
digitalWrite(UP,HIGH);
}
//}
else
{
digitalWrite(UP,LOW);
}
//
//if(downstate!=lastdownstate)
//{
if(downstate==HIGH)
{
digitalWrite(DOWN,HIGH);
}
//}
else
{
digitalWrite(DOWN,LOW);
}
//if(leftstate!=lastleftstate)
//{
if(leftstate==HIGH)
{
digitalWrite(LEFT,HIGH);
}
else
{
digitalWrite(LEFT,LOW);
}
//}
if(rightstate==HIGH)
{
digitalWrite(RIGHT,HIGH);
}
else
{
digitalWrite(RIGHT,LOW);
}
}

Thanks in advance for any assistance you can give.

You significantly reduce e complexity of your code by eliminating the IF statements. There is really no need to do the IF checking.

For example (my variables won't match yours):

upstate = digitalRead(UPbutton);
digitalWrite(UPLED, upstate);

I don't know if you only did it for this posting, but you only define the UP button's pinMode. Generally Pins default to Inputs, but it is always a good idea to explicitly define them.

What I don't remember about the genesis controller is if there the buttons are Debounced. Try adding a small delay (like 200-300ms) at the start of your loop to see if more button presses get caught.

Thank you very much for the input (no pun intended there, really)! I'll try implementing these changes in the next revision of my code.

I know the if statements are rather clunky, but that was how the example code I was basing the test code off of was written. I guess I need to mess around more with the code. I think I commented out most of the pinModes out of frustration when my code wasn't working in an attempt to simplify things as much as possible (I really need to document more).

Dont forget that SELECT is an input to the controller (and output for the arduino). You need to force it high or low to select the "correct" bank of buttons. If you leave it floating, you won't know mode the controller is in, and may change randomly.

Didn't think about that - that changes my code in a not-insignificant way. I am planning to rewrite the code and upload/test the new code tonight after I get off of work.

Can't thank you enough for the assist.

It really doesn't change your code that much. Maybe the variable names need to change for readability, but that is all. You can simply put the SELECT signal HIGH and set your appropriate state variable. Put SELECT low, then set your state variables. Then go about your loop. It isn't much different from what are you doing now.

Alternatively, you can just hold the signal high or low for a subset of the buttons.