1

I am using RN-42 with Arduino Micro and able to connect RN-42 in HID mode and it's connected to the PC as Mouse. I am using BPLib (https://github.com/witnessmenow/BPLib) for Bluetooth communication. I am not sure where I am making a mistake as whenever I roll the ball on my trackball there is no movement detected. But it is working fine when using it as a wired solution with a mouse library with Arduino.

Here is the code using Serial1

#include <Wire.h>
#include "I2Cdev.h"
#include <PMW3360.h>
#include <BPLib.h>

// Mouse Movement Variables
#define SS  17          // Slave Select pin. Connect this to SS on the module
PMW3360 sensor;
const unsigned long accumulationTime = 10; // Accumulate data for --- milliseconds
const int sensitivity = 500;
const int DPI = 400;

// Bluetooth Variables
// Bluetooth Variables
#define RX_PIN 0 // connect to TXD of module
#define TX_PIN 1 // connect to RXD of module (logic level 3.3v!)

Serial1 swSer(RX_PIN, TX_PIN, false); // Only three arguments
BPLib *BPMod;

void setup()
{
  Serial.begin(9600);  
  Serial1.begin(9600); // Set the baud rate for hardware serial
  BPMod = new BPLib(Serial1);
  BPMod->begin(BP_MODE_HID, BP_HID_MOUSE);

  if (sensor.begin(SS)) // Initialize the PMW3360 sensor
  {
    Serial.println("Sensor initialization succeeded");
  } 
  else
  {
    Serial.println("Sensor initialization failed");
  }
  sensor.setCPI(DPI);
  //Mouse.begin();
}

void loop()
{
  float Acc_X = 0;
  float Acc_Y = 0;
  bool movementDetected = false;

  unsigned long startTime = millis(); // Record the start time

  // Accumulate readings for the --- duration
  while (millis() - startTime < accumulationTime) {
    PMW3360_DATA data = sensor.readBurst();
    if (data.isOnSurface && data.isMotion) {
      float mdx = constrain(data.dx, -127, 127);
      float mdy = constrain(data.dy, -127, 127);

      Acc_X += mdx;
      Acc_Y += mdy;
      movementDetected = true;
    }
  }

  Acc_X /= DPI;
  Acc_Y /= DPI;

  // Move the mouse based on accumulated values if movement was detected
  if (movementDetected) {
    BPMod->mouseMove(-Acc_X * sensitivity, Acc_Y * sensitivity);
    //Mouse.move(-Acc_X * sensitivity, Acc_Y * sensitivity);
  }
}


Anyone could help to solve the issue? or point out where I am making a mistake.

4
  • The Arduino Micro has a hardware UART on pins 0 and 1 that is usable as Serial1. It is independent of the Serial connection to the PC/serial monitor. SoftwareSerial has caveats. If your error is unrelated to SoftwareSerial you will still have the issue over Serial1 and can eliminate a distraction from your actual problem. Either that or you'll have proven that it plays a significant role in the problem, which would also be good to know. Ultimately you probably don't want SoftwareSerial anyway. Commented Jul 11, 2024 at 12:45
  • I tried to use Serial1, but no luck. Any other suggestions? Commented Jul 11, 2024 at 16:22
  • Suggestion: replace your code in the question with your code making use of Serial1, eliminating any doubt that SoftwareSerial is related to the problem. Commented Jul 11, 2024 at 16:27
  • I edit the code. Please Check. Commented Jul 12, 2024 at 3:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.