Skip to content

Instantly share code, notes, and snippets.

@dolphin2410
Created May 18, 2022 13:25
Show Gist options
  • Save dolphin2410/5f8959c0fe3596e0c8eadbcd3d08fe45 to your computer and use it in GitHub Desktop.
Save dolphin2410/5f8959c0fe3596e0c8eadbcd3d08fe45 to your computer and use it in GitHub Desktop.
Arduino writes bluetooth input to the keyboard
#include <SoftwareSerial.h>
#include <Keyboard.h>
#define BLUETOOTH_RX 14 // Change this (RX)
#define BLUETOOTH_TX 16 // Change this (TX)
SoftwareSerial BTserial(BLUETOOTH_RX, BLUETOOTH_TX);
void setup() {
Serial.begin(9600);
BTserial.begin(9600);
Serial.println("Serial and Bluetooth Serial ready");
Keyboard.begin();
Serial.println("Keyboard ready");
}
void loop() {
if (BTserial.available()) {
String s = "";
char c;
while((c = BTserial.read()) != -1) {
s += c;
delay(10);
}
int N = s.length() + 1;
for (int i = 0; i < N; i++) {
Keyboard.write(s[i]);
delay(10);
}
Keyboard.write(0x0A);
Serial.println("Bluetooth -> Arduino: " + s);
}
if (Serial.available()) {
String s = "";
char c;
while((c = Serial.read()) != -1) {
s += c;
delay(10);
}
delay(10);
Serial.println("Arduino -> Bluetooth: " + s);
BTserial.print(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment