Go Down

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

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

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

Go Up