Arduino<>Python
PA003. 파이션 아두이노 조이스틱 그래픽(2)
spring99
2021. 3. 1. 08:07
import serial
from turtle import Turtle, Screen
def HVmove(T, S, forwardAmount):
for i in range (forwardAmount):
T.forward(i)
S .update()
def JoyMove (T, S, ser):
xy= ser.readline()
xy=xy.decode('utf-8')
#xy=xy.rstlip()
xy=xy.split(",")
horizontal=int(xy[0])
vertical =int(xy[1])
if (horizontal >0):
T.setheading(0)
HVmove(T, S, horizontal)
elif (horizontal <0):
T.setheading(180)
HVmove(T, S, horizontal * -1)
elif (vertical < 0):
T.setheading (90)
HVmove(T, S, vertical * -1)
elif (vertical >0):
T.setheading (270)
HVmove (T, S,vertical)
T = Turtle()
S = Screen()
ser = serial.Serial('COM3')
T.shape("turtle")
T. speed(0)
S. tracer(0,0)
while True:
JoyMove(T, S, ser)
int verti=A0;
int hori=A1;
void setup() {
Serial.begin(9600);
}
void loop() {
String vertical = String(map((analogRead(verti) + 18), 0, 1023, -3, +3));
String horizontal = String(map((analogRead(hori) -18), 0, 1023, -3, +3));
if (vertical != "0" || horizontal != "0") {
String dataToSend ="";
dataToSend += vertical;
dataToSend += ",";
dataToSend += horizontal;
Serial.println(dataToSend);
delay(40);
}}