Go Down

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

dave36

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:
  Serial.begin(9600);
  // 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:
  Serial.println(buttonState1);
  Serial.println(buttonState2);
  Serial.println(buttonState3);
  Serial.println(buttonState4);
  Serial.println(buttonState5);
  Serial.println(buttonState6);
  Serial.println(buttonState7);
  Serial.println(buttonState8);
  Serial.println(buttonState9);
  Serial.println(buttonState10);
  Serial.println(buttonState11);
  Serial.println(buttonState12);
 
  delay(1);        // delay in between reads for stability
}

Nick Gammon

http://www.gammon.com.au/switches

Also:

http://www.gammon.com.au/tips

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

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

Jimmy60

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.

PaulS

Code: [Select]
int Button1 = 1;

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

  int buttonState1 = digitalRead(Button1);
  Serial.println(buttonState1);

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).

Jimmy60

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
    delay(WAIT);       
    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