In this video lesson we are working towards creating a 3D version of the classic Pong arcade game. In this session, we show how to read data from the joystick using the arduino.
The arduino code developed in this video is included for your convenience below:
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 | int potX=A0; int potY=A1; int potZ=2; 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); } 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); } |
Also, this is the vPython 3D simulation we have developed thus far:
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 | from vpython import * 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) marbleX=0 deltaX=.1 marbleY=0 deltaY=.1 marbleZ=0 deltaZ=.1 while True: rate(20) 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) |