Learn Arduino with Olympia Circuits
Learn Arduino
  • Home
    • Get Started
    • How to Use This Site
  • Electronics
    • The Basics
    • Electricity Flows like Water
    • Electronic Components
    • The Arno Board
  • Programming
    • The Basics
    • setup and loop Blocks
    • Variables and Arrays
    • Connecting with the Pins
    • Flow Control >
      • if Statement
      • Loops
      • Delays
    • Functions
    • Serial Communication
    • USB
    • Some Thoughts
  • Projects
    • Projects 1 >
      • 1.01: Blink
      • 1.02 Blink x2
      • 1.03 Blink Faster
      • 1.04 LED Chase!
      • 1.05 Wait To Blink
      • 1.06 Blink a Little Faster Now
      • 1.07 LED Fade
      • 1.08 RGB Blink
      • 1.09 Change RGB Color with SW1
      • 1.10 Fade RGB Colors
      • 1.11 Reaction Time Game
    • Projects 2 >
      • 2.01 Hello World
      • 2.02 Talk Back
      • 2.03 ASCII Values
      • 2.04 Ski Game
      • 2.05 Demonstration of the String Object
    • Projects 3 >
      • 3.01 Read the Potentiometer
      • 3.02 ASCIIbet Soup
      • 3.03 Potentiometer sets LED Brightness
      • 3.04 Potentiometer Sets Blink Rate
      • 3.05 LED Chase, Part II
    • Projects 4 >
      • 4.01 Bringing the Piezo to Life
      • 4.02 Controlling the Piezo with a Function
      • 4.03 Piezo C Major
      • 4.04 Piezo Greensleaves
      • 4.05 Piezo Metronome
      • 4.06 Piezo as an Input
      • 4.07 Piezo as an Input 2
      • 4.08 Metronome II
      • 4.09 Piezo Playback
      • 4.10 Piezo Fireworks
      • 4.11 Piezo Mosquito
    • Projects 5 >
      • 5.01 The Phototransistor
      • 5.02 Light and Sound
      • 5.03 Light and Sound II
    • Projects 6 >
      • 6.01 EEPROM
      • 6.02 I2C Address Scan
      • 6.03 Read the I2C Temperature Sensor
      • 6.04 High Temperature Alarm
    • Projects 7 >
      • 7.01 Arno Phone Home
      • 7.02 Keyboard Alphabet
      • 7.03 Move Mouse
      • 7.04 Draw Squares
    • Special Projects >
      • Bike Light Demo
  • References
    • Arno Pin Key
    • Arno Schematic
    • Project Index

Project 1.11 Reaction Time Game

Project 1.11 Reaction Time Game We have now covered enough basic concepts to create a game.  It pits two players against each other to see who can react first by pressing their button (SW1 or SW2) when the RGB LED changes from red to green.  We also check to make sure neither player faults by pressing their button too soon. The single LED on each player’s side will flash along with the green channel of the RGB LED for the winning player.  The RGB LED will flash red if a player faults, along with the single LED on the player’s side.  We’ll look at the code and explain it afterwards.


Concepts: multiple inputs, random function

Circuits:
  • Circuit 1
  • Circuit 2
  • Circuit 3


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 10 Reaction time game
    int redLED = 9;
    int greenLED = 10;
    int LED2 = 6;
    int LED3 = 7;
    int winner = 0;
    int fault = 0;
    int timeLapse;
    int SW1 = 1;
    int SW2 = 4;
    long wait = 0;
    long now = 0;
    void setup(){
    pinMode(redLED,OUTPUT);
    pinMode(greenLED,OUTPUT);
    pinMode(LED3,OUTPUT);
    pinMode(LED2,OUTPUT);
    pinMode(SW1,INPUT);
    pinMode(SW2,INPUT);
    }
    void loop(){
    digitalWrite(redLED,HIGH);
    fault = 0;
    now = millis();
    wait = now + random(3000,6000);
    while(millis() < wait && digitalRead(SW1)==HIGH && digitalRead(SW2)==HIGH){
    }
    if(digitalRead(SW1)==LOW)fault = LED2;
    if(digitalRead(SW2)==LOW)fault = LED3;
    digitalWrite(redLED,LOW);
    //only continue if delay actually ran out
    if(fault == 0){
    digitalWrite(greenLED,HIGH);
    //wait until a player presses a button
    while(digitalRead(SW1)==HIGH && digitalRead(SW2)==HIGH){
    }
    //find which player won
    if(digitalRead(SW1)==LOW){
    winner = LED2;
    }
    else{
    winner = LED3;
    }
    for(int k = 0; k < 10; k++){
    digitalWrite(greenLED,HIGH);
    digitalWrite(winner,HIGH);
    delay(50);
    digitalWrite(winner,LOW);
    digitalWrite(greenLED,LOW);
    delay(50);
    }
    }
    else{
    //show that someone faulted and start over
    for(int k = 0; k < 10; k++){
    digitalWrite(redLED,HIGH);
    digitalWrite(fault,HIGH);
    delay(50);
    digitalWrite(redLED,LOW);
    digitalWrite(fault,LOW);
    delay(50);
    }
    }
    }
    ///////////////////////////////////////////////////

    At the beginning of the loop() block, the red channel of the RGB LED is turned on and a timer is set to wait before turning green:

      now = millis();

      wait = now + random(3000,6000);

      while(millis() < wait && digitalRead(SW1)==HIGH && digitalRead(SW2)==HIGH){

      }

    The random function returns a value between the first and second argument passed to the function. The variable wait holds the value that millis() must reach before the RGB LED turns green.  The while loop pauses the execution of the sketch until either enough time has passed or until a player presses their button (i.e., SW1 or SW2 is no longer HIGH).

    If while loop ends with one of the player’s buttons pressed, we record a fault:

      if(digitalRead(SW1)==LOW)fault = LED2;

      if(digitalRead(SW2)==LOW)fault = LED3;

      digitalWrite(redLED,LOW);

    If no fault has occurred, the RGB LED changes to green and the sketch waits for a player to press their button:

    if(fault == 0){

        digitalWrite(greenLED,HIGH);

        //wait until a player presses a button

        while(digitalRead(SW1)==HIGH &&        

              digitalRead(SW2)==HIGH){

        }

    Once a button is pressed, we determine a winner and blink the correct LED 10 times along with the green RGB channel:

        //find which player won

        if(digitalRead(SW1)==LOW){

          winner = LED2;

        }

        else{

          winner = LED3;

        }

        for(int k = 0; k < 10; k++){

          digitalWrite(greenLED,HIGH);

          digitalWrite(winner,HIGH);

          delay(50);

          digitalWrite(winner,LOW);

          digitalWrite(greenLED,LOW);

          delay(50);

        }

      }

    We now return to the else block that is executed if a fault occurred:

    else{

        //show that someone faulted and start over

        for(int k = 0; k < 10; k++){

          digitalWrite(redLED,HIGH);

          digitalWrite(fault,HIGH);

          delay(50);

          digitalWrite(redLED,LOW);

          digitalWrite(fault,LOW);

          delay(50);

        }

      }

    Back to Projects 1

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.