TUTORIALS

Tug of War // Arduino Project for Beginners with Code | Shortcuit

This tutorial will show you how to build a Tug of War Arduino project. A simple LED project that uses pushbuttons and internal pull-up resistors.

4 Feb 2022

In this post, you'll learn how to build a simple Arduino project with LEDs - the Tug of War project.

Project Info

There are pushbuttons on the left and right side, one for each player.
The player who pushes the fastest, will 'pull' the light to his/her side and win the game.
After someone wins, the light will reset and light up at the start position.

Video Tutorial

You can watch the video tutorial where I build the project step-by-step.

Which components do you need?

  • 11 x 5mm LEDs
  • 11 x 220 resistors (shouldn’t go lower than 160)
  • 2 x pushbuttons
  • breadboard
  • jumper wires
  • Arduino Uno (Shortcuit Uno)

How to build the project?

1. Place the LEDs in the breadboard

Place the first LED with the cathode pin in B 26 and anode pin next to it in B 25.
From there we can place the other 10 LEDs next to this one in the same way, so short pin left and long pin right.

2. Place the resistors in the breadboard

Place the resistors vertically in the same column as each negative pin of an LED. So the top pin of the resistors start in row E.

3. Insert the pushbuttons at each end of the breadboard

The first one will be placed vertically on the left side, with the left pins in column 30.
The right one has the right pins in the first column.

4. Place the Arduino UNO next to the breadboard on the top side

Place the Arduino UNO next to the breadboard with the digital pins on top.

5. Connect the components with wires

From here on we need to place wires. Start with digital pin 12 and connect it to the anode of the most left LED. Then do the same with the other digital pins and stop once you have connected pin 2.

The next step is to connect all the components to the Ground or GND pin of the Arduino. Connect the GND pin to the most left hole of the negative row at the bottom. Draw wires from the negative row to the pushbuttons and resistors.

Now we’ll need to connect the pushbuttons with the analog side. Connect A0 with the top left pin of the left pushbutton. Then connect A1 with the top left pin of the right pushbutton.

6. Connect a 9V battery as a power source (optional)

And lastly I’m going to add a 9V battery as a power source. Connect the positive wire of the battery to the Vin pin of the Arduino and negative wire to the GND pin. You can also connect your Arduino UNO to your laptop/desktop without having to use the battery.

Code Overview

Now that all the components and wires are connected, we can paste the code in a new Arduino sketch file and upload it. Check out the YouTube tutorial for a more detailed explanation of the code.

/*
  Tug of War

  There are 2 pushbuttons on each side. The player who presses on the button the fastest, 
  'pulls' the light to one side and when it reaches the most left or right LED, one of the players wins.

  Created 01/02/2022
  By Shortcuit

  More information can be found in the blog post:
  >>> https://shortcuit.com/blog/tutorials/electronics-projects/tug-of-war-arduino-project-for-beginners-with-code/

  shortcuit.com
  Educational game to learn electronics and Arduino programming.
*/


int currentLedPosition = 5; //middle led if count starting from 0
int previousLedPosition = 5;

int previousStateLeftBtn = HIGH;
int previousStateRightBtn = HIGH;
unsigned long timeSinceLeftPress = 0;
unsigned long timeSinceRightPress = 0;

const int LED_START = 2; //start at digital pin 2
const int LED_COUNT = 11;
const byte LEFT_BTN = A0;
const byte RIGHT_BTN = A1;
const int WAIT_TIME = 200; //ms

void setup()
{ 
  Serial.begin(9600);
  
  //set pushbutton input as input pullup
  pinMode(LEFT_BTN, INPUT_PULLUP);
  pinMode(RIGHT_BTN, INPUT_PULLUP);
  
  //set all leds as output and low
  for (int i=LED_START; i<LED_COUNT + LED_START; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, LOW);
  }
  
  //set current led high
  digitalWrite(currentLedPosition + LED_START, HIGH);
}

void loop() 
{
  unsigned long nowTime = millis();
  
  //handle button input
  int currentStateLeftBtn = digitalRead(LEFT_BTN);
  int currentStateRightBtn = digitalRead(RIGHT_BTN);
  if (currentStateLeftBtn == LOW && previousStateLeftBtn == HIGH && (nowTime - timeSinceLeftPress) > WAIT_TIME)
  {
    timeSinceLeftPress = nowTime; //track current time
    currentLedPosition++;
  }
  if (currentStateRightBtn == LOW && previousStateRightBtn == HIGH && (nowTime - timeSinceRightPress) > WAIT_TIME)
  {
    timeSinceRightPress = nowTime;
    currentLedPosition--;
  }
  previousStateLeftBtn = currentStateLeftBtn;
  previousStateRightBtn = currentStateRightBtn;

  //handle current led
  if (currentLedPosition != previousLedPosition)
  {
    //turn off previous position
    digitalWrite(previousLedPosition + LED_START, LOW);
    //turn on current position
    digitalWrite(currentLedPosition + LED_START, HIGH);
    
    //update previousPosition
    previousLedPosition = currentLedPosition;
  }
  
  //handle win
  if (currentLedPosition < 0)
  {
    Serial.println("Right wins!");
    win();
  }
  else if (currentLedPosition >= LED_COUNT)
  {
    Serial.println("Left wins!");
    win();
  }
}

void win()
{ 
  delay(2000); //wait 2 seconds
  currentLedPosition = 5; //reset position
}

Final Result

Shortcuit Project

Real Project

Here we have the project with real components, the wire colors are a bit different but it works the same way as the one in Shortcuit.

Get on early access list

Be one of the first to try it when early release launches.