Created
March 30, 2014 20:11
-
-
Save aral/9878984 to your computer and use it in GitHub Desktop.
1D LED Pong inspired by Jason Hotchkiss’s 1D Pong
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 1D LED PONG | |
// Copyright (c) 2014 Aral Balkan. Released under the MIT License. | |
// | |
// Inspired by Jason Hotchkiss’s 1D Pong (hat-tip Seb Lee-Delisle) | |
// | |
// Video of working result: https://twitter.com/aral/status/450334281953722369 | |
// | |
// With knowledge gleamed from the Build Brighton Introduction to Arduino Workshop on 29th March, 2014. | |
// http://buildbrighton.com | |
// | |
// PLAYER 2 Switch (Input — pin 6) | |
// | |
// * Light (Pin 13) | |
// * Light (Pin 12) | |
// * Light (Pin 11) | |
// ---------------- Speaker (Pin 7) | |
// * LED (Pin 10) | |
// * LED (Pin 9) | |
// * LED (Pin 8) | |
// | |
// Player 1 Switch (Input — pin 5) | |
// | |
int PLAYER_ONE = 0; | |
int PLAYER_TWO = 1; | |
int SPEAKER = 7; | |
int PLAYER_ONE_SWITCH = 5; | |
int PLAYER_TWO_SWITCH = 6; | |
int PLAYER_SWITCH_PIN_OFFSET = 5; | |
bool inGame = false; | |
bool waitingToStartGame = false; | |
int playerToStart = 0; | |
int currentMovementOffset = 0; | |
int currentLed = 8; | |
// Difficulty (how fast it is) | |
int gameLoopDelay = 300; | |
// Adding Switch with Pull Down resistor | |
void setup() | |
{ | |
// Inputs | |
pinMode(PLAYER_ONE_SWITCH, INPUT_PULLUP); | |
pinMode(PLAYER_TWO_SWITCH, INPUT_PULLUP); | |
// Speaker | |
pinMode(3, OUTPUT); | |
// The ‘screen’ — 6 LEDs | |
pinMode(8, OUTPUT); | |
pinMode(9, OUTPUT); | |
pinMode(10, OUTPUT); | |
pinMode(11, OUTPUT); | |
pinMode(12, OUTPUT); | |
pinMode(13, OUTPUT); | |
} | |
void loop() | |
{ | |
if (inGame) | |
{ | |
gameLoop(); | |
} | |
else | |
{ | |
if (!waitingToStartGame) | |
{ | |
// Coin toss to determine who starts | |
playerToStart = whoStarts(); | |
waitingToStartGame = true; | |
} | |
else | |
{ | |
// Blink the player’s light | |
int playerReadyLight = (playerToStart == PLAYER_ONE) ? 8 : 13; | |
digitalWrite(playerReadyLight, HIGH); | |
delay(100); | |
digitalWrite(playerReadyLight, LOW); | |
delay(100); | |
// Check if the player is about to start | |
if (digitalRead(playerToStart + PLAYER_SWITCH_PIN_OFFSET) == LOW) | |
{ | |
startGame(); | |
} | |
} | |
} | |
} | |
void playerOneInstantReplay() | |
{ | |
digitalWrite(10, HIGH); | |
tone(SPEAKER, 440, 400); | |
delay(500); | |
digitalWrite(10, LOW); | |
digitalWrite(9, HIGH); | |
tone(SPEAKER, 340, 400); | |
delay(500); | |
digitalWrite(9, LOW); | |
digitalWrite(8, HIGH); | |
tone(SPEAKER, 240, 400); | |
delay(500); | |
digitalWrite(8, LOW); | |
} | |
void playerTwoInstantReplay() | |
{ | |
digitalWrite(11, HIGH); | |
tone(SPEAKER, 440, 400); | |
delay(500); | |
digitalWrite(11, LOW); | |
digitalWrite(12, HIGH); | |
tone(SPEAKER, 340, 400); | |
delay(500); | |
digitalWrite(12, LOW); | |
digitalWrite(13, HIGH); | |
tone(SPEAKER, 240, 400); | |
delay(500); | |
digitalWrite(13, LOW); | |
} | |
void gameLoop() | |
{ | |
digitalWrite(currentLed, LOW); | |
currentLed += currentMovementOffset; | |
if (currentLed < 7) | |
{ | |
// Player One Lost | |
playerOneInstantReplay(); | |
waitingToStartGame = true; | |
inGame = false; | |
playerToStart = PLAYER_TWO; | |
} | |
else if (currentLed > 13) | |
{ | |
// Player Two Lost | |
playerTwoInstantReplay(); | |
waitingToStartGame = true; | |
inGame = false; | |
playerToStart = PLAYER_ONE; | |
} | |
else | |
{ | |
// Ball is still in play | |
digitalWrite(currentLed, HIGH); | |
} | |
if(digitalRead(PLAYER_ONE_SWITCH) == LOW) | |
{ | |
playerOnePaddleHit(); | |
if (gameLoopDelay > 100) gameLoopDelay -= 20; | |
} | |
if(digitalRead(PLAYER_TWO_SWITCH) == LOW) | |
{ | |
playerTwoPaddleHit(); | |
if (gameLoopDelay > 100) gameLoopDelay -= 20; | |
} | |
delay(gameLoopDelay); | |
} | |
int startGame() | |
{ | |
inGame = true; | |
currentLed = (playerToStart == PLAYER_ONE) ? 8 : 13; | |
currentMovementOffset = (playerToStart == PLAYER_ONE) ? 1 : -1; | |
gameLoopDelay = 300; | |
} | |
// | |
// Who starts | |
// | |
int whoStarts() | |
{ | |
randomSeed(analogRead(0)); | |
return random(0, 2); | |
} | |
// | |
// Hit testing | |
// | |
void playerOnePaddleHit() | |
{ | |
if (currentLed == 8) | |
{ | |
tone(SPEAKER, 440, 50); | |
currentMovementOffset = 1; | |
} | |
} | |
void playerTwoPaddleHit() | |
{ | |
if (currentLed = 13) | |
{ | |
tone(SPEAKER, 680, 50); | |
currentMovementOffset = -1; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment