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
}
}
Andy