Go Down

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

Craftee



im just a kid that needs help, gosh!


If you think you are capable of solving this problem already with a little help, then you ought to be at least part way there on your own. Show us where you've got so far and which part you need help with, and no doubt there will be plenty of people willing to steer you round the problem.

If you simply don't know where to start, then put this project down and start with something simpler. It's best not to try to boil the ocean - learn things one step at a time.




well i wanna do a project that matters to me, not just some flop project i wont learn anything from, because more often than not, i find myself copying code. i know very little about code.

Nick Gammon

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

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

Craftee


Well, try something and post what you tried.

What have you tried?




:/

Code: [Select]
const int latch = 2;
const int clock = 3;
const int data  = 4;

#define latchlow digitalWrite(latch, LOW)
#define latchhigh digitalWrite(latch, HIGH)
#define clocklow digitalWrite(clock, LOW)
#define clockhigh digitalWrite(clock, HIGH)
#define dataread digitalRead(data)
#define wait delayMicroseconds(200)

byte output;

void setup()
{
Serial.begin(9600);
        while (! Serial ) { }
pinMode(latch, OUTPUT);
        pinMode(clock, OUTPUT);
        pinMode(data, INPUT);
        Keyboard.begin();
}

void loop()
{
  if(KeyboardSerial == 127)
  Keyboard.press(a)
    else ()
    Keyboard.release()
  if(KeyboardSerial == 126)
  Keyboard.press(b)
    else ()
    Keyboard.release()
  if(KeyboardSerial == 119)
  Keyboard.press(up_arrow)
    else ()
    Keyboard.release()
  if(KeyboardSerial == 111)
  Keyboard.press(down_arrow)
    else ()
    Keyboard.release()
  if(KeyboardSerial == 63)
  Keyboard.press(right_arrow)
    else ()
    Keyboard.release()
  if(KeyboardSerial == 95)
  Keyboard.press(left_arrow)
    else ()
    Keyboard.release()
  output = 0;
  ReadNESjoy();
// Serial.write(output);
  Serial.println ((unsigned int)output);
}


void ReadNESjoy()
{
  latchlow;
  clocklow;
  latchhigh;
  wait;
  latchlow;
  for (int i = 0; i < 8; i++) {
     clockhigh;
     wait;
     output += dataread * (1 << i);
     clocklow;
     wait;
  }
}

Nick Gammon

You have to start thinking about what you are doing.

Code: [Select]

void loop()
{
  if(KeyboardSerial == 127)
...


What is KeyboardSerial?

Previously you were displaying the value of output. So surely you want to test that?

Code: [Select]

void loop()
{
  if(output == 127)
...


But, wait! You are testing it before you find out what it is.

How about:

Code: [Select]

void loop()
{
  output = 0;
  ReadNESjoy();

  if(output == 127)
...


It's not magic. Think about what the variables mean and use them in a useful way.
Please post technical questions on the forum, not by personal message. Thanks!

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

Craftee

Code: [Select]
void loop()
{
  output = 0;
  ReadNESjoy();
  Serial.println ((unsigned int)output);
 
  if(output == 127)
  Keyboard.press('a')
    else ()
    Keyboard.release()
  if(output == 126)
  Keyboard.press('b')
    else ()
    Keyboard.release()
  if(output == 119)
  Keyboard.press('up_arrow')
    else ()
    Keyboard.release()
  if(output == 111)
  Keyboard.press('down_arrow')
    else ()
    Keyboard.release()
  if(output == 63)
  Keyboard.press('right_arrow')
    else ()
    Keyboard.release()
  if(output == 95)
  Keyboard.press('left_arrow')
    else ()
    Keyboard.release()
  if(output == 123)
  Keyboard.press('s')
    else ()
    Keyboard.release()
  if(output == 125)
  Keyboard.press('l')
    else ()
    Keyboard.release()

}

Craftee

it says ";" is expected before else, im not sure where to put it....

Nick Gammon

Find a book on programming in C. Read it.

Quote
well i wanna do a project that matters to me, not just some flop project i wont learn anything from


You wanna learn? Start reading.
Please post technical questions on the forum, not by personal message. Thanks!

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

Craftee


Find a book on programming in C. Read it.

Quote
well i wanna do a project that matters to me, not just some flop project i wont learn anything from


You wanna learn? Start reading.


i did, i even bought a huge book!. but im just not getting it. i figure since alot of people learn by just diving into it, i would try the same

Craftee


it says ";" is expected before else, im not sure where to put it....


Code: [Select]
  if(output == 127)
  Keyboard.press('a');
    else ()
    Keyboard.release()


i fixed the ";" problem, but it says
"expected primary expression before ")"

Code: [Select]
    else ()

lloyddean

Code: [Select]

if ( output == 127 )
{
    Keyboard.press('a');
}
else
{
    Keyboard.release();
}


Craftee


Code: [Select]

if ( output == 127 )
{
    Keyboard.press('a');
}
else
{
    Keyboard.release();
}




hmm i tried that, and i got this error "no matching function for call to "Keyboard_::release()

Craftee

okay i fixed that error with

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

but now, it spams the "a" button. because for some reason when i serial monitor it, it spams 127 and not 0, im not sure why this is happening

JimboZA

Quote
well i wanna do a project that matters to me, not just some flop project i wont learn anything from,


Fair point...

But:

Quote
because more often than not, i find myself copying code. i know very little about code.


I thought you did copy the code, and I'd actually question what you're learning by getting the forum to debug it line by line.

Quote
i figure since alot of people learn by just diving into it, i would try the same


An admirable approach except it's a way of drowning if you don't have a strong support group (read as: forum members patient enough to help while putting up with your impatience as in bumping posts when you don't get an instant response)

I suggest you work through tutorials like these or these
Gone for good

Craftee

Quote

Quote
because more often than not, i find myself copying code. i know very little about code.


I thought you did copy the code, and I'd actually question what you're learning by getting the forum to debug it line by line.


actually, i have learned a lot from this thread. already i know 10x more than i did before i started.

Quote
Quote
i figure since alot of people learn by just diving into it, i would try the same


An admirable approach except it's a way of drowning if you don't have a strong support group...

HI :P

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

Go Up