Arduino Nes controller, Please Help!

hmmmm... i tried this

  if ( output != 255 ) {
    Serial.println(output, DEC);

this works when im not pressing a button, but when i press a button, it start spamming again :confused:

i think i solved it, it acts like a keyboard

void loop() {
  NES();
  if ( output != 255 ) {
    Serial.println(output, DEC);
  }
  if ( output == 127 ) {
    Keyboard.press('a');
  }
  else if ( output != 127 || output == output) {
    Keyboard.release('a');
  }
}

and yes i did it by myself :3

Okay, heres the complete code :stuck_out_tongue: works like i want it to, it was a journey, i learned alot from it. I couldnt have done this without you @Nick Gammon. Thanks a lot.

/*
Description:	Arduino NES Controller
Coded by:	Craftee with massive help from Nick Gammon
Date:		Feb 2, 2013
Revision:	V2.0
*/


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

byte output = 0;

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

void NES() {
  output = 0;
  digitalWrite(latch,LOW);
  digitalWrite(clock,LOW);
  digitalWrite(latch,HIGH);
  delayMicroseconds(4);
  digitalWrite(latch,LOW);
  output = digitalRead(data);
  for (int i = 1; i <= 7; i ++) {
    digitalWrite(clock,HIGH);
    delayMicroseconds(4);
    output = output << 1;
    output = output + digitalRead(data) ;
    delayMicroseconds(4);
    digitalWrite(clock,LOW);
  }
}

void loop() {
  NES();
  if ( output != 255 ) {
    Serial.println(output, DEC);
  }
  
  if ( output == 127 ) {
    Keyboard.press('a');
  }
  else if ( output != 127 || output == output) {
    Keyboard.release('a'); 
  }
  
  if ( output == 191 ) {
    Keyboard.press('b');
  }
  else if ( output != 191 || output == output) {
    Keyboard.release('b');
  }
  
  if ( output == 239 ) {
    Keyboard.press('s');
  }
  else if ( output != 239 || output == output) {
    Keyboard.release('s');
  }
  
  if ( output == 223 ) {
    Keyboard.press('l');
  }
  else if ( output != 223 || output == output) {
    Keyboard.release('l');
  }
  
  if ( output == 247 ) {
    Keyboard.press(KEY_UP_ARROW);
  }
  else if ( output != 247 || output == output) {
    Keyboard.release(KEY_UP_ARROW);
  }
  
  if ( output == 251 ) {
    Keyboard.press(KEY_DOWN_ARROW);
  }
  else if ( output != 251 || output == output) {
    Keyboard.release(KEY_DOWN_ARROW);
  }
  
  if ( output == 254 ) {
    Keyboard.press(KEY_RIGHT_ARROW);
  }
  else if ( output != 254 || output == output) {
    Keyboard.release(KEY_RIGHT_ARROW);
  }
  
  if ( output == 253 ) {
    Keyboard.press(KEY_LEFT_ARROW);
  }
  else if ( output != 253 || output == output) {
    Keyboard.release(KEY_LEFT_ARROW);
  }
}

Improving, very good!

However this test here:

output == output

You are asking "is output equal to output?"

It always will be, won't it?

However to help you along a bit, this is what I meant by oldOutput:

int oldOutput = 0;

void loop() 
  {
  NES();  // read controller

  if (output != oldOutput)
    {
    Serial.println (output, DEC);  // debugging

    // button 'a' pressed?
    if ( output == 127 )
      {
      Keyboard.press ('a');
      Keyboard.release ('a');
      }

    oldOutput = output;  // remember for next time
    }  // something changed!

  }  // end of loop

Okay, heres the complete code ... works like i want it to, it was a journey, i learned alot from it. I couldnt have done this without you @Nick Gammon. Thanks a lot.

Glad to hear it! However you might want to look at what I said above about the comparison.

i tried what you put, and it works for 1 button press, but if i hold it down, the a button doesnt repeatedly type.

/*
Description:	Arduino NES Controller
Coded by:	Craftee with massive help from Nick Gammon
Date:		Feb 2, 2013
Revision:	V2.0
*/


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

byte output = 0;
int oldOutput = 0;

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

void NES() {
  output = 0;
  digitalWrite(latch,LOW);
  digitalWrite(clock,LOW);
  digitalWrite(latch,HIGH);
  delayMicroseconds(4);
  digitalWrite(latch,LOW);
  output = digitalRead(data);
  for (int i = 1; i <= 7; i ++) {
    digitalWrite(clock,HIGH);
    delayMicroseconds(4);
    output = output << 1;
    output = output + digitalRead(data) ;
    delayMicroseconds(4);
    digitalWrite(clock,LOW);
  }
}

void loop() {
  NES();
  if ( output != oldOutput) {
    Serial.println(output, DEC);
  if ( output == 127 ) {
    Keyboard.press('a');
    Keyboard.release('a');
  }
  oldOutput = output;
  }
}

heres an example.

if i hold down the "a" button on the keyboard, this is what it looks like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa (and so on, slow and steady. not massive spam)

if i hold down the "a" button on the NES controller with your updated code, this is what it looks like

a

if i hold down the "a" button on the NES controller with my code, this is what it looks like

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa (and so on, same as the keyboard would, slow and steady. not massive spam)

am i missing something here?

OK, well I thought you were complaining about the button "spamming".

Remove (or comment-out) this line to make the button repeat:

  if (output != oldOutput)

no no no... this isnt what i want.
heres what i mean. open up "notepad" and hold down a key on your keyboard.
notice how the first letter pops up, then after a short pause, its a slow steady spam of letters?
well your code when i hold down the button, there is no short pause, and it spams super fast.
do you see the difference?

Well the crude approach is to add a delay. That may be acceptable, maybe not:

if ( output == 127 ) {
    Keyboard.press('a');
    Keyboard.release('a');
    delay (10);    // <--- adjust to taste
  }

i have taken out

|| output == output

and adding the delay still doesnt add the first pause.let me show you, the video will show everything

bandicam 2013-02-02 18-31-24-181.avi (1.32 MB)

Show me your modified code (all of it) with the delay in it.

i didnt add a delay, it just did it by itself. i didnt change anything from the once i posted earlier.

also, how can i make the code look for the previous code and compare it to the present code? like oldOutput.

for example in the serial monitor, when it says

127
127
127 <-- oldOutput
127 <-- look at this and compare it with oldOutput

if (newOutput == 127 ) {
  Keyboard.press('a')
  if (oldOutput != newOutput) {
    Keyboard.release('a')
  }
}

and then when

127
127
127 <-- oldOutput
255 <-- look at this and compare it with oldOutput

the key would be released.

the problem is, i dont know how to make it look at the previous data

Keep plugging away at it.

About the very first example program that comes with the IDE has a delay in it. If things are happening too fast, adding a delay is an obvious and simple way of dealing with it.

how can i make the code look for the previous code and compare it to the present code?

  if (oldOutput != newOutput) {

Are you sure you want to ask that? You mean, "how do I compare two things and make a decision, exactly like that line of code?"

I repeat, go and learn some simple C. If it isn't completely obvious to you by now, then you haven't learnt the simple stuff. Asking me to sit here and write code for you isn't teaching you anything.

i just need my controller to act like a keyboard,

because of my code where

  if ( output == 127 ) {
    Keyboard.press('a');
  }
  else if ( output != 127 ) {
    Keyboard.release('a');
  }

i cant have more than 1 input at one single time

but if i combine buttons i get different number outcomes, like

a = 127
b = 191

if i hold down a and b at the same time i get

a + b = 63

when i tried adding this to the code, i get problems because of the if else statement

  if ( output == 63 ) {
    Keyboard.press('b');
    Keyboard.press('a');
  }
  else if ( output != 63) {
    Keyboard.release('b');
    Keyboard.release('a');
  }

and when i try to use a or b regularly, it wont work because of

  else if ( output != 63) {
    Keyboard.release('b');
    Keyboard.release('a');

hmm im trying a new approach, im trying to use your

const int latch = 2;
const int clock = 3;
const int data  = 4;
int oldOutput = 0;
byte output = 0;

void loop() {
  NES();
  Serial.println(output, DEC);

  if ( output == 127 ) {
    Keyboard.press('a');
  }
  
  if ( output == 191 ) {
    Keyboard.press('b');
  }

  if ( output != oldOutput ) {
    Keyboard.releaseAll();
  }
}

but it doesnt seem to be working. im so frustrated D": i just need to know how to get the previous data, and i dont think "int oldOutput = 0;" is working :confused:

think "int oldOutput = 0;" is working :confused:

You haven't posted all of your code, but if declaration and initialisation of a global is not working, you have some very serious problems.
It may be best to not initialise it to zero ( the compiler will do that anyway for global or statics), but to read it in "setup"

The code he posted never changes oldOutput.

Hey people, sorry for bringing up this old topic again....

If the topic started managed to get this working, would he/she be willing to post the code?
I want to try this....