Components and supplies
GY-521 MPU-6050 3 Axis Gyroscope + Accelerometer Module For Arduino
Arduino UNO
Pushbutton switch 12mm
Apps and platforms
Processing
Arduino IDE
Project description
Code
Arduino code
arduino
1#include <Bounce.h> 2#include <Wire.h> 3 4const unsigned int Button_Pin=7; 5const int MPU=0x68; 6int16_t AcX,AcY,AcZ; 7const int NUM_AXES=3; 8 9const int BUFFER_SIZE=16; 10int buffer[NUM_AXES][BUFFER_SIZE]; 11int buffer_pos[NUM_AXES]={0}; 12Bounce button(Button_Pin, 20); 13 14void setup(){ 15 Wire.begin(); 16 Wire.beginTransmission(MPU); 17 Wire.write(0x6B); 18 Wire.write(0); 19 Wire.endTransmission(true); 20 Serial.begin(19200); 21 pinMode(Button_Pin, INPUT); 22} 23 24int get_axis(const int axis){ 25 delay(1); 26 buffer[axis][buffer_pos[axis]]=Wire.read()<<8|Wire.read(); 27 buffer_pos[axis]=(buffer_pos[axis]+1)%BUFFER_SIZE; 28 long sum=0; 29 for(int i=0; i<BUFFER_SIZE; i++) 30 sum+= buffer[axis][i]; 31 return round(sum/BUFFER_SIZE); 32 } 33 34 int get_x(){return get_axis(0);} 35 int get_y(){return get_axis(1);} 36 int get_z(){return get_axis(2);} 37 38void loop(){ 39 Wire.beginTransmission(MPU); 40 Wire.write(0x3B); 41 Wire.endTransmission(false); 42 Wire.requestFrom(MPU,6,true); 43 Serial.print(get_x()); 44 Serial.print(" "); 45 Serial.print(get_y()); 46 Serial.print(" "); 47 Serial.print(get_z()); 48 Serial.print(" "); 49 if(button.update()) 50 Serial.println(button.read()==HIGH ? "1" : "0"); 51 else 52 Serial.println("0"); 53 54 delay(50); 55}
Processing code
processing
1import processing.serial.*; 2 3Serial arduinoPort; 4 5final int COLUMNS=7; 6final int ROWS=4; 7final int BALL_RADIUS=8; 8final int BALL_DIAMETER=BALL_RADIUS*2; 9final int MAX_VELOCITY=8; 10final int MARGIN=10; 11final int PADDLE_WIDTH=60; 12final int PADDLE_HEIGHT=15; 13final int BRICK_WIDTH=40; 14final int BRICK_HEIGHT=20; 15final int HEIGHT=300; 16final int LINE_FEED=10; 17 18int px, py; 19int vx, vy; 20int xpos=150; 21int[][] bricks= new int[COLUMNS][ROWS]; 22 23boolean buttonPressed=false; 24boolean paused=true; 25boolean done=true; 26 27void setup(){ 28 size(300, 300); 29 noCursor(); 30 textFont(loadFont("Verdana-Bold-36.vlw")); 31 initGame(); 32 println(Arduino.list()); 33 arduinoPort=new Serial(this, "COM3", 19200); 34 arduinoPort.bufferUntil(10); 35} 36 37void initGame(){ 38 initBricks(); 39 initBall(); 40} 41 42void initBricks(){ 43 for(int x=0; x<COLUMNS; x++) 44 for(int y=0; y<ROWS; y++) 45 bricks[x][y]=1; 46} 47 48void initBall(){ 49 px=width/2; 50 py=height/2; 51 vx=int(random(-MAX_VELOCITY, MAX_VELOCITY)); 52 vy=-2; 53} 54 55void draw(){ 56 background(0); 57 stroke(255); 58 strokeWeight(3); 59 60 done=drawBricks(); 61 if(done){ 62 paused=true; 63 printWinMessage(); 64 } 65 if(paused) 66 printPauseMessage(); 67 else 68 updateGame(); 69 70 drawBall(); 71 drawPaddle(); 72} 73 74boolean drawBricks(){ 75 boolean allEmpty=true; 76 for(int x=0; x<COLUMNS; x++){ 77 for(int y=0; y<ROWS; y++){ 78 if(bricks[x][y]>0){ 79 allEmpty=false; 80 fill(0, 0, 100+y*8); 81 rect( 82 MARGIN+x*BRICK_WIDTH, 83 MARGIN+y*BRICK_HEIGHT, 84 BRICK_WIDTH, 85 BRICK_HEIGHT 86 ); 87 } 88 } 89 } 90 return allEmpty; 91} 92 93void drawBall(){ 94 strokeWeight(1); 95 fill(128, 0, 0); 96 ellipse(px, py, BALL_DIAMETER, BALL_DIAMETER); 97} 98 99void drawPaddle(){ 100 int x=xpos - PADDLE_WIDTH/2; 101 int y=height - 25; 102 strokeWeight(1); 103 fill(128); 104 rect(x, y, 60, 15); 105} 106 107void printWinMessage(){ 108 fill(225); 109 textSize(36); 110 textAlign(CENTER); 111 text("YOU WIN", width/2, height*2/3); 112} 113 114void printPauseMessage(){ 115 fill(128); 116 textSize(16); 117 textAlign(CENTER); 118 text("Press button to continue", width/2, height*5/6); 119} 120 121void updateGame(){ 122 if(ballDropped()){ 123 initBall(); 124 paused=true; 125 } else{ 126 checkBrickCollision(); 127 checkWallCollision(); 128 checkPaddleCollision(); 129 px+=vx; 130 py+=vy; 131 } 132} 133 134boolean ballDropped(){ 135 return py+vy> height - BALL_RADIUS; 136} 137 138boolean inXRange(final int row, final int v){ 139 return px + v > row*BRICK_WIDTH&& 140 px+v < (row+1)*BRICK_WIDTH+BALL_DIAMETER; 141} 142 143boolean inYRange(final int col, final int v){ 144 return py+v> col*BRICK_HEIGHT&& 145 py+v< (col+1)*BRICK_HEIGHT+BALL_DIAMETER; 146} 147 148void checkBrickCollision(){ 149 for(int x=0; x<COLUMNS; x++){ 150 for(int y=0; y<ROWS; y++){ 151 if(bricks[x][y]>0){ 152 if(inXRange(x, vx)&& inYRange(y, vy)){ 153 bricks[x][y]=0; 154 if(inXRange(x, 0)) 155 vy=-vy; 156 if(inYRange(y, 0)) 157 vx=-vx; 158 } 159 } 160 } 161 } 162} 163 164void checkWallCollision(){ 165 if(px+vx < BALL_RADIUS || px+vx > width - BALL_RADIUS) 166 vx=-vx; 167 168 if(py+vy < BALL_RADIUS || py+vy > height - BALL_RADIUS) 169 vy=-vy; 170} 171 172void checkPaddleCollision(){ 173 final int cx=xpos; 174 if(py+vy >=height - (PADDLE_HEIGHT + MARGIN + 6)&& 175 px >= cx - PADDLE_WIDTH/2 && 176 px <= cx + PADDLE_WIDTH/2) 177 { 178 vy=-vy; 179 vx=int( 180 map( 181 px - cx, 182 -(PADDLE_WIDTH/2), PADDLE_WIDTH/2, 183 -MAX_VELOCITY, 184 MAX_VELOCITY 185 ) 186 ); 187 } 188} 189 190void serialEvent(Serial port){ 191 final String arduinoData=port.readStringUntil(LINE_FEED); 192 if(arduinoData!=null){ 193 println(arduinoData); 194 final int[] data=int(split(trim(arduinoData), ' ')); 195 if(data.length==4){ 196 buttonPressed=(data[3]==1); 197 if(buttonPressed){ 198 paused= !paused; 199 if(done){ 200 done=false; 201 initGame(); 202 } 203 } 204 205 if(!paused) 206 xpos=int(map(data[0]-2000, 252, 443, 0, 300)); 207 } 208 } 209}
Processing code
processing
1import processing.serial.*; 2 3Serial arduinoPort; 4 5final int COLUMNS=7; 6final int ROWS=4; 7final int BALL_RADIUS=8; 8final int BALL_DIAMETER=BALL_RADIUS*2; 9final int MAX_VELOCITY=8; 10final int MARGIN=10; 11final int PADDLE_WIDTH=60; 12final int PADDLE_HEIGHT=15; 13final int BRICK_WIDTH=40; 14final int BRICK_HEIGHT=20; 15final int HEIGHT=300; 16final int LINE_FEED=10; 17 18int px, py; 19int vx, vy; 20int xpos=150; 21int[][] bricks= new int[COLUMNS][ROWS]; 22 23boolean buttonPressed=false; 24boolean paused=true; 25boolean done=true; 26 27void setup(){ 28 size(300, 300); 29 noCursor(); 30 textFont(loadFont("Verdana-Bold-36.vlw")); 31 initGame(); 32 println(Arduino.list()); 33 arduinoPort=new Serial(this, "COM3", 19200); 34 arduinoPort.bufferUntil(10); 35} 36 37void initGame(){ 38 initBricks(); 39 initBall(); 40} 41 42void initBricks(){ 43 for(int x=0; x<COLUMNS; x++) 44 for(int y=0; y<ROWS; y++) 45 bricks[x][y]=1; 46} 47 48void initBall(){ 49 px=width/2; 50 py=height/2; 51 vx=int(random(-MAX_VELOCITY, MAX_VELOCITY)); 52 vy=-2; 53} 54 55void draw(){ 56 background(0); 57 stroke(255); 58 strokeWeight(3); 59 60 done=drawBricks(); 61 if(done){ 62 paused=true; 63 printWinMessage(); 64 } 65 if(paused) 66 printPauseMessage(); 67 else 68 updateGame(); 69 70 drawBall(); 71 drawPaddle(); 72} 73 74boolean drawBricks(){ 75 boolean allEmpty=true; 76 for(int x=0; x<COLUMNS; x++){ 77 for(int y=0; y<ROWS; y++){ 78 if(bricks[x][y]>0){ 79 allEmpty=false; 80 fill(0, 0, 100+y*8); 81 rect( 82 MARGIN+x*BRICK_WIDTH, 83 MARGIN+y*BRICK_HEIGHT, 84 BRICK_WIDTH, 85 BRICK_HEIGHT 86 ); 87 } 88 } 89 } 90 return allEmpty; 91} 92 93void drawBall(){ 94 strokeWeight(1); 95 fill(128, 0, 0); 96 ellipse(px, py, BALL_DIAMETER, BALL_DIAMETER); 97} 98 99void drawPaddle(){ 100 int x=xpos - PADDLE_WIDTH/2; 101 int y=height - 25; 102 strokeWeight(1); 103 fill(128); 104 rect(x, y, 60, 15); 105} 106 107void printWinMessage(){ 108 fill(225); 109 textSize(36); 110 textAlign(CENTER); 111 text("YOU WIN", width/2, height*2/3); 112} 113 114void printPauseMessage(){ 115 fill(128); 116 textSize(16); 117 textAlign(CENTER); 118 text("Press button to continue", width/2, height*5/6); 119} 120 121void updateGame(){ 122 if(ballDropped()){ 123 initBall(); 124 paused=true; 125 } else{ 126 checkBrickCollision(); 127 checkWallCollision(); 128 checkPaddleCollision(); 129 px+=vx; 130 py+=vy; 131 } 132} 133 134boolean ballDropped(){ 135 return py+vy> height - BALL_RADIUS; 136} 137 138boolean inXRange(final int row, final int v){ 139 return px + v > row*BRICK_WIDTH&& 140 px+v < (row+1)*BRICK_WIDTH+BALL_DIAMETER; 141} 142 143boolean inYRange(final int col, final int v){ 144 return py+v> col*BRICK_HEIGHT&& 145 py+v< (col+1)*BRICK_HEIGHT+BALL_DIAMETER; 146} 147 148void checkBrickCollision(){ 149 for(int x=0; x<COLUMNS; x++){ 150 for(int y=0; y<ROWS; y++){ 151 if(bricks[x][y]>0){ 152 if(inXRange(x, vx)&& inYRange(y, vy)){ 153 bricks[x][y]=0; 154 if(inXRange(x, 0)) 155 vy=-vy; 156 if(inYRange(y, 0)) 157 vx=-vx; 158 } 159 } 160 } 161 } 162} 163 164void checkWallCollision(){ 165 if(px+vx < BALL_RADIUS || px+vx > width - BALL_RADIUS) 166 vx=-vx; 167 168 if(py+vy < BALL_RADIUS || py+vy > height - BALL_RADIUS) 169 vy=-vy; 170} 171 172void checkPaddleCollision(){ 173 final int cx=xpos; 174 if(py+vy >=height - (PADDLE_HEIGHT + MARGIN + 6)&& 175 px >= cx - PADDLE_WIDTH/2 && 176 px <= cx + PADDLE_WIDTH/2) 177 { 178 vy=-vy; 179 vx=int( 180 map( 181 px - cx, 182 -(PADDLE_WIDTH/2), PADDLE_WIDTH/2, 183 -MAX_VELOCITY, 184 MAX_VELOCITY 185 ) 186 ); 187 } 188} 189 190void serialEvent(Serial port){ 191 final String arduinoData=port.readStringUntil(LINE_FEED); 192 if(arduinoData!=null){ 193 println(arduinoData); 194 final int[] data=int(split(trim(arduinoData), ' ')); 195 if(data.length==4){ 196 buttonPressed=(data[3]==1); 197 if(buttonPressed){ 198 paused= !paused; 199 if(done){ 200 done=false; 201 initGame(); 202 } 203 } 204 205 if(!paused) 206 xpos=int(map(data[0]-2000, 252, 443, 0, 300)); 207 } 208 } 209}
Arduino code
arduino
1#include <Bounce.h> 2#include <Wire.h> 3 4const unsigned int Button_Pin=7; 5const int MPU=0x68; 6int16_t AcX,AcY,AcZ; 7const int NUM_AXES=3; 8 9const int BUFFER_SIZE=16; 10int buffer[NUM_AXES][BUFFER_SIZE]; 11int buffer_pos[NUM_AXES]={0}; 12Bounce button(Button_Pin, 20); 13 14void setup(){ 15 Wire.begin(); 16 Wire.beginTransmission(MPU); 17 Wire.write(0x6B); 18 Wire.write(0); 19 Wire.endTransmission(true); 20 Serial.begin(19200); 21 pinMode(Button_Pin, INPUT); 22} 23 24int get_axis(const int axis){ 25 delay(1); 26 buffer[axis][buffer_pos[axis]]=Wire.read()<<8|Wire.read(); 27 buffer_pos[axis]=(buffer_pos[axis]+1)%BUFFER_SIZE; 28 long sum=0; 29 for(int i=0; i<BUFFER_SIZE; i++) 30 sum+= buffer[axis][i]; 31 return round(sum/BUFFER_SIZE); 32 } 33 34 int get_x(){return get_axis(0);} 35 int get_y(){return get_axis(1);} 36 int get_z(){return get_axis(2);} 37 38void loop(){ 39 Wire.beginTransmission(MPU); 40 Wire.write(0x3B); 41 Wire.endTransmission(false); 42 Wire.requestFrom(MPU,6,true); 43 Serial.print(get_x()); 44 Serial.print(" "); 45 Serial.print(get_y()); 46 Serial.print(" "); 47 Serial.print(get_z()); 48 Serial.print(" "); 49 if(button.update()) 50 Serial.println(button.read()==HIGH ? "1" : "0"); 51 else 52 Serial.println("0"); 53 54 delay(50); 55}
Downloadable files
circuit diagram
circuit diagram
Comments
Only logged in users can leave comments
Nicholas_N
0 Followers
•0 Projects
Table of contents
Intro
4
0