-1

Arduino mini pro (3.3v) に接続された NES コントローラーと、Bluetooth HID モジュール (rn-42 BlueSmirf に似ています) を組み合わせました。Macbook に接続すると、キーストロークが送信されます。

ただし、キーを押し続けるのではなく、キーを繰り返し押しているように動作します。キーボードとまったく同じように動作する必要がありますが、現在はループ (50ms) を通過するたびにキーを押します。事前に助けてくれてありがとう!

Arduino コード:

const int buttonA = 2;//Button: A
const int buttonB = 3;//Button: B
const int buttonC = 4;//Button: Start
const int buttonD = 5;//Button: Select
const int buttonE = 6;//Button: Up
const int buttonF = 7;//Button: Down
const int buttonG = 8;//Button: Left
const int buttonH = 9;//Button: Right

...

void loop()
{
  if (digitalRead(buttonA) == LOW)      //pin is HIGH until a button is pressed
  {Serial.write('A');}
  if (digitalRead(buttonB) == LOW) 
  {Serial.write('B');}
  if (digitalRead(buttonC) == LOW) 
  {Serial.write('1');}
  if (digitalRead(buttonD) == LOW) 
  {Serial.write('2');}
  if (digitalRead(buttonE) == LOW) 
  {Serial.write('U');}
  if (digitalRead(buttonF) == LOW) 
  {Serial.write('D');}
  if (digitalRead(buttonG) == LOW) 
  {Serial.write('L');}
  if (digitalRead(buttonH) == LOW) 
  {Serial.write('R');}
  delay(50);
}
4

2 に答える 2

0

To get you started: your program is doing exactly what you told it to. Here's what's happening: every time through the loop the program asks: is the button pressed? If yes, then it sends a keystroke.

What you need to do is store the state of each button (i.e. whether the button is pressed or not), perhaps in an array. Then read each button, check if the state of that button changed from what you stored, and only if it did, send something to the Bluetooth. In that case you'll need to update the state to the new state of the button.

If the button state has not changed, send nothing (for that button). That will have the behavior you want.

于 2013-04-02T23:39:18.433 に答える