/** * 2 player reaction time game * * need 1 green LED wired to pin 13 this tells players when to hit the button * 3 red LEDs connected to pins 12,11,10 - these are the player win indications * (11,12) and the confirmation led (10) * 2 buttons are connected to pins 2 and 3 for the player inputs * * The red confirmation LED comes on, both players must then press the button to * indicate that they are ready. * All lights then go off and a random time later, the green light comes on, * the players must press and release their button as soon as they see this, * the player which presses the button first wins and their led comes on. * the game then freezes for 5 second before reverting to the confirmation stage */ // Constants /* INPUT PINS */ const int BUTTON1_PIN = 2; const int BUTTON2_PIN = 3; /* OUTPUT PINS */ const int READY_PIN = 10; const int PLAYER1_PIN = 11; const int PLAYER2_PIN = 12; const int SIGNAL_PIN = 13; /* GAME_STATES */ const int WAITING_CONFIRMATION = 0; const int IN_PROGRESS = 1; // Global variables int oldButtonState1 = 0; int oldButtonState2 = 0; boolean player1confirmed = false; boolean player2confirmed = false; int gameState = WAITING_CONFIRMATION; void setup() { pinMode(BUTTON1_PIN, INPUT); pinMode(BUTTON2_PIN, INPUT); pinMode(READY_PIN, OUTPUT); pinMode(PLAYER1_PIN, OUTPUT); pinMode(PLAYER2_PIN, OUTPUT); pinMode(SIGNAL_PIN, OUTPUT); Serial.begin(9600); // Get a random number from unconnected pin randomSeed(analogRead(1)); } void loop () { int buttonState1 = digitalRead(BUTTON1_PIN); int buttonState2 = digitalRead(BUTTON2_PIN); if (gameState == WAITING_CONFIRMATION) { if (buttonState1 == LOW && buttonState1 != oldButtonState1) { // Player 1 has just released the button and so has confirmed player1confirmed = true; } if (buttonState2 == LOW && buttonState2 != oldButtonState2) { // Player 2 has just released the button and so has confirmed player2confirmed = true; } if (player1confirmed && player2confirmed) { // we are ready to play, turn off confirmation LED // start timer, reset confirmation states and change the game state digitalWrite(READY_PIN, LOW); digitalWrite(PLAYER1_PIN, LOW); digitalWrite(PLAYER2_PIN, LOW); player1confirmed = player2confirmed = false; gameState = IN_PROGRESS; digitalWrite(SIGNAL_PIN, LOW); long randDelay = random (30); randDelay *= 1000; delay(randDelay); } else { digitalWrite(READY_PIN, HIGH); } } else if (gameState == IN_PROGRESS) { digitalWrite(SIGNAL_PIN, HIGH); if (buttonState1 == LOW && buttonState1 != oldButtonState1) { // Player 1 has just released the button and so has won digitalWrite(PLAYER1_PIN, HIGH); gameState = WAITING_CONFIRMATION; } if (buttonState2 == LOW && buttonState2 != oldButtonState2) { // Player 2 has just released the button and so has won digitalWrite(PLAYER2_PIN, HIGH); gameState = WAITING_CONFIRMATION; } if (gameState == WAITING_CONFIRMATION) { delay(5000); // 5 second lock down } } /* Update button states */ oldButtonState1 = buttonState1; oldButtonState2 = buttonState2; } /** * Subroutine to test our inputs are wired correctly. * Echoes the state of the input out the serial connection * if it changes */ void testInputs () { int buttonState1 = digitalRead(BUTTON1_PIN); int buttonState2 = digitalRead(BUTTON2_PIN); if (buttonState1 != oldButtonState1) { Serial.println("Button 1:"); Serial.println(buttonState1, DEC); } if (buttonState2 != oldButtonState2) { Serial.println("Button 2:"); Serial.println(buttonState2, DEC); } oldButtonState1 = buttonState1; oldButtonState2 = buttonState2; } /** * Subroutine to test our outputs are wired correctly. * Turns all ouput LEDs on and then off again. */ void testOutputs () { digitalWrite(READY_PIN, HIGH); digitalWrite(PLAYER1_PIN, HIGH); digitalWrite(PLAYER2_PIN, HIGH); digitalWrite(SIGNAL_PIN, HIGH); delay(500); digitalWrite(READY_PIN, LOW); digitalWrite(PLAYER1_PIN, LOW); digitalWrite(PLAYER2_PIN, LOW); digitalWrite(SIGNAL_PIN, LOW); delay(500); }