beer pong trebuchet

Want a fun way to familiarize others with programming an Arduino? Make a tebuchet you can play beer pong with!

Introduction

For anybody working in IT, knowledge and experience with programming is always useful. For people that practise learning by doing, a fun project to work on is key. Beer pong is a well-known game, esspecially by young adults. Building a controllable trebuchet with an Arduino to play beer pong is defenitely an awesome project.

For anybody interested, here follows a guide for connecting the required components to the Arduino UNO and the used code. The construction itself, made here by Meccano, can be made with different system of course.

Check out the video for the end result.

Set-up steps

  1. Required parts:
  2. For this project you need two Arduino boards. One controls the trebuchet and the other measures the distance. Connect the stepper motor & driver, joystick and relay to the first Arduino and the ultra sound module with the display to the ohter.
  3. Download the CustomStepper library from The Arduino Playground and add it to the library files. Although convenient to use, the library is tricky to get familiar with. Read the provided documentation on the website carefully.
  4. Initialize the stepper: pin numbers, the stepping sequence, steps per rotation, revolutions per minute, rotation. In the beginning, use the code as is and play with the values once you get it up and running.

#include <CustomStepper.h< //step motor library (playground.arduino.cc/Main/CustomStepper/)

CustomStepper stepper(   8, 9, 10, 11,
  (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001},
  4075.7728395, 12, CW);
#define joyX A0 //joystick back front
#define joyY A1 //joystick left right
#define joyZ 2 //joystick up down
#define relay 3 //Relay
#define trigPin 4 // Trigger
#define echoPin 5 // Echo

long duration, cm;
boolean magnetOn = false;

void setup()
{
  Serial.begin(9600); // Setup serial connection for print out to console
  stepper.setRPM(12);
  stepper.setSPR(4075.7728395);

  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);

  pinMode(joyX, INPUT);
  pinMode(joyY, INPUT);
  pinMode(joyZ, INPUT_PULLUP);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop()
{
  int xValue = analogRead(joyX);
  int yValue = analogRead(joyY);
  int zValue = digitalRead(joyZ);

  if (stepper.isDone())
  {
    if (xValue==1023 && zValue==1 && yValue!=0 && yValue!=1023)
    {
      stepper.setDirection(CW);
    }
    else if (xValue==0)
    {
      stepper.setDirection(CCW);
    }
    stepper.rotate();
  }
  else
  {
    stepper.setDirection(STOP);
  }
  stepper.run();

  if (yValue==0 && xValue<600 && xValue>500)
  {
    Serial.println(getDistance());

    while (analogRead(joyY)==0) {delay(1000);}
  }

  if (zValue==0)
  {
    if (magnetOn == false)
    {
      digitalWrite(relay, HIGH);
      magnetOn = true;
    }
  else
  {
    digitalWrite(relay, LOW);
    magnetOn = false;
  }
  delay(1000);
  }
}

int getDistance()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
  return cm;

}

/*
Each letter represents a segment of the display. The horizontal segments from top to bottom: A, G and D. The vertical segments clockwise: B, C, E and F. The decimal point is represented by H.
    _A_
  F | | B
    _G_
  E | | C
    _D_ .H
*/

// Pin 2-8 is connected to the 7 segments of the display.

int pinA = 15;
int pinB = 19;
int pinC = 6;
int pinD = 8;
int pinE = 9;
int pinF = 16;
int pinG = 5;
int pinH = 7;
int D1 = 14;
int D2 = 17;
int D3 = 18;
int D4 = 4;
int inputLength;
int displayNumbers[]={4,18,17,14};
#define trigPin 3 // Trigger
#define echoPin 2 // Echo
// the setup routine runs once when you press reset:
void setup()
{
  Serial.begin(9600);
  // initialize the digital pins as outputs.
  pinMode(pinA, OUTPUT);
  pinMode(pinB, OUTPUT);
  pinMode(pinC, OUTPUT);
  pinMode(pinD, OUTPUT);
  pinMode(pinE, OUTPUT);
  pinMode(pinF, OUTPUT);
  pinMode(pinG, OUTPUT);
  pinMode(pinH, OUTPUT);
  pinMode(D1, OUTPUT); //on=LOW, off=HIGH, dot at the bottom, left to right
  pinMode(D2, OUTPUT);
  pinMode(D3, OUTPUT);
  pinMode(D4, OUTPUT);
  digitalWrite(D1, HIGH);
  digitalWrite(D2, HIGH);
  digitalWrite(D3, HIGH);
  digitalWrite(D4, HIGH);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}
// the loop routine runs over and over again forever:
void loop()
{
  int distance = getDistance();
  displayNumber(distance);
}

void displayNumber(int input)
{
  int inputArray[] = {0,0,0,0};

  if (input>9999)
  {
    inputLength = 0;
  }
  else if (input>999)
  {
    inputLength = 4;
  }
  else if (input>99)
  {
    inputLength = 3;
  }
  else if (input>9)
  {
    inputLength = 2;
  }
  else
  {
    inputLength = 1;
  }
  inputArray[0] = (input/1) % 10;
  inputArray[1] = (input/10) % 10;
  inputArray[2] = (input/100) % 10;
  inputArray[3] = (input/1000) % 10;

  for (int i=0; i<5000; i++)
  {
    int position = 0;
    for (int v=0; v<inputLength; v++)
    {
      digitalWrite(displayNumbers[position], LOW);
      setNumber(inputArray[v]);
      setClear();
      digitalWrite(displayNumbers[position], HIGH);
      position++;
    }
  }
}

void setNumber(int digit){
  switch (digit)
  {
    case 1:
      setOne();
      break;
    case 2:
      setTwo();
      break;
    case 3:
      setThree();
      break;
    case 4:
      setFour();
      break;
    case 5:
      setFive();
      break;
    case 6:
      setSix();
      break;
    case 7:
      setSeven();
      break;
    case 8:
      setEight();
      break;
    case 9:
      setNine();
      break;
    default:
      setZero();
      break;
}
}

void setZero()
{
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, LOW);
digitalWrite(pinH, LOW);
}
void setOne()
{
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
digitalWrite(pinH, LOW);
}
void setTwo()
{
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, LOW);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
digitalWrite(pinH, LOW);
}void setThree()
{
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, HIGH);
digitalWrite(pinH, LOW);
}void setFour()
{
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
digitalWrite(pinH, LOW);
}void setFive()
{
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
digitalWrite(pinH, LOW);
}void setSix()
{
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
digitalWrite(pinH, LOW);
}void setSeven()
{
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
digitalWrite(pinH, LOW);
}void setEight()
{
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
digitalWrite(pinH, LOW);
}void setNine()
{
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, LOW);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
digitalWrite(pinH, LOW);
}void setClear()
{
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
digitalWrite(pinC, LOW);
digitalWrite(pinD, LOW);
digitalWrite(pinE, LOW);
digitalWrite(pinF, LOW);
digitalWrite(pinG, LOW);
digitalWrite(pinH, LOW);
}
int getDistance()
  {
    long duration;
    int distance;

  do {
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    distance = (int) (duration/2) / 29.1;
  } while (distance==0);

  return distance;
}

image of the Beer Pong Trebuchet image of the Beer Pong Trebuchet image of the Beer Pong Trebuchet image of the Beer Pong Trebuchet