Components and supplies
10
LED (generic)
10
Resistor 221 ohm
1
Jumper wires (generic)
2
Resistor 10k ohm
1
Buzzer
1
Arduino UNO
1
Solderless Breadboard Full Size
1
I2C 16x2 Arduino LCD Display Module
2
Pushbutton switch 12mm
Apps and platforms
1
Arduino Web Editor
Project description
Code
Untitled file
arduino
1#include <Wire.h> 2#include <LiquidCrystal_I2C.h> 3 4LiquidCrystal_I2C lcd(0x27,16,2); 5 6bool willTheBallGoTowardsPlayerTwo = true; 7bool isInputAllowed = true; 8 9const int btn = 0; 10const int playerOne = 12; 11const int goalPlayerOne = 13; 12const int buttonPlayerOne = 3; 13 14const int playerTwo = 5; 15const int goalPlayerTwo = 4; 16const int buttonPlayerTwo = 2; 17const int PIEZO = 1; 18 19int scoreOfPlayerOne = 0; 20int scoreOfPlayerTwo = 0; 21 22const unsigned long initialMillisecondsPerLED = 500; 23const unsigned long initialDeltaMillisecondsPerLED = 50; 24unsigned long millisecondsPerLED = initialMillisecondsPerLED; 25unsigned long deltaMillisecondsPerLED = initialDeltaMillisecondsPerLED; 26unsigned long currentMillis = 0; 27unsigned long previousMillis = 0; 28 29int currentPosition = playerOne; 30int previousPosition = playerOne + 1; 31int deltaPosition = 0; 32 33int buttonStatePlayerOne = 0; 34int lastButtonStatePlayerOne = 0; 35int buttonStatePlayerTwo = 0; 36int lastButtonStatePlayerTwo = 0; 37 38void setup() { 39 lcd.init(); 40 lcd.backlight(); 41 pinMode(1, 1); 42 pinMode(4, 1); 43 pinMode(5, 1); 44 pinMode(6, 1); 45 pinMode(7, 1); 46 pinMode(8, 1); 47 pinMode(9, 1); 48 pinMode(10, 1); 49 pinMode(11, 1); 50 pinMode(12, 1); 51 pinMode(13, 1); 52 53 pinMode(2, 0); 54 pinMode(3, 0); 55} 56 57void loop() 58{ 59 ListenForInput(); 60 currentMillis = millis(); 61 if (currentMillis - previousMillis >= millisecondsPerLED) 62 { 63 CheckGoalConditions(); 64 DetermineNextPosition(); 65 MoveBallToNextPosition(); 66 67 previousMillis = currentMillis; 68 } 69 } 70 71void ListenForInput() 72{ 73 buttonStatePlayerOne = digitalRead(buttonPlayerOne); 74 buttonStatePlayerTwo = digitalRead(buttonPlayerTwo); 75 76 if (buttonStatePlayerOne != lastButtonStatePlayerOne && isInputAllowed) 77 { 78 if (buttonStatePlayerOne == 1) 79 { 80 if (currentPosition == playerOne) 81 { 82 ToggleBallDirection(); 83 IncreaseSpeed(); 84 } 85 else 86 { 87 ScoreForPlayer(2); 88 } 89 } 90 lastButtonStatePlayerOne = buttonStatePlayerOne; 91 } 92 if (buttonStatePlayerTwo != lastButtonStatePlayerTwo && isInputAllowed) 93 { 94 if (buttonStatePlayerTwo == 1) 95 { 96 if (currentPosition == playerTwo) 97 { 98 ToggleBallDirection(); 99 IncreaseSpeed(); 100 } 101 else 102 { 103 ScoreForPlayer(1); 104 } 105 } 106 lastButtonStatePlayerTwo = buttonStatePlayerTwo; 107 } 108} 109 110void ToggleBallDirection() 111{ 112 willTheBallGoTowardsPlayerTwo = !willTheBallGoTowardsPlayerTwo; 113 isInputAllowed = false; 114} 115 116void IncreaseSpeed() 117{ 118 millisecondsPerLED -= deltaMillisecondsPerLED; 119 if (deltaMillisecondsPerLED > 5) 120 { 121 deltaMillisecondsPerLED -= 5; 122 } 123} 124 125void MoveBallToNextPosition() 126{ 127 previousPosition = currentPosition; 128 digitalWrite(previousPosition, 0); 129 currentPosition = currentPosition + deltaPosition; 130 digitalWrite(currentPosition, 1); 131 isInputAllowed = true; 132} 133 134void DetermineNextPosition() 135{ 136 if (willTheBallGoTowardsPlayerTwo) 137 { 138 deltaPosition = -1; 139 } 140 else 141 { 142 deltaPosition = 1; 143 } 144} 145 146void CheckGoalConditions() 147{ 148 if (currentPosition == goalPlayerTwo) 149 { 150 ScoreForPlayer(1); 151 152 } 153 else if (currentPosition == goalPlayerOne) 154 { 155 ScoreForPlayer(2); 156 } 157} 158 159void ScoreForPlayer(int playerWhoScored) 160{ 161 isInputAllowed = false; 162 FlashAllLEDs(1, 0); 163 if (playerWhoScored == 1) 164 { 165 scoreOfPlayerOne++; 166 ShowScores(1); 167 } 168 else if (playerWhoScored == 2) 169 { 170 scoreOfPlayerTwo++; 171 ShowScores(2); 172 } 173 CheckEndGame(); 174} 175 176void CheckEndGame() 177{ 178 if (scoreOfPlayerOne == 3) 179 { 180 EndGameCeremonyFor(1); 181 lcd.setCursor(0, 0); 182 lcd.print("player one wins "); 183 lcd.setCursor(0, 1); 184 lcd.print("congratulations "); 185 delay(2500); 186 soundCorrectGuess(); 187 lcd.setCursor(0, 0); 188 lcd.print("player 2, "); 189 lcd.setCursor(0, 1); 190 lcd.print("play again "); 191 delay(2500); 192 soundBuzzer (); 193 lcd.setCursor(0, 0); 194 lcd.print(" "); 195 lcd.setCursor(0, 1); 196 lcd.print(" "); 197 delay(2500); 198 } 199 if (scoreOfPlayerTwo == 3) 200 { 201 EndGameCeremonyFor(2); 202 lcd.setCursor(0, 0); 203 lcd.print("player two wins "); 204 lcd.setCursor(0, 1); 205 lcd.print("congratulations "); 206 delay(2500); 207 soundCorrectGuess(); 208 lcd.setCursor(0, 0); 209 lcd.print("player 1, "); 210 lcd.setCursor(0, 1); 211 lcd.print("play again "); 212 delay(2500); 213 soundBuzzer (); 214 lcd.setCursor(0, 0); 215 lcd.print(" "); 216 lcd.setCursor(0, 1); 217 lcd.print(" "); 218 delay(2500); 219 } 220} 221 222void ShowScores(int playerCurrentlyScored) 223{ 224 if (playerCurrentlyScored == 1) 225 { 226 digitalWrite(playerOne, 1); 227 currentPosition = playerOne; 228 willTheBallGoTowardsPlayerTwo = true; 229 } 230 else if (playerCurrentlyScored == 2) 231 { 232 digitalWrite(playerTwo, 1); 233 currentPosition = playerTwo; 234 willTheBallGoTowardsPlayerTwo = false; 235 } 236 237 for (int i = 0; i < scoreOfPlayerOne; i++) 238 { 239 digitalWrite((6 + i), 1); 240 } 241 for (int i = 0; i < scoreOfPlayerTwo; i++) 242 { 243 digitalWrite((11 - i), 1); 244 } 245 246 delay(3000); 247 ResetValuesForNextRound(); 248} 249 250void ResetValuesForNextRound() 251{ 252 FlashAllLEDs(1, 0); 253 millisecondsPerLED = initialMillisecondsPerLED; 254 deltaMillisecondsPerLED = initialDeltaMillisecondsPerLED; 255} 256void EndGameCeremonyFor(int winner) 257{ 258 FlashAllLEDs(50, winner); 259 TurnOffAllLEDsForPlayer(0); 260 scoreOfPlayerOne = 0; 261 scoreOfPlayerTwo = 0; 262} 263 264void TurnOnAllLEDsForPlayer(int player) 265{ 266 if (player != 1) 267 { 268 digitalWrite(9, 1); 269 digitalWrite(10, 1); 270 digitalWrite(11, 1); 271 digitalWrite(12, 1); 272 digitalWrite(13, 1); 273 } 274 if (player != 2) 275 { 276 digitalWrite(4, 1); 277 digitalWrite(5, 1); 278 digitalWrite(6, 1); 279 digitalWrite(7, 1); 280 digitalWrite(8, 1); 281 } 282} 283 284void TurnOffAllLEDsForPlayer(int player) 285{ 286 if (player != 1) 287 { 288 digitalWrite(4, 0); 289 digitalWrite(5, 0); 290 digitalWrite(6, 0); 291 digitalWrite(7, 0); 292 digitalWrite(8, 0); 293 } 294 if (player != 2) 295 { 296 digitalWrite(9, 0); 297 digitalWrite(10, 0); 298 digitalWrite(11, 0); 299 digitalWrite(12, 0); 300 digitalWrite(13, 0); 301 } 302} 303 304void FlashAllLEDs(int blinkCount, int player) 305{ 306 for (int i = 0; i < blinkCount; i++) 307 { 308 TurnOnAllLEDsForPlayer(player); 309 delay(35); 310 TurnOffAllLEDsForPlayer(player); 311 delay(35); 312 } 313} 314void soundBuzzer() { 315 tone(PIEZO, 100, 2000); 316 delay(2300); 317} 318 319void soundCorrectGuess() { 320 tone(PIEZO, 700, 100); 321 delay(100); 322 tone(PIEZO, 800, 100); 323 delay(100); 324 tone(PIEZO, 900, 100); 325 delay(100); 326 tone(PIEZO, 1000, 100); 327 delay(100); 328 tone(PIEZO, 1100, 100); 329 delay(100); 330 tone(PIEZO, 1200, 100); 331 delay(100); 332}
Downloadable files
powerful_blorr_31i5OpHiaC.png
powerful_blorr_31i5OpHiaC.png

powerful_blorr_31i5OpHiaC.png
powerful_blorr_31i5OpHiaC.png

Comments
Only logged in users can leave comments