I am trying to make a code that makes the arduino move the mouse smoothly over a certain amount of time
arduino code:
#include <Mouse.h>
int currentX = 0;
int currentY = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Mouse.begin();
currentX=0;
currentY=0;
}
int getInt()
{
String inputString = "";
while (Serial.available()) {
char c = Serial.read();
if (c == ' ') {
break;
}
inputString += c;
}
return inputString.toInt();
}
void loop() {
String inputString = "";
if(Serial.available())
{
// Convert string to integer
int x = getInt();
int y = getInt();
long time = getInt();
// Move mouse and delay
int xToMove = 1000;
int yToMove = 1000;
int prevXMove = 0;
int prevYMove = 0;
float t = 0;
long startTime = new int(millis());
while (millis()-startTime<time)
{
t = (millis()-startTime)/time;
int xMove = round(t*xToMove)-prevXMove;
int yMove = round(t*yToMove)-prevYMove;
Mouse.move(xMove,yMove);
currentX += xMove;
currentY += yMove;
prevXMove += xMove;
prevYMove += yMove;
delay(10);
}
if (!Serial.available())
{
Serial.println("ACK");
}
}
}
the code gets the x y and time data from serial and calculates how much it needs to move by subtracting the current position from it. the while(millis()-startTime<time) runs for time milliseconds and t is a variable between zero and one which is a fraction of how far through the stroke it should be. So i multiply t by the amount to move and then subtract the prev movements from it. I move the mouse there and add the coordinates to the current position variable and the previous position variable. When i send the serial data however, no movement happens. I am very confused. what would be the solution to this?
python code:
ser = Serial("COM5",9600)
ser.write("1000 1000 5000".encode()) #move to 1000, 1000 over 5 seconds
while True:
if ser.readline().strip() == b"ACK":
break
I don't understand. What do you mean, move the Arduino? Surely not literally move the board.
Is this just code snippets from a larger project? If so, what's the goal?
I'm not trying to be obtuse. What I mean is, what is going on with the Python program that you want to use an Arduino in?
I have never used a mouse or keyboard through an Arduino, perhaps if I wanted to control a robot or something it might be an option, but I have never had to go that route.
Trying to understand your overall reason for this, ie your goal, to see if what I have in mind based on the limited info is something worth suggesting.
Yes this is just kind of like its own thing. My goal is to give the arduino coordinates and time and for it to move the mouse smoothly over that amount a time to those coordinates. if there is a better way to get about this than i did then please tell me!
I don't know if it's better, but Processing and Arduino were a match made in heaven. You can use your Serial stuff to your PC, all the mouse and keyboard stuff is already built into Processing. Like Arduino, Processing is free and open source, here is the example sketch "mouse2D" from Processing 4. Note: I use Processing because I'm an average joe hobbyist. I haven't learned Python yet so that's why I don't use Python. Processing is based in Java I think.
/**
* Mouse 2D.
*
* Moving the mouse changes the position and size of each box.
*/
void setup() {
size(640, 360);
noStroke();
rectMode(CENTER);
}
void draw() {
background(51);
fill(255, 204);
rect(mouseX, height/2, mouseY/2+10, mouseY/2+10);
fill(255, 204);
int inverseX = width-mouseX;
int inverseY = height-mouseY;
rect(inverseX, height/2, (inverseY/2)+10, (inverseY/2)+10);
}
So where is the mouse being moved in this script? also for my script, i think what might be happening is that the Mouse.move only takes values from -127 to 127 or something like that, but it worked when i just had
Do you mean the one I posted? It's not an Arduino sketch. It's a Processing 4 sketch. Sorry I didn't exactly make that clear. I was responding quickly as my daughter was waiting to watch Fallout with me. https://processing.org/
Processing is great, similar IDE to Arduino. Lots of options, as I mentioned above.
To your original question, I don't know. That's why I wanted to know more.
I use Processing for mouse/keyboard/Arduino interaction, thought maybe you'd find it useful for your goal.