Hey guys, I just got an Arduino Leonardo a couple of days ago and I'm trying to jump into the deep end of coding. I've only ever made scripts for the AHK language(similar to C++), but I discovered that you can emulate mouse and keyboard movements with a Arduino, so I wanted to test it out. I'm mainly doing this to teach myself coding and have fun.
Code objective:
- In python -
- Get the current mouse position and send that over to Arduino IDE
- The mouse position string is formatted to strip the parentheses for the Arduino function
-In Arduino-
- Receive the mouse coordinates
- Read the serial input by bytes
- Convert the input into a integer
- Split the x and y coordinates into two different variable integers to feed into the mouse.move function, using the (, ) as an indicator to split
Current un-working code
Python
import serial
import win32api
import time
Ard = serial.Serial(port='COM5', baudrate=115200)
XY = win32api.GetCursorPos()
#Convert = (str(XY).strip('()')) // I was testing splitting the string first in python
#Xcord, Ycord = Convert.split(", ")
#print (Xcord)
#print (Ycord)
Ard.write(str(XY).strip('()').encode())
Arduino
#include <Keyboard.h>
#include <Mouse.h>
int buf = 0;
int xcoordinate = 0;
int ycoordinate = 0;
void setup()
{
Serial.begin(115200);
Mouse.begin();
}
void loop()
{
if (Serial.available() > 0) {
readCoordinates();
if(Serial.available() == 0){
if (xcoordinate > 0 && ycoordinate > 0){
Mouse.move(xcoordinate, ycoordinate);
}
}
}
}
void readCoordinates(){
byte character = Serial.read(); //read the first byte on serial
if(character != 10 && character != ','){ //newline(10) and , are special
buf = buf*10;
buf += (int)(character - '0'); //these two lines turn the string into an integer
} else if(character == ','){
xcoordinate = buf; //after a comma the buffer has the x coordinate
buf = 0;
} else {
ycoordinate = buf; //after a space the buffer has the y coordinate
buf = 0;
}
}
Hopefully I'm not asking for too much help, I know I'm very new to this, feel free to point out my bad code.