Skip to content

Instantly share code, notes, and snippets.

@scottyob
Last active September 8, 2022 09:02
Show Gist options
  • Save scottyob/6849165 to your computer and use it in GitHub Desktop.
Save scottyob/6849165 to your computer and use it in GitHub Desktop.
Arduino Leonardo HID keyboard and mouse via Seeeduino Bluetooth Module.
/*
This code will be used as a demo to controll the mouse and keyboard from an Arduino Leonardo.
It shows an example of recieving commands from a remote Bluetooth endpoint (be it computer, mobile phone, etc) and using those commands to control
the mouse and keyboard of a locally connected USB target.
Any character that gets recieved will be typed into the keyboard, unless that character is a '#' which we will use to toggle between mouse and keyboard modes
Once in mouse mode, the following characters will be used as commands:
'u': Move the mouse up
'd': Move the mouse down
'l': Move the mouse left
'r': Move the mouse right
'#': Toggle back into keyboard mode.
*/
/* Upload this sketch into Seeeduino and press reset*/
#include <SoftwareSerial.h> //Software Serial Port
#define RxD 8
#define TxD 7
SoftwareSerial blueToothSerial(RxD,TxD); //Serial I/O Object for talking with the Bluetooth Shield
bool mouseMode = false; //if we are currently controlling the mouse
void setup()
{
Serial.begin(9600);
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
setupBlueToothConnection();
Mouse.begin();
Keyboard.begin();
}
void loop()
{
//We have a command to read
if(blueToothSerial.available()) {
char recieved;
recieved = blueToothSerial.read();
if(recieved == '#') {
//We have recieved the 'switch' character, change modes
mouseMode = !mouseMode;
return;
}
if (mouseMode) {
//We are in a mode to control the mouse
switch(recieved) {
case 'u':
Mouse.move(0, -40);
break;
case 'd':
Mouse.move(0, 40);
break;
case 'l':
Mouse.move(-40, 0);
break;
case 'r':
Mouse.move(40, 0);
break;
}
}
else {
//We are in keyboard mode
Keyboard.write(recieved);
}
}
}
void setupBlueToothConnection()
{
blueToothSerial.begin(9600); //Set BluetoothBee BaudRate to (9600) the default baud rate is 38400
blueToothSerial.print("\r\n+STWMOD=0\r\n"); //set the bluetooth work in slave mode
blueToothSerial.print("\r\n+STNA=SeedBT\r\n"); //set the bluetooth name as "SeedBT"
blueToothSerial.print("\r\n+STOAUT=1\r\n"); // Permit Paired device to connect me
blueToothSerial.print("\r\n+STAUTO=0\r\n"); // Auto-connection should be forbidden here
delay(2000); // This delay is required.
blueToothSerial.print("\r\n+INQ=1\r\n"); //make the slave bluetooth inquirable
delay(2000); // This delay is required.
blueToothSerial.flush();
}
@neuhaus
Copy link

neuhaus commented Nov 25, 2015

I'm interested in the opposite direction, i.e. receive data from a USB HID device and pass it on via Bluetooth HID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment