Algorithim is not fast enough to move mouse smoothly

Code:

#include <Mouse.h>
#include <hiduniversal.h>
#include "hidmouserptparser.h"
#include <USBController.h>

USBController controller;
USB Usb;
HIDUniversal Hid(&Usb);
HIDMouseReportParser Mou(nullptr);
String x;
String y;
boolean isX; 
int recv;

void setup() {
	Mouse.begin();
	Usb.Init();
	delay(200);

	if (!Hid.SetReportParser(0, &Mou))
		ErrorMessage<uint8_t > (PSTR("SetReportParser"), 1);
}

void loop() {
  	Usb.Task();
	recv = controller.read();
  	if ((recv-48)==29){ // recv = M
		// , : -4
		// /n : -38
		x="";
		y="";
		isX = true; 
		while (true){
			recv = controller.read();
			if (recv!=0 && recv!=2){ // filter out 0 and 2
				if ((recv-48)==-38){ // -38 is a line break
					break;
				}
				else if ((recv-48)==-4){ // switch to y value after comma
					isX = false;
					continue;
				}
			
				if (isX){
					x+=String(recv-48);
				}
				else{
					y+=String(recv-48);
				}
			}
		}
		Mouse.move(x.toInt(),y.toInt());
  	}
}

void onButtonDown(uint16_t buttonId) {
	Mouse.press(buttonId);
}

void onButtonUp(uint16_t buttonId) {
	Mouse.release(buttonId);
}

void onMouseMove(int8_t xMovement, int8_t yMovement, int8_t scrollValue) {
	Mouse.move(xMovement, yMovement, scrollValue);
}

I'm using an arduino leonardo with a usb host shield. My goal is to move my cursor to a specific location based on data read in. Format of the data read in is "MX,Y" and a line break
In the main loop I read in the data and subtract 48 because that's the real number. Then I check if the first character is M because that is where the beginning of my data starts. Then I filter out 0 and 2 because those values are useless to me and randomly appear. I loop to add the integer values as strings to x then switch to y after the comma appears. Then I convert the string values to int and mouse.move. The problem is the mouse movement is slow and jittery and I think its because the toInt and String() methods are slowing it down. How could I improve my algorithm?

“Don’t know why” is a terrible place to start from when becoming a programmer

It’s a good reminder that you should dig a bit deeper, or pay someone that “does know why”

Sorry I shouldn't have included those in my question and have tried searching. The USBController library I'm using has virtually no documentation. Could you help me out?

So i’m guessing you understand those lines of code now.

There a few lines and comments that are nonsense, maybe work through those, and the whole program will start making more sense.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.