2010年6月1日 星期二

Control a Servo by a Mouse via python and arduino

After I watched this interesting movie, I tried to do it by using python. However, I am a novel of python and arduino, so I spent lots of time to find the reference.



In python, I need:
1. win32gui module: to get the mouse position
2. pySerial module: to send the data to arduino


Here is my python code


import win32gui
import time
import serial

ser = serial.Serial(4)

while (True):
x,y = win32gui.GetCursorPos()
time.sleep(0.2)
pos=int(179*x/1599)
ser.write(chr(pos))
print x,y



And my code in arduino


#include

Servo myservo;
int val;

void setup(){
myservo.attach(9);
Serial.begin(9600);
}

void loop(){
if(Serial.available()){
val=Serial.read();
myservo.write(val);
delay(15);
}
}

0 意見:

張貼留言