You are currently viewing python code for smartphone controlled mouse using arduino IDE
Python Code For Smartphone Controlled Mouse Using Arduino IDE

python code for smartphone controlled mouse using arduino IDE

Spread the love

In this article we are going to make project : arduino base  smartphone controlled mouse using python code project. so guys let’s start

Step 1: pre-requirement 

  1. Arduino IDE
  2. Bluetooth module (HC-05)
  3. Python 3.7 Envi. setup
  4. Android Application : you need to simply implement Android App using MIT APP INVENTER  you can take the reference from Tabletop robotics.
Python Code For Smartphone Controlled Mouse Using Arduino IDE
Python Code For Smartphone Controlled Mouse Using Arduino IDE

Step 2: Procedure : python code for smartphone controlled mouse using arduino IDE

The sequence of data transfer is as follows-

  •  Send ‘255’ signal to system for operation start purposed
  •  Send 1 byte joystick on x-axis value
  •  Send 1 byte joystick on  y-axis value
  •  Send also status of left click, right click button and scroll status

Remember that one thing data from the application is sent in every 20 ms to the Arduino UNO.

Step 3 : performed below steps on the command prompt. 

Open command prompt and enter the following-

1. cd <paste the path of the python file>

2. py –m pip install –-upgrade pip

3. py –m pip install mouse

4. py -m pip install pyserial

Step 4 : python code for smartphone controlled mouse using arduino IDE :

First import the required library in python 3.7 

import mouse, sys

import time 

import serial



mouse.FAILSAFE=False

ArduinoSerial=serial.Serial('com3',9600)  #Specify the correct COM port

time.sleep(1)                             #delay of 1 second



while 1:

   data=str(ArduinoSerial.readline().decode('ascii'))

   (x,y,z)=data.split(":")  # read the x and y axis data

   (x,y)=(int(x),int(y))   # convert to int

   mouse.move(x,y)         # move the cursor to desired coordinates

   if '1' in z:                       

      mouse.click(button="left")     #clicks mouse button

   elif '2' in z:

      mouse.click(button="right")

   elif '3' in z:

      mouse.wheel(delta=-1)       # Scroll down

   elif '4' in z:

      mouse.wheel(delta=1)       # Scroll up

------------------------------------------------------------------------  

 step 5 : Write the arduino code for smartphone controlled mouse using arduino IDE :

int datareceived[5] {0,0,0,0};          // To store byte from phone

int in_byte = 0;

int array_index = 0;

int l_prev=0,r_prev=0; // previous status of mouse left and right click 

void setup() {

Serial.begin (9600); // starts the serial monitor

}

 int height=0,width=0;

void loop() {

  int clicks=0;

  int sensitivity=20;       // you can adjust the sensitivity

  int xpos=0,ypos=0;

if (Serial.available() > 0) {  //recieve byte from phone

  in_byte= Serial.read(); //store in byte into a variable

  if (in_byte == (255)) { // if the variable is 0 stet the array inxed to 0.

    array_index = 0;

  }

  datareceived[array_index] = in_byte;  //store number into array

  array_index = array_index +1;

  

if(datareceived[1]>=110)

xpos=map(datareceived[1],110,172,0,sensitivity);       // When moved right

if(datareceived[1]<=70)

xpos=map(datareceived[1],60,1,0,-sensitivity);        // When moved left

if(datareceived[2]>=110)

ypos=map(datareceived[2],110,255,0,sensitivity);     // When moved down

if(datareceived[2]<=60)

ypos=map(datareceived[2],70,1,0,-sensitivity);       // When moved up





if(datareceived[3]==1 && l_prev==0)      // TO recognise a single button press

  clicks=1;

else if(datareceived[3]==2 && r_prev==0)

clicks=2;

else if(datareceived[3]==3 || datareceived[3]==4)

clicks=datareceived[3];  //  scroll



l_prev=datareceived[3];

r_prev=datareceived[3];



if(xpos!=0 or ypos!=0 or clicks!=0)       // when either of the joystick is moved or the button is pressed or scrolled

{

height=height+ypos;

width=width+xpos;

if(height>=799)

height=799;

if(height<=0)

height=0;

if(width>=1279)

width=1279;

if(width<=0)

width=0;

Serial.print(width);

Serial.print(":");

Serial.print(height);

Serial.print(":");

Serial.println(clicks);

clicks=0;

}

}

}

Step 6 : result

After the finishing coding work you need to Opening the smartphone application and connect to the bluetooth module and you will notice the cursor movement upon changing the direction of the joystick.

Summary :

In this article we saw python code for smartphone controlled mouse using arduino IDE. if you have any query about this project then free to ask me

Resource : Arduino official site

BEST OF LUCK !!

I am Mr. Sachin pagar Embedded system engineer, the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

sachin Pagar

I am Mr. Sachin pagar Embedded system engineer, the founder of Pythonslearning, a Passionate Educational Blogger and Author, who love to share the informative content on educational resources.

This Post Has One Comment

Leave a Reply