3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Interactive Beer Pong (Beirut) Table

Step 9The Code

The Code
The Arduino Sketch is below and attached:

 /* Interactive Pong Table - by Rohit Kabra
Following is the code for an interactive pong table I designed for a class called Making Things Interact at Carnegie Mellon University. 
The table was built to change the game of Beer Pong (Beirut) so that the table reacts to the current state of the game. 
A 3'x8' table was built that housed holes for 6 cups on either end. The following code controls only one side (6 cups).
Force Sensitive Resistors were mounted underneath each cup and wired to an Arduino. 
The program below tells whether or not there is a cup on the resistor and depnding on how many cups have been removed a series of RGB LEDs change color. 
The RESET button is especially curious as it is just the top cup on the table pressed down a little harder. 
*/

int fsrCup0 = 0; //force sensitive resitor for top cup
int fsrCup1 = 1; //fsr for middle left cup
int fsrCup2 = 2; //fsr for middle right cup
int fsrCup3 = 3; //fsr for bottom left cup
int fsrCup4 = 4; //fsr for bottom middle cup
int fsrCup5 = 5; //fsr for bottom right cup

 //the 6 force sensitive resistors are controlled by analog pins 0-5. 
 //the following array will hold the input of the 6 sensors
int myCups [] = {0,1,2,3,4,5}; 
boolean isCupHit [] = {false, false, false, false, false, false}; //array that tells whether cup has been removed or not
int numberOfCupsHit = 0; //a variable that tells the program how many cups have been removed from the table

int redLED = 3; //all red's of the RGB LEDS controlled by pin 3
int greenLED = 5; //all greens's of the RGB LEDS controlled by pin 5
int blueLED = 9; //all blue's of the RGB LEDS controlled by pin 9
int redValue; //the current value of the red LED's, helps to change colors
int blueValue; //the current value of the blue LED's, helps to change colors
int greenValue; //the current value of the green LED's, helps to change colors

void setup()
{
  pinMode(redLED, OUTPUT); //initiated the red LEDs as outputs
  pinMode(greenLED, OUTPUT); //initiated the green LEDs as outputs
  pinMode(blueLED, OUTPUT); //initiated the blue LEDs as outputs
  Serial.begin(9600); //starts up the sketch
}

void loop()
{
  if(analogRead(myCups[0]>>1100)) reset(); //if the top cup is pushed down the program resets, and lights go back to initial state
  
  for(int x = 0; x <= 5; x++)
  {
    
    Serial.print (analogRead(myCups[x])); //a print out of the 6 cups pressure for testing purposes
    Serial.print("\t"); 
    
    if(isCupHit[x] == false) //checks to make sure only looping through cups that have NOT been hit
    {
      if(analogRead(myCups[x])>300) //checks cup's fsr, if cup has been removed then force should drop below 300
      {
        
        numberOfCupsHit++; //adds 1 to total number of cups hit thus far in the game
        interact(numberOfCupsHit); //run the method interact with the specific number of cups hit
        isCupHit[x] = true; //makes sure the cup that was just hit isnt checked again for being hit or not
      }
    }
  }
  
  Serial.println();//space the testing printouts
}

