Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #105 on: February 02, 2013, 09:02:46 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. Glad to hear it! However you might want to look at what I said above about the comparison.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #106 on: February 02, 2013, 09:16:06 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
i tried what you put, and it works for 1 button press, but if i hold it down, the a button doesnt repeatedly type. /* 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; } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #107 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 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 a if i hold down the "a" button on the NES controller with my code, this is what it looks like aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa (and so on, same as the keyboard would, slow and steady. not massive spam) am i missing something here?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #108 on: February 02, 2013, 09:20:09 pm » |
OK, well I thought you were complaining about the button "spamming". Remove (or comment-out) this line to make the button repeat: if (output != oldOutput)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #109 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: 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?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #110 on: February 02, 2013, 09:25:34 pm » |
Well the crude approach is to add a delay. That may be acceptable, maybe not: if ( output == 127 ) { Keyboard.press('a'); Keyboard.release('a'); delay (10); // <--- adjust to taste }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #111 on: February 02, 2013, 09:33:37 pm » |
Well the crude approach is to add a delay. That may be acceptable, maybe not: if ( output == 127 ) { Keyboard.press('a'); Keyboard.release('a'); delay (10); // <--- adjust to taste }
i have taken out || output == output and adding the delay still doesnt add the first pause.let me show you, the video will show everything
|
|
|
|
« Last Edit: February 02, 2013, 09:35:45 pm by Craftee »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #112 on: February 02, 2013, 09:41:35 pm » |
Show me your modified code (all of it) with the delay in it.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #113 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 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 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
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #114 on: February 02, 2013, 10:07:01 pm » |
Keep plugging away at it. About the very first example program that comes with the IDE has a delay in it. If things are happening too fast, adding a delay is an obvious and simple way of dealing with it. how can i make the code look for the previous code and compare it to the present code? if (oldOutput != newOutput) { Are you sure you want to ask that? You mean, "how do I compare two things and make a decision, exactly like that line of code?" I repeat, go and learn some simple C. If it isn't completely obvious to you by now, then you haven't learnt the simple stuff. Asking me to sit here and write code for you isn't teaching you anything.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #115 on: February 02, 2013, 10:08:31 pm » |
i just need my controller to act like a keyboard, because of my code where 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 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 else if ( output != 63) { Keyboard.release('b'); Keyboard.release('a');
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 67
|
 |
« Reply #116 on: February 02, 2013, 10:45:44 pm » |
hmm im trying a new approach, im trying to use your 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 :/
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19060
I don't think you connected the grounds, Dave.
|
 |
« Reply #117 on: February 03, 2013, 04:49:25 am » |
think "int oldOutput = 0;" is working :/ You haven't posted all of your code, but if declaration and initialisation of a global is not working, you have some very serious problems. It may be best to not initialise it to zero ( the compiler will do that anyway for global or statics), but to read it in "setup"
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #118 on: February 03, 2013, 05:36:48 am » |
The code he posted never changes oldOutput.
|
|
|
|
|
Logged
|
|
|
|
|
|