Offline
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #90 on: February 02, 2013, 01:34:00 am » |
I know why it's doing it. But I want you to work it out. It's a learning process.
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
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #91 on: February 02, 2013, 01:39:11 am » |
or maybe something like this? if ( output == 127 ) { Keyboard.press('a'); if (Keyboard.press('a') = TRUE) { Keyboard.release('a'); } } or 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
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #92 on: February 02, 2013, 01:42:39 am » |
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
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #93 on: February 02, 2013, 01:45:42 am » |
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
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #94 on: February 02, 2013, 01:49:44 am » |
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
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #95 on: February 02, 2013, 02:07:35 am » |
or am i supposed to use while? cuz in the arduino reference it says 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
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #96 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...
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #97 on: February 02, 2013, 02:48:38 am » |
i think im getting the 127 problem because of this section of the 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
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #98 on: February 02, 2013, 06:37:27 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: if (output != oldOutput) { Serial.println (output, DEC); oldOutput = output; }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #99 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: if (output != oldOutput) { Serial.println (output, DEC); oldOutput = output; }
i did some research on debugging, and this is what i came up with. 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
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #100 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: 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
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #101 on: February 02, 2013, 08:30:34 pm » |
hmmmm... i tried this 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
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #102 on: February 02, 2013, 08:47:04 pm » |
i think i solved it, it acts like a keyboard 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
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #103 on: February 02, 2013, 08:58:14 pm » |
Okay, heres the complete code  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. /* 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
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #104 on: February 02, 2013, 09:02:16 pm » |
Improving, very good! However this test here: 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: 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
|
|
|
|
|
|