jasrajdevs
Published © Apache-2.0

Arduino mouse

Just a mouse.

IntermediateFull instructions provided15 minutes638
Arduino mouse

Things used in this project

Story

Read more

Schematics

circuit

connect according to it

Code

Arduino code

C/C++
upload it
#define left 8
#define right 9
#define x A0
#define y A1
#define v3 7
int X;
int Y;
int LEFT;
int RIGHT;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(v3, OUTPUT);
  digitalWrite(v3, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:
  X = analogRead(x);
  Y = analogRead(y);
  LEFT = digitalRead(left);
  RIGHT = digitalRead(right);

  Serial.print(X);
  Serial.print(",");
  Serial.print(Y); 
  Serial.print(",");
  Serial.print(LEFT);
  Serial.print(",");
  Serial.print(RIGHT);
  Serial.print(",");
  Serial.println("0");
  
}

Python code

Python
run it
import win32con, win32api
import serial.tools.list_ports


port = serial.Serial()
port.baudrate = 9600
port.port = "COM3"
port.open()

s = 7

def move(D):
    if (D=='left'):
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, -s, 0)
    elif (D=='right'):
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, s, 0)
    elif(D=='up'):
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, -s)
    elif(D=='down'):
        win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 0, s)

def lclick():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
def lup():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
def rclick():
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0)
def rup():
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0)

def main():
    while True:
        if port.in_waiting:
            portElements = port.readline()
            a = portElements.decode('utf')
            Final = a.split(',')
            final = list(Final)
            ux = final[0]
            uy = final[1]   
       
            ul = final[3]
            ur = final[2] 

            x = int(ux)
            y = int(uy)  
            l = int(ul)
            r = int(ur)

            if(y>900):
                move('left')
            elif(y<20):
                move('right')
            if (x>900):
                move('down')
            elif(x<150):
                move('up')
            else:
                move('.')
            if(r==0):
                lclick()
            elif(r==1):
                lup()

            if(l==0):
                rclick()
                rup()
            else:
                pass
            
            
    
main()

Credits

jasrajdevs
7 projects • 10 followers
Developing since 2020 july..... I am a student.

Comments