Go Down

Topic: Arduino Nes controller, Please Help! (Read 9014 times) previous topic - next topic

Craftee



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


Code: [Select]
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

Craftee

#91
Feb 02, 2013, 07:39 am Last Edit: Feb 02, 2013, 07:43 am by Craftee Reason: 1
or maybe something like this?

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


or


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

    if  (Keyboard.stillpressed('a') = TRUE)
    {
        Keyboard.press('a');
        else
            Keyboard.release('a');
    }
       
}


Nick Gammon

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).
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Craftee


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

Craftee


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?

Craftee

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

Code: [Select]

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"

Craftee


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

Craftee

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

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

Nick Gammon

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: [Select]

if (output != oldOutput)
  {
  Serial.println (output, DEC);
  oldOutput = output;
  }
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Craftee


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: [Select]

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



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

Code: [Select]
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);
    }
}

Craftee


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: [Select]

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
}

Craftee

hmmmm... i tried this

Code: [Select]
  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 :/

Craftee

i think i solved it, it acts like a keyboard

Code: [Select]
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

Craftee

Okay, heres the complete code :P 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: [Select]
/*
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);
  }
}

Nick Gammon

Improving, very good!

However this test here:

Code: [Select]

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: [Select]

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
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up