Arduino Intro Labs for Tangible Computing
4. Lab 2 - Tug of War




4.1 Introduction

The previous lab exercise was a very intense introduction to some key ideas in hardware circuits and Arduino C/C++ programs. This lab does not introduce any new concepts, but instead asks you to take an existing sketch (hardware plus software) and "enhance" it. The outcomes of this part are:
  1. Handle simultaneous inputs from multiple sources (two buttons).
  2. Use chains of if-then conditionals.
  3. Use variables to introduce additional state and behaviour (for example detecting that a button has gone down, then back up, and acting only after it has gone back up.)
  4. Begin developing diagnostic/debugging skills.
The circuit below, and accompanying program, implement a tug of war game. Please go ahead and construct it as shown below, and start a new Arduino sketch with the Tug of War code given below. If you're having troubles you can try pulling out all the wires and try again. Sometimes it's faster just to re-wire everything rather than trying to find out which piece of hardware is an issue.
Arduino Mega Arduino Uno
Click the image for full size!






code/Lab_TugOfWar/Lab_TugOfWar.ino

    /* Tug of war game:
     
    Two players, Player 1 and Player 2, are at a 
    board consisting of 2 push buttons and 3 LEDS
     
    There are 3 LEDs
        RED1 GRN RED2
    and 2 buttons
        B1 B2
     
    The objective is to push your button faster than your opponent
    so that when time runs out you have the rope on your side.
     
    Whoever has pushed their button the most times will be
    winning, and their red LED will turn on.  If the two buttons
    are within Threshold of each other, then the players are
    currently tied, and the green LED will be on.
       
    */
    int P1LEDLowPin = 10;
    int P2LEDLowPin = 8;
    int LEDEvenPin = 9;
     
    int P1ButtonPin = 7;
    int P2ButtonPin = 6;
     
    void setup() {
      // configure LED pins to be a digital outputs
      pinMode(P1LEDLowPin, OUTPUT);
      pinMode(P2LEDLowPin, OUTPUT);
      pinMode(LEDEvenPin, OUTPUT);
     
      // set button pins to INPUT and 
      // turn on internal pull up resistor 
      pinMode(P1ButtonPin, INPUT);
      digitalWrite(P1ButtonPin, HIGH);
      pinMode(P2ButtonPin, INPUT);
      digitalWrite(P2ButtonPin, HIGH);
    }
     
    /* PushCount is incremented when player 1 pushes their
       button, and decremented when player 2 pushes their 
       button.  So if it is > 0 player 1 has made more pushes
       and if it is < 0 player 2 has made more pushes.
    */
     
    int PushCount = 0;
     
    /* PushCount has to be Threshold far away from zero before a
       player is considered to be winning.
    */
     
    int Threshold = 5;
     
    void loop() {
      if ( digitalRead(P1ButtonPin) == LOW ) {
        PushCount++;
      }
      if ( digitalRead(P2ButtonPin) == LOW ) {
        PushCount--;
      }
     
      if ( PushCount > Threshold ) {
        digitalWrite(P1LEDLowPin, HIGH);
        digitalWrite(P2LEDLowPin, LOW);
        digitalWrite(LEDEvenPin, LOW);
      } else if ( PushCount < (0-Threshold) ) {
        digitalWrite(P1LEDLowPin, LOW);
        digitalWrite(P2LEDLowPin, HIGH);
        digitalWrite(LEDEvenPin, LOW);
      } else {
        digitalWrite(P1LEDLowPin, LOW);
        digitalWrite(P2LEDLowPin, LOW);
        digitalWrite(LEDEvenPin, HIGH);
      }
     
      // wait 50 mS for any button bounce to die out
      delay(50);
    }
     


Your tasks: Questions:
4. Lab 2 - Tug of War
Arduino Intro Labs for Tangible Computing / Version 1.00 2012-09-24