Components and supplies
1
sparkfun micro oled breakout
1
Jumper wires (generic)
1
Resistor 220 ohm
1
Pushbutton switch 12mm
1
Arduino UNO
Tools and machines
1
Soldering iron (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
untitled
c_cpp
1#include <Wire.h> // Include Wire if you're using I2C 2#include <SPI.h> // Include SPI if you're using SPI 3#include <SFE_MicroOLED.h> // Include the SFE_MicroOLED library 4 5////////////////////////// 6// MicroOLED Definition // 7////////////////////////// 8#define PIN_RESET 9 // Connect RST to pin 9 9#define PIN_DC 8 // Connect DC to pin 8 10#define PIN_CS 10 // Connect CS to pin 10 11#define DC_JUMPER 0 12 13////////////////////////////////// 14// MicroOLED Object Declaration // 15////////////////////////////////// 16MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS); // SPI declaration 17//MicroOLED oled(PIN_RESET, DC_JUMPER); // I2C declaration 18int a= 0; 19int b=0; 20void setup(){ 21 digitalWrite(7, HIGH); 22 delay(200); 23 digitalWrite(7, LOW); 24 delay(100); 25 digitalWrite(7, HIGH); 26 delay(200); 27 digitalWrite(7, LOW); 28 oled.clear(PAGE); 29 oled.begin(); // Initialize the OLED 30 oled.clear(ALL); // Clear the display's internal memory 31 oled.setFontType(1); 32 oled.setCursor(0, 0); 33 oled.print("PONG :)"); 34 oled.display(); // Display what's in the buffer (splashscreen) 35 delay(1000); // Delay 1000 ms 36 oled.clear(PAGE); // Clear the buffer. 37 38 randomSeed(analogRead(A0) + analogRead(A1)); 39 pinMode(7,OUTPUT); 40 pinMode(2,INPUT); 41 pinMode(3,INPUT); 42 pinMode(4,INPUT); 43 pinMode(5,INPUT); 44} 45 46 47void loop() { 48 // put your main code here, to run repeatedly: 49 int paddleW = 3; // Paddle width 50 int paddleH = 15; // Paddle height 51 // Paddle 0 (left) position coordinates 52 int paddle0_Y = (oled.getLCDHeight() / 2) - (paddleH / 2); 53 int paddle0_X = 2; 54 // Paddle 1 (right) position coordinates 55 int paddle1_Y = (oled.getLCDHeight() / 2) - (paddleH / 2); 56 int paddle1_X = oled.getLCDWidth() - 3 - paddleW; 57 int ball_rad = 2; // Ball radius 58 // Ball position coordinates 59 int ball_X = paddle0_X + paddleW + ball_rad; 60 int ball_Y = random(1 + ball_rad, oled.getLCDHeight() - ball_rad);//paddle0_Y + ball_rad; 61 int ballVelocityX = 1; // Ball left/right velocity 62 int ballVelocityY = 1; // Ball up/down velocity 63 int paddle0Velocity = -1; // Paddle 0 velocity 64 int paddle1Velocity = 1; // Paddle 1 velocity 65 66 //while(ball_X >= paddle0_X + paddleW - 1) 67 while ((ball_X - ball_rad > 1) && 68 (ball_X + ball_rad < oled.getLCDWidth() - 2)) 69 { 70 int aa= digitalRead(2); 71 int ab= digitalRead(3); 72 int ba= digitalRead(4); 73 int bb= digitalRead(5); 74 // Increment ball's position 75 ball_X+=ballVelocityX; 76 ball_Y+=ballVelocityY; 77 // Check if the ball is colliding with the left paddle 78 if(aa == HIGH){ 79 paddle1_Y++; 80 paddle1_Y++; 81 } 82 if(ab == HIGH){ 83 paddle1_Y--; 84 paddle1_Y--; 85 } 86 if(ba == HIGH){ 87 paddle0_Y++; 88 paddle0_Y++; 89 } 90 if(bb == HIGH){ 91 paddle0_Y--; 92 paddle0_Y--; 93 } 94 if (ball_X - ball_rad < paddle0_X + paddleW) 95 { 96 // Check if ball is within paddle's height 97 if ((ball_Y > paddle0_Y) && (ball_Y < paddle0_Y + paddleH)) 98 { 99 ball_X++; // Move ball over one to the right 100 ballVelocityX = -ballVelocityX; // Change velocity 101 } 102 } 103 // Check if the ball hit the right paddle 104 if (ball_X + ball_rad > paddle1_X) 105 { 106 // Check if ball is within paddle's height 107 if ((ball_Y > paddle1_Y) && (ball_Y < paddle1_Y + paddleH)) 108 { 109 ball_X--; // Move ball over one to the left 110 ballVelocityX = -ballVelocityX; // change velocity 111 } 112 } 113 // Check if the ball hit the top or bottom 114 if ((ball_Y <= ball_rad) || (ball_Y >= (oled.getLCDHeight() - ball_rad - 1))) 115 { 116 // Change up/down velocity direction 117 ballVelocityY = -ballVelocityY; 118 } 119 // Move the paddles up and down 120 // Change paddle 0's direction if it hit top/bottom 121 if ((paddle0_Y <= 1) || (paddle0_Y > oled.getLCDHeight() - 2 - paddleH)) 122 { 123 paddle0Velocity = -paddle0Velocity; 124 } 125 // Change paddle 1's direction if it hit top/bottom 126 if ((paddle1_Y <= 1) || (paddle1_Y > oled.getLCDHeight() - 2 - paddleH)) 127 { 128 paddle1Velocity = -paddle1Velocity; 129 } 130 131 // Draw the Pong Field 132 oled.clear(PAGE); // Clear the page 133 // Draw an outline of the screen: 134 oled.rect(0, 0, oled.getLCDWidth() - 1, oled.getLCDHeight()); 135 // Draw the center line 136 oled.rectFill(oled.getLCDWidth()/2 - 1, 0, 2, oled.getLCDHeight()); 137 // Draw the Paddles: 138 oled.rectFill(paddle0_X, paddle0_Y, paddleW, paddleH); 139 oled.rectFill(paddle1_X, paddle1_Y, paddleW, paddleH); 140 // Draw the ball: 141 oled.circle(ball_X, ball_Y, ball_rad); 142 143 oled.setFontType(1); 144 oled.setCursor(20, 0); 145 oled.print(b); 146 oled.print("-"); 147 oled.print(a); 148 // Actually draw everything on the screen: 149 oled.display(); 150 delay(25); // Delay for visibility 151 } 152 digitalWrite(7, HIGH); 153 delay(200); 154 digitalWrite(7, LOW); 155 delay(100); 156 digitalWrite(7, HIGH); 157 delay(200); 158 digitalWrite(7, LOW); 159 delay(1000); 160 if(ball_X + ball_rad < oled.getLCDWidth() - 2){ 161 a++; 162 }else{ 163 b++; 164 } 165}
untitled
c_cpp
1#include <Wire.h> // Include Wire if you're using I2C 2#include <SPI.h> 3 // Include SPI if you're using SPI 4#include <SFE_MicroOLED.h> // Include the 5 SFE_MicroOLED library 6 7////////////////////////// 8// MicroOLED Definition 9 // 10////////////////////////// 11#define PIN_RESET 9 // Connect RST to pin 9 12#define 13 PIN_DC 8 // Connect DC to pin 8 14#define PIN_CS 10 // Connect CS to pin 15 10 16#define DC_JUMPER 0 17 18////////////////////////////////// 19// MicroOLED 20 Object Declaration // 21////////////////////////////////// 22MicroOLED oled(PIN_RESET, 23 PIN_DC, PIN_CS); // SPI declaration 24//MicroOLED oled(PIN_RESET, DC_JUMPER); // 25 I2C declaration 26int a= 0; 27int b=0; 28void setup(){ 29 digitalWrite(7, 30 HIGH); 31 delay(200); 32 digitalWrite(7, LOW); 33 delay(100); 34 digitalWrite(7, 35 HIGH); 36 delay(200); 37 digitalWrite(7, LOW); 38 oled.clear(PAGE); 39 oled.begin(); 40 // Initialize the OLED 41 oled.clear(ALL); // Clear the display's internal 42 memory 43 oled.setFontType(1); 44 oled.setCursor(0, 0); 45 oled.print("PONG 46 :)"); 47 oled.display(); // Display what's in the buffer (splashscreen) 48 49 delay(1000); // Delay 1000 ms 50 oled.clear(PAGE); // Clear the buffer. 51 52 53 randomSeed(analogRead(A0) + analogRead(A1)); 54 pinMode(7,OUTPUT); 55 56 pinMode(2,INPUT); 57 pinMode(3,INPUT); 58 pinMode(4,INPUT); 59 pinMode(5,INPUT); 60} 61 62 63void 64 loop() { 65 // put your main code here, to run repeatedly: 66 int paddleW = 67 3; // Paddle width 68 int paddleH = 15; // Paddle height 69 // Paddle 0 (left) 70 position coordinates 71 int paddle0_Y = (oled.getLCDHeight() / 2) - (paddleH / 72 2); 73 int paddle0_X = 2; 74 // Paddle 1 (right) position coordinates 75 int 76 paddle1_Y = (oled.getLCDHeight() / 2) - (paddleH / 2); 77 int paddle1_X = oled.getLCDWidth() 78 - 3 - paddleW; 79 int ball_rad = 2; // Ball radius 80 // Ball position coordinates 81 82 int ball_X = paddle0_X + paddleW + ball_rad; 83 int ball_Y = random(1 + ball_rad, 84 oled.getLCDHeight() - ball_rad);//paddle0_Y + ball_rad; 85 int ballVelocityX = 86 1; // Ball left/right velocity 87 int ballVelocityY = 1; // Ball up/down velocity 88 89 int paddle0Velocity = -1; // Paddle 0 velocity 90 int paddle1Velocity = 1; 91 // Paddle 1 velocity 92 93 //while(ball_X >= paddle0_X + paddleW - 1) 94 95 while ((ball_X - ball_rad > 1) && 96 (ball_X + ball_rad < oled.getLCDWidth() 97 - 2)) 98 { 99 int aa= digitalRead(2); 100 int ab= digitalRead(3); 101 int 102 ba= digitalRead(4); 103 int bb= digitalRead(5); 104 // Increment ball's position 105 106 ball_X+=ballVelocityX; 107 ball_Y+=ballVelocityY; 108 // Check if the 109 ball is colliding with the left paddle 110 if(aa == HIGH){ 111 paddle1_Y++; 112 113 paddle1_Y++; 114 } 115 if(ab == HIGH){ 116 paddle1_Y--; 117 paddle1_Y--; 118 119 } 120 if(ba == HIGH){ 121 paddle0_Y++; 122 paddle0_Y++; 123 } 124 125 if(bb == HIGH){ 126 paddle0_Y--; 127 paddle0_Y--; 128 } 129 if 130 (ball_X - ball_rad < paddle0_X + paddleW) 131 { 132 // Check if ball is 133 within paddle's height 134 if ((ball_Y > paddle0_Y) && (ball_Y < paddle0_Y 135 + paddleH)) 136 { 137 ball_X++; // Move ball over one to the right 138 139 ballVelocityX = -ballVelocityX; // Change velocity 140 } 141 } 142 143 // Check if the ball hit the right paddle 144 if (ball_X + ball_rad > paddle1_X) 145 146 { 147 // Check if ball is within paddle's height 148 if ((ball_Y > 149 paddle1_Y) && (ball_Y < paddle1_Y + paddleH)) 150 { 151 ball_X--; // 152 Move ball over one to the left 153 ballVelocityX = -ballVelocityX; // change 154 velocity 155 } 156 } 157 // Check if the ball hit the top or bottom 158 159 if ((ball_Y <= ball_rad) || (ball_Y >= (oled.getLCDHeight() - ball_rad - 1))) 160 161 { 162 // Change up/down velocity direction 163 ballVelocityY = -ballVelocityY; 164 165 } 166 // Move the paddles up and down 167 // Change paddle 0's direction 168 if it hit top/bottom 169 if ((paddle0_Y <= 1) || (paddle0_Y > oled.getLCDHeight() 170 - 2 - paddleH)) 171 { 172 paddle0Velocity = -paddle0Velocity; 173 } 174 175 // Change paddle 1's direction if it hit top/bottom 176 if ((paddle1_Y <= 177 1) || (paddle1_Y > oled.getLCDHeight() - 2 - paddleH)) 178 { 179 paddle1Velocity 180 = -paddle1Velocity; 181 } 182 183 // Draw the Pong Field 184 oled.clear(PAGE); 185 // Clear the page 186 // Draw an outline of the screen: 187 oled.rect(0, 188 0, oled.getLCDWidth() - 1, oled.getLCDHeight()); 189 // Draw the center line 190 191 oled.rectFill(oled.getLCDWidth()/2 - 1, 0, 2, oled.getLCDHeight()); 192 // 193 Draw the Paddles: 194 oled.rectFill(paddle0_X, paddle0_Y, paddleW, paddleH); 195 196 oled.rectFill(paddle1_X, paddle1_Y, paddleW, paddleH); 197 // Draw the ball: 198 199 oled.circle(ball_X, ball_Y, ball_rad); 200 201 oled.setFontType(1); 202 oled.setCursor(20, 203 0); 204 oled.print(b); 205 oled.print("-"); 206 oled.print(a); 207 // Actually 208 draw everything on the screen: 209 oled.display(); 210 delay(25); // Delay 211 for visibility 212 } 213 digitalWrite(7, HIGH); 214 delay(200); 215 digitalWrite(7, 216 LOW); 217 delay(100); 218 digitalWrite(7, HIGH); 219 delay(200); 220 digitalWrite(7, 221 LOW); 222 delay(1000); 223 if(ball_X + ball_rad < oled.getLCDWidth() - 2){ 224 225 a++; 226 }else{ 227 b++; 228 } 229}
Downloadable files
untitled_sketch_bb_tEVX0vEdyE.png
untitled_sketch_bb_tEVX0vEdyE.png

untitled_sketch_bb_tEVX0vEdyE.png
untitled_sketch_bb_tEVX0vEdyE.png

Comments
Only logged in users can leave comments