Pages: 1 ... 6 7 [8]   Go Down
Author Topic: Arduino Nes controller, Please Help!  (Read 6935 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18768
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Improving, very good!

However this test here:

Code:
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:

Code:
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

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

Code:
/*
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;
  }
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

heres an example.

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

Code:
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

Code:
a

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

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


am i missing something here?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18768
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Code:
 if (output != oldOutput)
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Code:
 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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18768
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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


Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

i have taken out
Code:
|| 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 (1352.27 KB - downloaded 16 times.)
« Last Edit: February 02, 2013, 09:35:45 pm by Craftee » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18768
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Code:
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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18768
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

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

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.
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i just need my controller to act like a keyboard,

because of my code where

Code:
 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

Code:
 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

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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 :/
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26285
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
think "int oldOutput = 0;" is working :/
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"
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 484
Posts: 18768
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The code he posted never changes oldOutput.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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....
Logged

Pages: 1 ... 6 7 [8]   Go Up
Jump to: