Components and supplies
1
Arduino Nano
2
Resistor 10k
1
I2C LCD
1
Piezo Buzzer
2
Push Button
10
LED (generic)
10
Resistor 470 ohm
Tools and machines
1
Soldering kit
Apps and platforms
1
Arduino IDE
Project description
Code
Code Pong
cpp
...
1//#include <LiquidCrystal.h> 2 3#include <Wire.h> 4 5#include <LiquidCrystal_I2C.h> 6LiquidCrystal_I2C lcd(0x27, 16, 2); 7 8 9bool willTheBallGoTowardsPlayerTwo = true; 10bool isInputAllowed = true; 11 12const int playerOne = 12; 13const int goalPlayerOne = 13; 14const int buttonPlayerOne = 3; 15 16const int playerTwo = 5; 17const int goalPlayerTwo = 4; 18const int buttonPlayerTwo = 2; 19 20int scoreOfPlayerOne = 0; 21int scoreOfPlayerTwo = 0; 22 23const unsigned long initialMillisecondsPerLED = 400; 24const unsigned long initialDeltaMillisecondsPerLED = 50; 25unsigned long millisecondsPerLED = initialMillisecondsPerLED; 26unsigned long deltaMillisecondsPerLED = initialDeltaMillisecondsPerLED; 27unsigned long currentMillis = 0; 28unsigned long previousMillis = 0; 29 30int currentPosition = playerOne; //Player one starts the game. 31int previousPosition = playerOne + 1; 32int deltaPosition = 0; 33 34int buttonStatePlayerOne = 0; 35int lastButtonStatePlayerOne = 0; 36int buttonStatePlayerTwo = 0; 37int lastButtonStatePlayerTwo = 0; 38 39void setup() { 40 41 42 lcd.backlight(); 43 lcd.begin(16,2); 44 lcd.clear(); 45 lcd.setCursor(2, 0); 46 lcd.print("1D-Pong Game"); 47 lcd.setCursor(3, 1); 48 lcd.print("by mircemk"); 49 delay(2000.); 50 51 lcd.clear(); 52 lcd.setCursor(0, 0); 53 lcd.print("Player One: 0"); 54 lcd.setCursor(0, 1); 55 lcd.print("Player Two: 0"); 56 57 58 59 pinMode(4, 1); 60 pinMode(5, 1); 61 pinMode(6, 1); 62 pinMode(7, 1); 63 pinMode(8, 1); 64 pinMode(9, 1); 65 pinMode(10, 1); 66 pinMode(11, 1); 67 pinMode(12, 1); 68 pinMode(13, 1); 69 /*Connect pins 4 to 13 to 220ohm resistors, connect the LEDs' longer legs(+) to the resistors, 70 connect the other legs(-) of the LEDs' to ground. 71 I recommend red LEDs for 4 and 13(Goals), yellow LEDs for 5 and 12(Players), green LEDs for other pins(Field). 72 */ 73 74 pinMode(2, 0); //Pushbuttons for players. Pin 2 is for player two. Pin 3 is for player one. 75 pinMode(3, 0); //Connect pushbuttons following the instructions on this page: https://www.arduino.cc/en/Tutorial/DigitalReadSerial 76} 77 78void loop() 79{ 80 ListenForInput(); 81 currentMillis = millis(); 82 if (currentMillis - previousMillis >= millisecondsPerLED) //If you don't understand this, see: https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay 83 { 84 CheckGoalConditions(); 85 lcd.clear(); 86 lcd.setCursor(0, 0); //(Column,Row) 87 lcd.print("Player One: "); 88 lcd.setCursor(12, 0); 89 lcd.print(scoreOfPlayerOne); 90 91 lcd.setCursor(0, 1); //(Column,Row) 92 lcd.print("Player Two: "); 93 lcd.setCursor(12, 1); 94 lcd.print(scoreOfPlayerTwo); 95 CheckEndGame(); 96 DetermineNextPosition(); 97 MoveBallToNextPosition(); 98 99 previousMillis = currentMillis; 100 101 } 102} 103 104void ListenForInput() //If you don't understand this method. See: https://www.arduino.cc/en/Tutorial/StateChangeDetection 105{ 106 buttonStatePlayerOne = digitalRead(buttonPlayerOne); 107 buttonStatePlayerTwo = digitalRead(buttonPlayerTwo); 108 109 if (buttonStatePlayerOne != lastButtonStatePlayerOne && isInputAllowed) 110 { 111 if (buttonStatePlayerOne == 1) 112 { 113 if (currentPosition == playerOne) 114 { 115 ToggleBallDirection(); 116 IncreaseSpeed(); 117 118 } 119 else 120 { 121 ScoreForPlayer(2); 122 123 } 124 } 125 lastButtonStatePlayerOne = buttonStatePlayerOne; 126 } 127 if (buttonStatePlayerTwo != lastButtonStatePlayerTwo && isInputAllowed) 128 { 129 if (buttonStatePlayerTwo == 1) 130 { 131 if (currentPosition == playerTwo) 132 { 133 ToggleBallDirection(); 134 IncreaseSpeed(); 135 136 } 137 else 138 { 139 ScoreForPlayer(1); 140 141 } 142 } 143 lastButtonStatePlayerTwo = buttonStatePlayerTwo; 144 } 145} 146 147void ToggleBallDirection() 148{ 149 willTheBallGoTowardsPlayerTwo = !willTheBallGoTowardsPlayerTwo; 150 isInputAllowed = false; //Only one direction change per frame is allowed for consistent gameplay. 151} 152 153void IncreaseSpeed() 154{ 155 millisecondsPerLED -= deltaMillisecondsPerLED; 156 if (deltaMillisecondsPerLED > 50) //Because of this, it takes a little more time to reach to an insane speed. Adjust or remove this if rounds become too long. 157 { 158 deltaMillisecondsPerLED -= 50; 159 } 160} 161 162void MoveBallToNextPosition() //Moves the ball one spot. 163{ 164 previousPosition = currentPosition; 165 digitalWrite(previousPosition, 0); 166 currentPosition = currentPosition + deltaPosition; 167 digitalWrite(currentPosition, 1); 168 tone(1, 500, 50); 169 isInputAllowed = true; 170} 171 172void DetermineNextPosition() 173{ 174 if (willTheBallGoTowardsPlayerTwo) 175 { 176 deltaPosition = -1; 177 } 178 else 179 { 180 deltaPosition = 1; 181 } 182} 183 184void CheckGoalConditions() 185{ 186 if (currentPosition == goalPlayerTwo) 187 { 188 ScoreForPlayer(1); 189 } 190 else if (currentPosition == goalPlayerOne) 191 { 192 ScoreForPlayer(2); 193 } 194} 195 196void ScoreForPlayer(int playerWhoScored) 197{ 198 isInputAllowed = false; 199 FlashAllLEDs(1, 0); 200 if (playerWhoScored == 1) 201 { 202 scoreOfPlayerOne++; 203 204 Reset(1); 205 } 206 else if (playerWhoScored == 2) 207 { 208 scoreOfPlayerTwo++; 209 210 Reset(2); 211 } 212 213} 214 215void CheckEndGame() 216{ 217 if (scoreOfPlayerOne == 10) 218 { 219 lcd.clear(); 220 lcd.setCursor(0, 0); 221 lcd.print("Player One Win"); 222 EndGameCeremonyFor(1); 223 224 } 225 if (scoreOfPlayerTwo == 10) 226 { 227 lcd.clear(); 228 lcd.setCursor(0, 0); 229 lcd.print("Player Two Win"); 230 EndGameCeremonyFor(2); 231 232 } 233} 234 235void Reset(int playerCurrentlyScored) 236{ 237 if (playerCurrentlyScored == 1) 238 { 239 digitalWrite(playerOne, 1); 240 currentPosition = playerOne; 241 willTheBallGoTowardsPlayerTwo = true; 242 tone(1, 200 ,150 ); 243 } 244 else if (playerCurrentlyScored == 2) 245 { 246 digitalWrite(playerTwo, 1); 247 currentPosition = playerTwo; 248 willTheBallGoTowardsPlayerTwo = false; 249 tone(1, 300 ,150 ); 250 251 } 252 253 delay(1000); //Are three seconds enough for players to process the score? 254 millisecondsPerLED = initialMillisecondsPerLED; //Sets speed to initial value at the beginning of each round. 255 deltaMillisecondsPerLED = initialDeltaMillisecondsPerLED; 256} 257 258 259void EndGameCeremonyFor(int winner) 260{ 261 FlashAllLEDs(50, winner); 262 TurnOffAllLEDsForPlayer(0); 263 scoreOfPlayerOne = 0; 264 scoreOfPlayerTwo = 0; 265 delay(1000); 266 lcd.clear(); 267 lcd.setCursor(4, 0); 268 lcd.print("Starting"); 269 lcd.setCursor(4,1); 270 lcd.print("New Game"); 271 delay(2000); 272} 273 274void TurnOnAllLEDsForPlayer(int player) 275{ 276 if (player != 1) //When this method is called with (2), only these pins(player two's) will turn on 277 { 278 digitalWrite(4, 1); 279 digitalWrite(5, 1); 280 digitalWrite(6, 1); 281 digitalWrite(7, 1); 282 digitalWrite(8, 1); 283 tone(1, 1000, 35); 284 } 285 if (player != 2) //When this method is called with (1), only these pins(player one's) will turn on 286 { 287 digitalWrite(9, 1); 288 digitalWrite(10, 1); 289 digitalWrite(11, 1); 290 digitalWrite(12, 1); 291 digitalWrite(13, 1); 292 tone(1, 1000, 35); 293 } 294} 295 296void TurnOffAllLEDsForPlayer(int player) 297{ 298 if (player != 1) //When this method is called with (2), only these pins(player two's) will turn off 299 { 300 digitalWrite(4, 0); 301 digitalWrite(5, 0); 302 digitalWrite(6, 0); 303 digitalWrite(7, 0); 304 digitalWrite(8, 0); 305 } 306 if (player != 2) //When this method is called with (1), only these pins(player one's) will turn off 307 { 308 digitalWrite(9, 0); 309 digitalWrite(10, 0); 310 digitalWrite(11, 0); 311 digitalWrite(12, 0); 312 digitalWrite(13, 0); 313 } 314} 315 316void FlashAllLEDs(int blinkCount, int player) //Second parameter(int player) is for when you want to flash only one player's LEDs. Use 1 for player one, use 2 for player two. 317{ 318 for (int i = 0; i < blinkCount; i++) 319 { 320 TurnOnAllLEDsForPlayer(player); 321 delay(35); 322 TurnOffAllLEDsForPlayer(player); 323 delay(35); 324 } 325}
Downloadable files
Schematic
...
Schematic.jpg

Comments
Only logged in users can leave comments