Pages: 1 ... 5 6 [7] 8   Go Down
Author Topic: Arduino Nes controller, Please Help!  (Read 6797 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know why it's doing it. But I want you to work it out. It's a learning process.

Code:
if ( output == 127 )
{
    Keyboard.press('a');
    delay(1000);
    Keyboard.release('a');
}

is this what you were thinking? i dont see anything else :/



LOL! that didnt work xD
Logged

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

or maybe something like this?

Code:
if ( output == 127 )
{
    Keyboard.press('a');
    if (Keyboard.press('a') = TRUE)
    {
        Keyboard.release('a');
    }
}

or


Code:
if ( output == 127 )
{
    Keyboard.press('a');
    Keyboard.release('a');

    if  (Keyboard.stillpressed('a') = TRUE)
    {
        Keyboard.press('a');
        else
            Keyboard.release('a');
    }
       
}
« Last Edit: February 02, 2013, 01:43:26 am by Craftee » Logged

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

No. Think why it is spamming "a".

Let me ask you something. If it was raining, you would get an umbrella? Yes? And if it was raining a moment later would you get another umbrella? Or would you say "hey, I already have one umbrella". Or would you get one umbrella every second (as your solution did).
Logged


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

No. Think why it is spamming "a".

Let me ask you something. If it was raining, you would get an umbrella? Yes? And if it was raining a moment later would you get another umbrella? Or would you say "hey, I already have one umbrella". Or would you get one umbrella every second (as your solution did).

yes that does make sense. but im not sure what its called in code. but i do understand your point
Logged

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

No. Think why it is spamming "a".

are you talking about when in serial monitor it spamming 127 instead of 0? or are you talking when i hold down the button?
Logged

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

or am i supposed to use while?
cuz in the arduino reference it says

Code:
void loop() {
  while (digitalRead(2) == HIGH) {
    // do nothing until pin 2 goes low
    delay(500);
  }
  delay(1000);
  // new document:
  Keyboard.press(ctrlKey);
  Keyboard.press('n');
  delay(100);
  Keyboard.releaseAll();
  // wait for new window to open:
  delay(1000);
}

the only difference i see from this code and my code is "delay" and "while"
Logged

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


i still think i need to fix the "a" button problem before anything else...

i think the reason why i cant isolate the "a" button is because i think the chip in the controller is doing math. for instance

the base number would be 127 - x  (x being the value of the button)

so when i press the "a" button, in the serial monitor it comes out to 127
because 127 - 0 = 127 and therefor a = 0

when i press the "b" button, in the serial monitor it comes out to 126
because 127 - 1 = 126 and therefor b = 1

and so on.

im not sure how to approach this for a fix...
Logged

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

i think im getting the 127 problem because of this section of the code

Code:
void ReadNESjoy()
{
  latchlow;
  clocklow;
  latchhigh;
  wait;
  latchlow;
  for (int i = 0; i < 8; i++) {
     clockhigh;
     wait;
     output += dataread * (1 << i);
     clocklow;
     wait;
  }
}
Logged

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

Try to do some basic debugging. I'm waiting for some sign you are attempting to fix it yourself.

For example, with the earlier sketch running, which displays the number you get, push buttons and see what numbers appear. That way you can relate button-pushes to what bits they set.

This would be a good time to work in code that looks for changes in what is pressed. Something like:

Code:
if (output != oldOutput)
  {
  Serial.println (output, DEC);
  oldOutput = output;
  }
Logged


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

Try to do some basic debugging. I'm waiting for some sign you are attempting to fix it yourself.

For example, with the earlier sketch running, which displays the number you get, push buttons and see what numbers appear. That way you can relate button-pushes to what bits they set.

This would be a good time to work in code that looks for changes in what is pressed. Something like:

Code:
if (output != oldOutput)
  {
  Serial.println (output, DEC);
  oldOutput = output;
  }

i did some research on debugging, and this is what i came up with.

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

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

Try to do some basic debugging. I'm waiting for some sign you are attempting to fix it yourself.

For example, with the earlier sketch running, which displays the number you get, push buttons and see what numbers appear. That way you can relate button-pushes to what bits they set.

This would be a good time to work in code that looks for changes in what is pressed. Something like:

Code:
if (output != oldOutput)
  {
  Serial.println (output, DEC);
  oldOutput = output;
  }

also, how do i define "oldOutput"?
or am i supposed to do
if ( output != output ){
  Serial.println (output, DEC);
  else
    do nothing
}
Logged

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

hmmmm... i tried this

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

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

i think i solved it, it acts like a keyboard

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

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

Okay, heres the complete code smiley-razz 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.

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;

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);
  }
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 480
Posts: 18732
Lua rocks!
View Profile
WWW
 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
Logged


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