//the interact method is meant to act like a finite state machine. With each cup initing a different state of the lights. 
void interact(int state)
{
  //state 1 - after first cup is hit
  //color will fade into bluish green as the bar moves 
  if (state == 1) 
  {
    blueValue = 255; //sets starting values of blues
    greenValue = 0; //sets starting value of greens
    for(int x = 0; x <= 125 ; x++)
    {
      analogWrite(blueLED, blueValue--); //fades out blue to desired brightness
      analogWrite(redLED, 0); //sets value of red input to 0 or no red
      analogWrite(greenLED, greenValue++); //fades green to desired brightness
      delay(10);
    }
  }
  
  //state 2 - after second cup is hit
  //color will fade into green as the bar moves 
  if (state == 2) 
  {
    blueValue = 125; //sets starting value of blues before fading
    greenValue = 125; //sets starting value of greens before fading
    for(int x = 0; x <= 125 ; x++) 
    {
      analogWrite(blueLED, blueValue--); //dims out blue to off
      analogWrite(redLED, 0); //sets value of red input to 0 or no red
      analogWrite(greenLED, greenValue++); //fades green to full brightness
      delay(10);
    }
  }
  
  //state 3 - after third cup is hit
  //color will fade into CYAN as the bar moves 
  if (state == 3) 
  {
    blueValue = 0; //sets starting value of blues before fading
    greenValue = 255; //sets starting value of greens before fading
    for(int x = 0; x <= 255 ; x++) 
    {
      analogWrite(blueLED, blueValue++); //fades in blue so color turns cyan
      analogWrite(redLED, 0); //sets value of red input to 0 or no red
      analogWrite(greenLED, greenValue); //stays green throughout movement
      delay(5); //only 5 delay because of longer loop
    }
  }
      
  //state 4 - after fourth cup is hit
  //color will fade into PINK as the bar moves. 
  if (state == 4) 
  {
    blueValue = 250; //sets starting value of blues before fading
    greenValue = 250; //sets starting value of greens before fading
    redValue = 0; //sets starting value of reds before fading
    for(int x = 0; x <= 125 ; x++)
    {
      analogWrite(blueLED, blueValue--); //fades in blue so color turns cyan
      analogWrite(redLED, redValue++); //sets value of red input to 0 or no red
      analogWrite(greenLED, greenValue-2); //stays green throughout movement
      delay(10);
    }
  }
  
  //state 5 - after fifth cup is hit
  //color will fade to red and start pulsing
  if (state == 5) 
  {
    for(int x = 0; x <= 125 ; x++) //loop to fade to red
    {
      analogWrite(blueLED, blueValue--); //fades in blue so color turns cyan
      analogWrite(redLED, redValue++); //sets value of red input to 0 or no red
      delay(10);
    }
    
    do{ //pulsing loop
       for (int intensity = 255; intensity >=0; intensity++)
       {
         analogWrite(redLED, intensity);
         delay(2);
       }
       for(int intensity=0;intensity<=255;intensity++) 
       {  analogWrite(redLED,intensity);
          delay(2);
       }
    }while(state!=6); //break once state 6 is reached
    
  }
     
   //state 6 - after final cup has been removed
   //colors will quickly start flashing and then reset after 2 seconds of being off 
   if (state == 6)
    { 
      for(int flashing = 0; flashing<=10;flashing++)
      {
        //pulse red fast
       for(int intensity=0;intensity<=255;intensity++) 
       {  analogWrite(redLED,intensity);
          delay(1);
       }
       for (int intensity = 255; intensity >=0; intensity++)
       {
         analogWrite(redLED, intensity);
         delay(1);
       }
       //pulse green fast
       for(int intensity=0;intensity<=255;intensity++) 
       {  analogWrite(greenLED,intensity);
          delay(1);
       }
       for (int intensity = 255; intensity >=0; intensity++)
       {
         analogWrite(greenLED, intensity);
         delay(1);
       }
       //pulse blue fast
       for(int intensity=0;intensity<=255;intensity++) 
       {  analogWrite(greenLED,intensity);
          delay(1);
       }
       for (int intensity = 255; intensity >=0; intensity++)
       {
         analogWrite(greenLED, intensity);
         delay(1);
       }
      }
      
      analogWrite(blueLED, 0); //shuts off blue LED for 2 seconds
      analogWrite(redLED, 0); //shuts off red LED for 2 seconds
      analogWrite(greenLED, 0); //shuts off green LED for 2 seconds
      delay(2000); //wait 2 seconds
      
       reset(); //run the reset program
    }   
}
    
//reset program sets variables back to default state and puts LED's back to blue    
void reset()
{
  for(int z = 0; z<6 ; z++)
  {
    isCupHit[z] = false; //refills the array with false's, telling the program that no cups have been hit. 
  }
  
  numberOfCupsHit = 0; //no cups of have been hit
  analogWrite(redLED, 0); //red leds are off
  analogWrite(greenLED, 0); //green leds are off
  analogWrite(blueLED, 255); //blue leds on high
  redValue = 0; //store red led value as off or 0
  blueValue = 255; //store blue led value as high
  greenValue = 0; //store green led value as 0
  
}
  
« Previous StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
0
Followers
1
Author:rohitk