Bluetooth Trinket keyboard Double key press . Help.

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ando_guest
 
Posts: 5
Joined: Fri Jun 05, 2015 6:12 pm

Bluetooth Trinket keyboard Double key press . Help.

Post by ando_guest »

I am using the Bluefruit EZ-Key - 12 Input Bluetooth HID Keyboard Controller in an arcade stick I have built. It works great. The problem is I want more than 12 inputs. So I have wired a pro Trinket 5V to the RX pin in the Bluefruit EZ-Key.

Unfortunately I have never programmed a microcontroller. So after a lot of reading and hours of frustration, I finally have the code below. This is the result of my recently acquired knowledge and trying to adapt numerous other bits of code which sort of did what I wanted.

The goal is to press the button on pin 4 and have it output a TAB to the Bluefruit EZ-Key RX pin (via pin 3).

My problem is that the TAB is sent on press and release of the button (momentary switch), i.e I get 2 TABs per press instead of one.

Can someone please tell me what I’m doing wrong?

Code: Select all

#include <SendOnlySoftwareSerial.h>

const int TAB_PIN = 4;
const int TX_PIN = 3;

SendOnlySoftwareSerial BT(TX_PIN);

void setup() {
  pinMode(TAB_PIN, INPUT);
  digitalWrite(TAB_PIN, HIGH); // pull-up
  BT.begin(9600);
}

int prevstate = 0;

void keyCommand(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, 
                uint8_t keycode4, uint8_t keycode5, uint8_t keycode6) {
  BT.write(modifiers);  
  BT.write(keycode1); // press #1
  BT.write(keycode2); // press #2
  BT.write(keycode3); // press #3
  BT.write(keycode4); // press #4
  BT.write(keycode5); // press #5
  BT.write(keycode6); // press #6
  BT.write((byte)0x00); //unpress    
}

void loop() {
  int state = digitalRead(TAB_PIN);
  if (prevstate != state) {
    prevstate = state;
    keyCommand(0, 0x09, 0, 0, 0, 0, 0);  //TAB
    keyCommand(0, 0, 0, 0, 0, 0, 0);
    delay(50); // debounce
  }
}
Thanks,
Andy

User avatar
adafruit_support_rick
 
Posts: 35092
Joined: Tue Mar 15, 2011 11:42 am

Re: Bluetooth Trinket keyboard Double key press . Help.

Post by adafruit_support_rick »

Code: Select all

void loop() {
  int state = digitalRead(TAB_PIN);
  if (prevstate != state) {
    prevstate = state;
    if (LOW == state) {  //if the key is pressed
      keyCommand(0, 0x09, 0, 0, 0, 0, 0);  //TAB
      keyCommand(0, 0, 0, 0, 0, 0, 0);
    }
    delay(50); // debounce
  }
}

User avatar
ando_guest
 
Posts: 5
Joined: Fri Jun 05, 2015 6:12 pm

Re: Bluetooth Trinket keyboard Double key press . Help.

Post by ando_guest »

Worked like a charm, thanks a lot!

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”