Show Posts
Pages: [1] 2 3 ... 5
1  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 10:45:44 pm
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 :/
2  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 10:08:31 pm
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');
3  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 10:02:34 pm
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
4  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 09:33:37 pm
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
5  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 09:24:05 pm
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?
6  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 09:19:41 pm
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?
7  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 09:16:06 pm
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;
  }
}
8  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 08:58:14 pm
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);
  }
}
9  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 08:47:04 pm
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
10  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 08:30:34 pm
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 :/
11  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 08:22:21 pm
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
}
12  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 08:00:41 pm
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);
    }
}
13  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 02:48:38 am
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;
  }
}
14  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 02:30:18 am

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...
15  Using Arduino / Programming Questions / Re: Arduino Nes controller, Please Help! on: February 02, 2013, 02:07:35 am
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"
Pages: [1] 2 3 ... 5