星期六, 2月 06, 2010

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

##CONTINUE##
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);
}
}

5 則留言:

SimpleBear 提到...

恭喜你!!!

TsaiCY 提到...
作者已經移除這則留言。
TsaiCY 提到...

How a disgusting furry hand!!!!

ha ha!!!

匿名 提到...
網誌管理員已經移除這則留言。
Eric Wu 提到...

龜毛男=TsaiCY

哈哈哈哈哈!!!