![]() |
![]() |
파이션소스
import mouse, sys
import time
import serial
mouse.FAILSAFE=False
ArduinoSerial=serial.Serial('com3',9600) #아두이노 소스 통신과 일치 해야 합니다.
time.sleep(1) #1초간 정치
while 1:
data=str(ArduinoSerial.readline().decode('ascii')) #데이터를 읽습니다.
(x,y,z)=data.split(":") # x,y,z를 지정
(X,Y)=mouse.get_position() #현재 위치의 읽습니다.
(x,y)=(int(x),int(y)) #정수 변환
mouse.move(X+x,Y-y) #마우스 포인터 이동
if '1' in z: # 스위치 상태를 읽습니다.
mouse.click(button="left") # 왼쪽 버튼을 읽습니다.
아두이노 소스
void setup()
{
Serial.begin(9600);
pinMode(9,INPUT);
digitalWrite(9,HIGH);
}
int prev_state=0; // 현재 스위치 상태
void loop() {
int z=0,xpos=0,ypos=0;
int x=analogRead(A0);
int y=analogRead(A1);
int sensitivity=10; // 사용하기 편하게 감도 조절
if(x>=550) // 위로 이동
xpos=map(x,550,1023,0,sensitivity);
if(x<=450) // 아래로 이동
xpos=map(x,450,0,0,-sensitivity);
if(y>=550) // 오른쪽 이동
ypos=map(y,550,1023,0,sensitivity);
if(y<=450) // 왼쪽 이동
ypos=map(y,450,0,0,-sensitivity);
int curr_state=digitalRead(9);
if(curr_state==1 && prev_state==0) // 스위치 버튼 동작 ,현재 상태1, 이전 상태가 0이면,z=1
z=1;
else
z=0;
if(xpos!=0 or ypos!=0 or z==1) // 조이 스틱이 이동 할 경우
{
Serial.print(xpos); // ":" 구분하여 파이션으로 송신
Serial.print(":");
Serial.print(ypos);
Serial.print(":");
Serial.println(z);
}
prev_state=curr_state;
delay(10); // for normal operation
}
※ 셀 실행
Python 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:18:16) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import mouse, sys
>>> import time
>>> import serial
>>> mouse.FAILSAFE=False
>>> ser=serial.Serial('COM3',9600)
>>> time.sleep(1)
>>>
>>> while 1:
data=str(ser.readline().decode('ascii'))
(x,y,z)=data.split(":")
(X,Y)=mouse.get_position()
(x,y)=(int(x),int(y))
mouse.move(X+x,Y-y)
if '1' in z:
mouse.click(button="left")
출처 : create.arduino.cc/projecthub/shubhamsantosh99/joystick-controlled-mouse-af2939
'Arduino<>Python' 카테고리의 다른 글
아두니노와 파이션(firmata) -[1] (0) | 2021.04.02 |
---|---|
조이스틱 으로 그래픽 생성 (0) | 2021.03.02 |
PA004. 파이션 이두이노 버튼 turtle head 제어 (0) | 2021.03.01 |
PA003. 파이션 아두이노 조이스틱 그래픽(2) (0) | 2021.03.01 |
PA002. 파이션아두이노 버튼그래픽(1) (0) | 2021.03.01 |