Tug of War

Note

🌟 Welcome to the SunFounder Facebook Community! Whether you’re into Raspberry Pi, Arduino, or ESP32, you’ll find inspiration, help ideas here.

  • ✅ Be the first to get free learning resources.

  • ✅ Stay updated on new products & exclusive giveaways.

  • ✅ Share your creations and get real feedback.

  • 👉 Need faster updates or support? Click [here] join our Facebook community

  • 👉 Or join our WhatsApp group: Click [here]

Kit purchase

Looking for parts? Check out our all-in-one kits below — packed with components, beginner-friendly guides, and tons of fun.

../_images/ultimate_sensor_kit.png

Name

Includes Arduino board

PURCHASE LINK

Elite Explorer Kit

Arduino Uno R4 WiFi

BUY

3 in 1 Ultimate Starter Kit

Arduino Uno R4 Minima

BUY

Course Introduction

In this lesson, we’ll create a two-player LED tug-of-war game using an LED strip, two buttons, and a buzzer, where players race to push the light to their side to win.

Note

If this is your first time working with an Arduino project, we recommend downloading and reviewing the basic materials first.

Required Components

In this project, we need the following components:

SN

COMPONENT INTRODUCTION

QUANTITY

PURCHASE LINK

1

Arduino UNO R4 Minima/Arduino UNO R4 WIFI

1

BUY

2

USB Type-C cable

1

3

Breadboard

1

BUY

4

Wires

Several

BUY

5

Passive Buzzer

1

BUY

6

Button

2

BUY

7

LED Strip

1

BUY

Wiring

../_images/tug_of_war_bb.png

Common Connections:

  • Button

    • button1: Connect to the breadboard’s negative power bus, and the other end to 2 on the Arduino board.

    • button2: Connect to the breadboard’s negative power bus, and the other end to 3 on the Arduino board.

  • Passive Buzzer

    • +: Connect to 9 on the Arduino.

    • -: Connect to breadboard’s negative power bus.

  • LED Strip

    • Din: Connect a to 220Ω resistor then to 6 on the Arduino.

    • GND: Connect to breadboard’s negative power bus.

    • +5V: Connect to breadboard’s passive power bus.

Writing the Code

Note

  • You can copy this code into Arduino IDE.

  • To install the library, use the Arduino Library Manager and search for Adafruit_NeoPixel and install it.

  • Don’t forget to select the board(Arduino UNO R4 WIFI) and the correct port before clicking the Upload button.

  1. Information writing

#include <Adafruit_NeoPixel.h>

#define LED_PIN    10
#define LED_COUNT  8    // Number of LEDs
#define BTN1_PIN   2    // Player 1 button (Red side)
#define BTN2_PIN   3    // Player 2 button (Blue side)
#define BUZZER_PIN 9    // Buzzer

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

int position = LED_COUNT / 2;  // Initial center index (4)
bool gameRunning = false;

// Button states for edge detection
bool lastBtn1State = HIGH;
bool lastBtn2State = HIGH;

void setup() {
  pinMode(BTN1_PIN, INPUT_PULLUP);
  pinMode(BTN2_PIN, INPUT_PULLUP);
  pinMode(BUZZER_PIN, OUTPUT);

  strip.begin();
  strip.show();
  drawInitial();  // Initial: left 4 red, right 4 blue
}

void loop() {
  bool btn1State = digitalRead(BTN1_PIN);
  bool btn2State = digitalRead(BTN2_PIN);

  if (!gameRunning) {
    // Wait for both buttons pressed to start game
    if (btn1State == LOW && btn2State == LOW) {
      delay(300);  // debounce
      position = LED_COUNT / 2;  // Reset to center
      gameRunning = true;
      drawPosition();
    }
  } else {
    // Player 1 press detected (edge) → push right
    if (lastBtn1State == HIGH && btn1State == LOW) {
      position++;
      tone(BUZZER_PIN, 1200, 50);
    }
    // Player 2 press detected (edge) → push left
    if (lastBtn2State == HIGH && btn2State == LOW) {
      position--;
      tone(BUZZER_PIN, 1000, 50);
    }

    drawPosition();

    // Victory check
    if (position >= LED_COUNT - 1) {
      winAnimation(1);  // Player 1 (Red) wins
      gameRunning = false;
    }
    if (position <= 0) {
      winAnimation(2);  // Player 2 (Blue) wins
      gameRunning = false;
    }
  }

  lastBtn1State = btn1State;
  lastBtn2State = btn2State;
}

// Initial symmetric display: left 4 red, right 4 blue
void drawInitial() {
  strip.clear();
  for (int i = 0; i < 4; i++) {
    strip.setPixelColor(i, strip.Color(255, 0, 0)); // Red
  }
  for (int i = 4; i < 8; i++) {
    strip.setPixelColor(i, strip.Color(0, 0, 255)); // Blue
  }
  strip.show();
}

// Game progress display (no green light, just color boundary)
void drawPosition() {
  strip.clear();
  for (int i = 0; i < LED_COUNT; i++) {
    if (i < position) {
      strip.setPixelColor(i, strip.Color(255, 0, 0)); // Red side (left of boundary)
    } else {
      strip.setPixelColor(i, strip.Color(0, 0, 255)); // Blue side (right of boundary)
    }
  }
  strip.show();
}

void winAnimation(int player) {
  uint32_t color = (player == 1) ? strip.Color(255, 0, 0) : strip.Color(0, 0, 255);
  for (int i = 0; i < 5; i++) {
    strip.clear();
    for (int j = 0; j < LED_COUNT; j++) {
      strip.setPixelColor(j, color);
    }
    strip.show();
    tone(BUZZER_PIN, player == 1 ? 1200 : 1000, 200);
    delay(200);
    strip.clear();
    strip.show();
    delay(200);
  }
  // Final solid winner color
  for (int j = 0; j < LED_COUNT; j++) {
    strip.setPixelColor(j, color);
  }
  strip.show();
  noTone(BUZZER_PIN);
}