Go Down

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

Nick Gammon

@ Craftee  

I'm sorry, but you haven't learnt the absolute basics, like how to write statements in C. I don't want to seem unhelpful, but you really need to learn a bit of that, before you start making something that sends keystrokes when buttons on an NES controller are pushed. You are trying to run, when you haven't learnt to crawl yet.

Statements in C end in a semicolon. Function calls have brackets. "if" and "else" are not function calls.

Things have to be done in sequence. You don't get out your raincoat, and then afterwards ask if it is raining.

Do some simpler examples, get the hang of programming, and it will reward you with being a lot less frustrating.

If you ask, on a line-by-line basis, how to fix things,  you aren't learning, are you? You are just posting code, asking what is wrong, applying a one-line fix, and then moving onto the next line.
Please post technical questions on the forum, not by personal message. Thanks!

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

Craftee

also, when i use the d-pad. it oddly types "W". nowhere in my code is there a "W"..........

Craftee


@ Craftee  

I'm sorry, but you haven't learnt the absolute basics, like how to write statements in C. I don't want to seem unhelpful, but you really need to learn a bit of that, before you start making something that sends keystrokes when buttons on an NES controller are pushed. You are trying to run, when you haven't learnt to crawl yet.

Statements in C end in a semicolon. Function calls have brackets. "if" and "else" are not function calls.

Things have to be done in sequence. You don't get out your raincoat, and then afterwards ask if it is raining.

Do some simpler examples, get the hang of programming, and it will reward you with being a lot less frustrating.

If you ask, on a line-by-line basis, how to fix things,  you aren't learning, are you? You are just posting code, asking what is wrong, applying a one-line fix, and then moving onto the next line.


lol i like the raincoat analogy. i might not be learning, but i do know more than i used to. better then nothing right?

Craftee


also, when i use the d-pad. it oddly types "W". nowhere in my code is there a "W"..........


i fixed this with

Code: [Select]
if ( output == 119 )
{
   Keyboard.press(KEY_UP_ARROW);
}
else
   Keyboard.release(KEY_UP_ARROW);



before it was

Code: [Select]
if ( output == 119 )
{
   Keyboard.press('up_arrow');
}
else
   Keyboard.release('up_arrow');



but i still have the "a" button problem, where in the serial monitor it spams 127, instead of 0

Nick Gammon

Quote
Code: [Select]
    Keyboard.press('up_arrow');


This was always wrong, for two reasons. As your research has now shown (and congrats for doing it) you have to use KEY_UP_ARROW and not 'up_arrow'.

Second, strings literals are in double quotes, not single quotes, so it should have been "up_arrow" (not 'up_arrow'). That is, assuming you wanted a string literal at all, which you really didn't.

Since you are trying to squeeze a "character" literal into one byte, that accounts for why you saw "w" (which is the last letter in up_arrow).

Please post technical questions on the forum, not by personal message. Thanks!

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

Craftee

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


Quote

This was always wrong, for two reasons. As your research has now shown (and congrats for doing it) you have to use KEY_UP_ARROW and not 'up_arrow'.

Second, strings literals are in double quotes, not single quotes, so it should have been "up_arrow" (not 'up_arrow'). That is, assuming you wanted a string literal at all, which you really didn't.

Since you are trying to squeeze a "character" literal into one byte, that accounts for why you saw "w" (which is the last letter in up_arrow).




ahh okay, thanks for the explanation

Nick Gammon

Quote
Code: [Select]
if ( output == 119 )
{
    Keyboard.press(KEY_UP_ARROW);
}
else
    Keyboard.release(KEY_UP_ARROW);


Wouldn't this make more sense?


Code: [Select]
if ( output == 119 )
  {
  Keyboard.press (KEY_UP_ARROW);
  Keyboard.release (KEY_UP_ARROW);
  }


You only need to release the key after you've pressed it, don't you?

What you had is like:

Code: [Select]

  if (phoneRings)
    answerPhone ();
  else
    hangUpPhone ();


But you don't need to hang up the phone if it hasn't rung!
Please post technical questions on the forum, not by personal message. Thanks!

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

Craftee

Quote

Code: [Select]
if ( output == 119 )
  {
  Keyboard.press (KEY_UP_ARROW);
  Keyboard.release (KEY_UP_ARROW);
  }


You only need to release the key after you've pressed it, don't you?

What you had is like:

Code: [Select]

  if (phoneRings)
    answerPhone ();
  else
    hangUpPhone ();


But you don't need to hang up the phone if it hasn't rung!


does this fix stuff? or does it just make the code more efficient?

Nick Gammon

I'm trying to teach you stuff here.

Right now, by luck, it may work your way. Best to get things right though. It is the logical thought processes that will help you here.

If your code was littered with stuff (as it is right now) like:

Code: [Select]
    Keyboard.release(KEY_UP_ARROW);


even when the key is not down ... you are just asking for future problems.
Please post technical questions on the forum, not by personal message. Thanks!

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

Craftee

#85
Feb 02, 2013, 07:25 am Last Edit: Feb 02, 2013, 07:27 am by Craftee Reason: 1

Quote


does this fix stuff? or does it just make the code more efficient?


okay i see what it did, but that wasnt the result i wanted. when i did it your way, the button was spamming ( it wasnt acting like a keyboard )

Nick Gammon

Let me ask you this ... would you go around your house turning off light switches if they are already off?

No, you wouldn't, would you? And would you complain "hey, does it matter"? No. It is not logical to do it. That is the thing you have to learn.
Please post technical questions on the forum, not by personal message. Thanks!

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

Nick Gammon

Quote
it wasnt acting like a keyboard


Try to think why. Please.
Please post technical questions on the forum, not by personal message. Thanks!

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

Nick Gammon

I know why it's doing it. But I want you to work it out. It's a learning process.
Please post technical questions on the forum, not by personal message. Thanks!

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

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

Go Up