Hello,
I have a USB Host Shield and an Arduino Leonardo R3. All the components are working properly—there are no soldering or other hardware issues. When I test with the library below, everything works fine.
With the library, the orange LED is always on. But with my custom code, it stays off—even when I move or click the mouse.
However, I have a custom code that, when uploaded to the Arduino, establishes a connection with the mouse (the mouse receives power), but it doesn’t detect any mouse movement or clicks.
How can I make my code compatible with my mouse? AI couldn't solved.
Byte logs:
1- buttons
2- dx
3- dy
4- scroll
5- null, always 00

Custom code:
#define RAWHID_TX_INTERVAL 1
#include <SPI.h>
#include <Usb.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <HID-Project.h>
#include <HID-Settings.h>
USB Usb;
USBHub Hub(&Usb);
HIDUniversal Hid(&Usb);
uint8_t rawBuf[64];
// Parser for your USB-Host-Shield mouse
class MouseParser : public HIDReportParser {
uint8_t prevBtns = 0;
const uint8_t masks[5] = {0x01, 0x02, 0x04, 0x08, 0x10};
void Parse(USBHID*, bool isRptID, uint8_t len, uint8_t *buf) override {
uint8_t off = isRptID ? 1 : 0;
if (len < off + 6) return;
uint8_t btns = buf[off + 0];
int8_t dx = (int8_t)buf[off + 1];
int8_t dy = (int8_t)buf[off + 3];
int8_t wheel = (int8_t)buf[off + 5];
// Buttons 1–5
for (uint8_t i = 0; i < 5; i++) {
uint8_t m = masks[i];
if ((btns & m) && !(prevBtns & m)) BootMouse.press(m);
if (!(btns & m) && (prevBtns & m)) BootMouse.release(m);
}
prevBtns = btns;
// Move + scroll
BootMouse.move(dx, dy, wheel);
}
} mouseParser;
void setup() {
BootMouse.begin();
RawHID.begin(rawBuf, sizeof(rawBuf));
RawHID.enable();
Hid.SetReportParser(0, &mouseParser);
if (Usb.Init() == -1) while (1);
}
void loop() {
Usb.Task();
int len = RawHID.read();
if (len > 0) {
const uint8_t masks[5] = {0x01, 0x02, 0x04, 0x08, 0x10};
uint8_t btns = rawBuf[0];
int8_t dx = (int8_t)rawBuf[1];
int8_t dy = (int8_t)rawBuf[2];
// Press+release all requested buttons
for (uint8_t i = 0; i < 5; i++) {
uint8_t m = masks[i];
if (btns & m) {
BootMouse.press(m);
delayMicroseconds(100); // quick release
BootMouse.release(m);
}
}
BootMouse.move(dx, dy, 0);
RawHID.enable();
}
}