# Import the required libraries for this script import math, string, time, serial, win32api, win32con # The port to which your Arduino board is connected port = 'COM3' # Invert y-axis (True/False) invertY = False # The cursor speed cursorSpeed = 20 # The baudrate of the Arduino program baudrate = 19200 # Variables indicating whether the mouse buttons are pressed or not leftDown = False rightDown = False # Variables indicating the center position (no movement) of the controller midAccelX = 530 # Accelerometer X midAccelY = 510 # Accelerometer Y midAnalogY = 134 # Analog Y # Connect to the serial port ser = serial.Serial(port, baudrate, timeout = 1) # Wait 1s for things to stabilize time.sleep(1) count=1 # While the serial port is open while ser.isOpen(): count=count+1 # Read one line line = ser.readline() # Strip the ending (\r\n) line = string.strip(line, '\r\n') # Split the string into an array containing the data from the Wii Nunchuk line2 = string.split(line, ' ') print line2[0] print line2[1] print line2[2] print line2[3] print line2[4] print line2[5] print line2[6] if (count > 3): analogX = int(line2[0]) analogY = int(line2[1]) accelX = int(line2[2]) accelY = int(line2[3]) accelZ = int(line2[4]) zButton = int(line2[5]) cButton = int(line2[6]) if(zButton and not leftDown): leftDown = True win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0,0,0) elif(leftDown and not zButton): leftDown = False win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0,0,0) if(cButton and not rightDown): rightDown = True win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,0,0,0,0) elif(rightDown and not cButton): rightDown = False win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,0,0,0,0) if(abs(analogY - midAnalogY) > 5): win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL,0,0,int(math.floor((analogY - midAnalogY)/2)),0) else: win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL,0,0,0,0) dx = 0 dy = 0 if(abs(accelX - midAccelX) > 20): dx = int(math.floor((accelX - midAccelX)*cursorSpeed/400)) if(abs(accelY - midAccelY) > 20): dy = int(math.floor((accelY - midAccelY)*cursorSpeed/400)) if invertY: dy = dy*-1 win32api.mouse_event(win32con.MOUSEEVENTF_MOVE,dx,dy,0,0) ser.close()