Sending Mouse Coordinates to Arduino from Python

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.

Which part is not working ?

Does the Arduino receive the message from Python ?

I'm not sure, I did try to print the x and y coordinates over to Python again to debug, but I couldn't get that to work either. I could just try to read then send the value back to python as soon as it is sent.

imbatatthis:
I'm not sure, I did try to print the x and y coordinates over to Python again to debug, but I couldn't get that to work either. I could just try to read then send the value back to python as soon as it is sent.

To make sure your communication works, you can use SerialTransfer.h and pySerialTransfer for fast and reliable communication between your Arduino and Python. SerialTransfer.h is installable via the Arduino IDE Libraries Manager, pySerialTransfer is pip-installable, and both come with examples.

imbatatthis:
I'm not sure, I did try to print the x and y coordinates over to Python again to debug, but I couldn't get that to work either. I could just try to read then send the value back to python as soon as it is sent.

Why not just print the values received by the Arduino ?

You can test using Serial monitor instead of your python script. Just send the text from Serial monitor. If that does not work, add some serial prints in readCoordinates() so you know what is going on.

I'm not sure if the Leonardo can act as a Serial device over the regular USB connection at the same time that it acts as a mouse.

If not you could connect the Python program to Serial1 (pins 0 and 1) on the Leonardo using a USB-TTL cable.

...R
Simple Python - Arduino demo

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