In this video lesson we show how to control the position of a game paddle in Python using data from a joystick controlled by Arduino. the data is passed from Arduino to Python, and then the position of the game paddle is dynamically adjusted in real time. On the arduino side, this is the code which we developed:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// ==================================================================== // DISCLAIMER: // This code is provided as-is for educational and experimental // purposes only. The author makes no representations or warranties of // any kind concerning the safety, suitability, or accuracy of this // code. Use at your own risk. The author assumes no liability for any // damages, system failures, security breaches, or network issues // resulting from the use or implementation of this script. // ==================================================================== int potX=A0; int potY=A1; int potZ=2; int dl=100; int xVal=0; int yVal=0; int zVal=0; void setup() { // put your setup code here, to run once: Serial.begin(115200); pinMode(potX,INPUT); pinMode(potY,INPUT); pinMode(potZ,INPUT); digitalWrite(potZ,HIGH); delay(dl); } void loop() { // put your main code here, to run repeatedly: xVal=analogRead(potX); yVal=analogRead(potY); zVal=digitalRead(potZ); Serial.print(xVal); Serial.print(','); Serial.print(yVal); Serial.print(','); Serial.println(zVal); delay(25); } |
And on the Python side, this is the code which we used:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== from vpython import * import serial arduinoData=serial.Serial('com3',115200) roomX=12 roomY=10 roomZ=16 wallT=.5 wallColor=vector(1,1,1) wallOpacity=.8 frontOpacity=.1 marbleR=.5 ballColor=vector(0,0,1) myFloor=box(size=vector(roomX,wallT,roomZ),pos=vector(0,-roomY/2,0),color=wallColor,opacity=wallOpacity) myCeiling=box(size=vector(roomX,wallT,roomZ),pos=vector(0,roomY/2,0),color=wallColor,opacity=wallOpacity) leftWall=box(size=vector(wallT,roomY,roomZ),pos=vector(-roomX/2,0,0),color=wallColor,opacity=wallOpacity) rightWall=box(size=vector(wallT,roomY,roomZ),pos=vector(roomX/2,0,0),color=wallColor,opacity=wallOpacity) backWall=box(size=vector(roomX,roomY,wallT),pos=vector(0,0,-roomZ/2),color=wallColor,opacity=wallOpacity) frontWall=box(size=vector(roomX,roomY,wallT),pos=vector(0,0,roomZ/2),color=wallColor,opacity=frontOpacity) marble=sphere(color=ballColor,radius=marbleR) paddleX=2 paddleY=2 paddleZ=.2 paddleOpacity=.8 paddleColor=vector(0,.8,.6) paddle=box(size=vector(paddleX,paddleY,paddleZ),pos=vector(0,0,roomZ/2),color=paddleColor,opacity=paddleOpacity) marbleX=0 deltaX=.1 marbleY=0 deltaY=.1 marbleZ=0 deltaZ=.1 while True: while arduinoData.inWaiting()==0: pass dataPacket=arduinoData.readline() print('data is: ',dataPacket) dataPacket=str(dataPacket,'utf-8') dataPacket.strip('\r\n') splitPacket=dataPacket.split(',') x=float(splitPacket[0]) y=float(splitPacket[1]) z=float(splitPacket[2]) padX=(roomX/1023.)*x -roomX/2 padY=(-roomY/1023.)*y+roomY/2 marbleX=marbleX+deltaX marbleY=marbleY+deltaY marbleZ=marbleZ+deltaZ if marbleX+marbleR>(roomX/2-wallT/2) or marbleX-marbleR<(-roomX/2+wallT/2): deltaX=deltaX*(-1) marbleX=marbleX+deltaX if marbleY+marbleR>(roomY/2-wallT/2) or marbleY-marbleR<(-roomY/2+wallT/2): deltaY=deltaY*(-1) marbleY=marbleY+deltaY if marbleZ+marbleR>(roomZ/2-wallT/2) or marbleZ-marbleR<(-roomZ/2+wallT/2): deltaZ=deltaZ*(-1) marbleZ=marbleZ+deltaZ marble.pos=vector(marbleX,marbleY,marbleZ) paddle.pos=vector(padX,padY,roomZ/2) |