Components and supplies
Arduino UNO
Dual axis Joystick
Apps and platforms
IDLE(Python GUI)
Arduino IDE
Project description
Code
Python Code
python
1# Joystick controlled mouse 2# By Shubham Santosh 3# last edited 12/11/2020 4import mouse, sys 5import time 6import serial 7 8mouse.FAILSAFE=False 9ArduinoSerial=serial.Serial('com3',9600) #Specify the correct COM port 10time.sleep(1) #delay of 1 second 11 12while 1: 13 data=str(ArduinoSerial.readline().decode('ascii')) #read the data 14 (x,y,z)=data.split(":") # assigns to x,y and z 15 (X,Y)=mouse.get_position() #read the cursor's current position 16 (x,y)=(int(x),int(y)) #convert to int 17 mouse.move(X+x,Y-y) #move cursor to desired position 18 if '1' in z: # read the Status of SW 19 mouse.click(button="left") # clicks left button 20 21
Python Code
python
1# Joystick controlled mouse 2# By Shubham Santosh 3# last edited 12/11/2020 4import mouse, sys 5import time 6import serial 7 8mouse.FAILSAFE=False 9ArduinoSerial=serial.Serial('com3',9600) #Specify the correct COM port 10time.sleep(1) #delay of 1 second 11 12while 1: 13 data=str(ArduinoSerial.readline().decode('ascii')) #read the data 14 (x,y,z)=data.split(":") # assigns to x,y and z 15 (X,Y)=mouse.get_position() #read the cursor's current position 16 (x,y)=(int(x),int(y)) #convert to int 17 mouse.move(X+x,Y-y) #move cursor to desired position 18 if '1' in z: # read the Status of SW 19 mouse.click(button="left") # clicks left button 20 21
Arduino Code
arduino
1//////////////////////////////// 2// Joystick controlled mouse/// 3/// by Shubham Santosh//////// 4///////////////////////////// 5 6 7void setup() 8{ 9 Serial.begin(9600); 10 pinMode(9,INPUT); // SW pin 11 digitalWrite(9,HIGH); 12 13} 14int prev_state=0; // previous state of switch 15void loop() { 16 int z=0,xpos=0,ypos=0; 17 int x=analogRead(A0); 18 int y=analogRead(A1); 19 int sensitivity=10; // you can adjust the sensitivity based on your comfort 20 if(x>=550) // when moved up 21 xpos=map(x,550,1023,0,sensitivity); 22 if(x<=450) // when moved down 23 xpos=map(x,450,0,0,-sensitivity); 24 if(y>=550) // when moved right 25 ypos=map(y,550,1023,0,sensitivity); 26 if(y<=450) // when moved left 27 ypos=map(y,450,0,0,-sensitivity); 28 int curr_state=digitalRead(9); 29 if(curr_state==1 && prev_state==0) // when SW is pressed 30 z=1; 31 else 32 z=0; 33 if(xpos!=0 or ypos!=0 or z==1) // prints only when the joystick is moved 34 { 35 Serial.print(xpos); // print the data and separating by ":" 36 Serial.print(":"); 37 Serial.print(ypos); 38 Serial.print(":"); 39 Serial.println(z); 40 } 41 prev_state=curr_state; 42 delay(10); // for normal operation 43} 44
Downloadable files
Sketch
Sketch
Sketch
Sketch
Comments
Only logged in users can leave comments