Go Down

Topic: Duemilanove for gaming interface problems (Read 322 times) previous topic - next topic


I am trying to build a button box for my PC using a Duemilanove board and 12 momentary switches. This is my first attempt to program so I used an example and expanded it to the following code. It works fine when I plug in the USB and monitor it with the Serial Monitor. My problem comes with my games not seeing the button presses. I have been looking for a tutorial about using Arduino boards for games and found a few but I am missing something to make it recognizable to the PC.

Code: [Select]
int Button1 = 1;
int Button2 = 2;
int Button3 = 3;
int Button4 = 4;
int Button5 = 5;
int Button6 = 6;
int Button7 = 7;
int Button8 = 8;
int Button9 = 9;
int Button10 = 10;
int Button11 = 11;
int Button12 = 12;

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  // make the pushbutton's pin an input:
  pinMode(Button1, INPUT);
  pinMode(Button2, INPUT);
  pinMode(Button3, INPUT);
  pinMode(Button4, INPUT);
  pinMode(Button5, INPUT);
  pinMode(Button6, INPUT);
  pinMode(Button7, INPUT);
  pinMode(Button8, INPUT);
  pinMode(Button9, INPUT);
  pinMode(Button10, INPUT);
  pinMode(Button11, INPUT);
  pinMode(Button12, INPUT);

// the loop routine runs over and over again forever:
void loop() {
  // read the input pin:
  int buttonState1 = digitalRead(Button1);
  int buttonState2 = digitalRead(Button2);
  int buttonState3 = digitalRead(Button3);
  int buttonState4 = digitalRead(Button4);
  int buttonState5 = digitalRead(Button5);
  int buttonState6 = digitalRead(Button6);
  int buttonState7 = digitalRead(Button7);
  int buttonState8 = digitalRead(Button8);
  int buttonState9 = digitalRead(Button9);
  int buttonState10 = digitalRead(Button10);
  int buttonState11 = digitalRead(Button11);
  int buttonState12 = digitalRead(Button12);
  // print out the state of the button:
  delay(1);        // delay in between reads for stability

Nick Gammon




In particular, tip 1 (not trap 1).
Please post technical questions on the forum, not by personal message. Thanks!

More info:


Can the Due emulate a keyboard like Leonardo can? With the Leonardo you would use the Keyboard methods. I found, doing a similar project, that my games only seemed to like the Keyboard.press() and Keyboard.release() methods. With these methods you can send out a character that appears to the computer as a keyboard character.


Code: [Select]
int Button1 = 1;

  // make the pushbutton's pin an input:
  pinMode(Button1, INPUT);

  int buttonState1 = digitalRead(Button1);

Do you want to use pin one for a switch (buttons are for shirts) OR for Serial? Both is the wrong answer. (So is yes).
The art of getting good answers lies in asking good questions.


So I bothered to go look and it appears that the Due does support the Keyboard methods. What I found was for games I had to use the Keyboard.press() and .release()

Code: [Select]

const unsigned long WAIT = 40UL;
void setup()
    Keyboard.begin();   // starts keyboard services
    Keyboard.press('A');  // press an A
    Keyboard.release('A');  // release the A
void loop(){}

That should simply type an A to where ever your active cursor is on your computer. I suggest using a blank document. If you play with the WAIT value and increase it to say 2000 the key will repeat as you simulated holding it for 2 seconds. When mapping to push button switches you can issue the key press when pressed and release it when the switch is released,  just like a keyboard key.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131