Hi,
I’m trying to use the RN 42 and Arduino Uno to control a bluetooth mouse. I configured the RN 42 with the commands S~,6 and SH,0220 and R,1 to put the device in HID mouse mode. Is there anything I missed? Below is the sketch I am using to send mouse reports to the bluetooth. The sketch compiles fine, but moving the joystick does not move the cursor. I included a line of code at the end to print on the serial port whatever data is being sent through software serial to the bluetooth. However when I view the serial monitor, I get back a lot of reversed question marks. Anyone know why this is? Thanks in advance.
#include <SoftwareSerial.h>
const int bluetoothTx = 4; // TX pin of RN-42, Arduino D2 (configure as RX pin of Ard)
const int bluetoothRx = 3; // RX pin of RN-42, Arduino D3 (configure as TX pin)
const int button = 5;
const int xAxis = A0; // joystick X axis
const int yAxis = A1; // joystick Y axis
int range = 12; // output range of X or Y movement
int responseDelay = 5; // response delay of the mouse, in ms
int threshold = range / 4; // resting threshold
int center = range / 2; // resting position value
SoftwareSerial BT(bluetoothTx, bluetoothRx); // args SoftwareSerial(RXpin, TXpin)
void mouseCommand(byte buttons, byte x, byte y) {
BT.write((byte)0xFD);
BT.write((byte)0x05);
BT.write((byte)0x02);
BT.write((byte)buttons);
BT.write((byte)x);
BT.write((byte)y);
BT.write((byte)0x00);
}
int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);
// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1023, 0, 12);
/* threshold prevents the mouse from reading small unintentional movements.
If the output reading is outside from the rest position threshold, use it: */
int distance = reading - center;
if (abs(distance) < threshold) {
distance = 0;
}
// return the scaled distance for this axis:
return distance;
}
void setup() {
pinMode(button, INPUT);
pinMode(bluetoothTx, INPUT); // bluetoothTX is equivalent to arduino RX pin
pinMode(bluetoothRx, OUTPUT);
Serial.begin(115200); // Begin the serial monitor at 115200bps
BT.begin(115200); // RN-42 defaults to 115200bps
}
void loop() {
// read and scale the two axes:
int xReading = readAxis(xAxis);
int yReading = readAxis(yAxis);
int buttonState = digitalRead(button);
// move the mouse:
mouseCommand(buttonState, xReading, yReading);
delay(responseDelay);
if(BT.available()) // If there's data being sent to the bluetooth module
{
// Send any characters the bluetooth receives to the serial monitor
Serial.print((char)BT.read());
}
}