2

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.


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()); 
 }
}
asked May 30, 2020 at 3:57
6
  • 1
    what do you mean control a bluetooth mouse? ... a mouse is an input device, not an output device Commented May 30, 2020 at 5:33
  • I think the OP want to configure and use the RN42 (which is a bluetooth chip) as HID mouse to use with a PC. The Arduino would then read from the joystick and send the data to the RN42 to move the cursor correspondingly on the PC Commented May 30, 2020 at 22:04
  • @Tadashi Have you GPIO11 of the RN42 on high level, when powering up? Commented May 30, 2020 at 22:13
  • Thanks for the responses. The problem with getting no response from the mouse was that the bluetooth did not auto reconnect to the computer after powering down. I solved this by sending the command SM,6 for auto reconnect and then R,1 to reboot. Commented May 31, 2020 at 20:23
  • However, I ran into but fixed a few bugs after I connected the RN 42. For anyone trying to make an HID mouse, I have a few troubleshooting tips. My mouse was swerving to the top left of my screen because the resting position of the joystick was not at the center of the range (12) that I wanted the analog pot values to be mapped to. This was because my joystick did not output 1023 as a max value. I scaled 1023 down by .6 to solve this. Another issue was that upon moving the joystick, my mouse would follow a single trajectory and not move anywhere else. Increasing the response delay solved this. Commented May 31, 2020 at 20:28

1 Answer 1

2

The problem with getting no response from the mouse was that the bluetooth did not auto reconnect to the computer after powering down. I solved this by sending the command SM,6 for auto reconnect and then R,1 to reboot.

answered May 31, 2020 at 20:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.