Announcements

November 24, 2007
Reading for November 27th, are now posted. Enjoy!

October 2, 2007
To upload your thoughtless acts, create a new assignment page like any other lab. You'll see "Thoughtless Acts" listed as one of the assignment options.

May 24, 2008
This site has been archived and is no longer editable. Stay tuned for the next version, coming in the fall!


Revision of Jug Hero from Wed, 11/07/2007 - 21:59

Project Members: 
k7lim
Lora Oehlberg
Seung Wan Hong
Shawna Hein

Description

Haven't you always wanted to play Guitar Hero... But with Jugs??  Enter Jug Hero.  This little program allows the user to blow on various jugs, following along with a computer-generated screen which gives the user direction for which jugs to blow.

As a bonus, the user can determine the speed of the computer program depending on how fast or slow they want to blow.   

Components Used

.

Arduino Code

/*
 * sends serial out from a pot and a FSR
 */
int FSRPin = 0;   // select the input pin for the FSR
int FSRVal = 0;   // variable to store the value coming from FSR
int lastFSRVal = 0;

int lastTapTime = 0;
int tapTime = 0;
int numtaps = 0;
int difference = 0;
int average = 0;
double seconds = 0;
int THRESHOLD = 5;
unsigned long FINISHEDTIME = 500;

int delaytime = 0; //variable that keeps track of how long someone is not pressing the FSR
boolean startedTapping = false;

void setup() {
   Serial.begin(9600);
   //Serial.println("Start Tapping!");
}

void loop() {
   FSRVal = analogRead(FSRPin);   // read the value from FSR, between 0 - 1024
      
   Serial.print("V");
   Serial.println(FSRVal);
   Serial.print("L");
   Serial.println(lastFSRVal);
   //Serial.print("M");
   //Serial.println(average);
       
   
   if(FSRVal>THRESHOLD && lastFSRVal<THRESHOLD){  //If the person is starting a new tap
     startedTapping = true;
     delaytime = 0;  //delaytime gets set back to zero
     tapTime = millis();
     numtaps++;
     
     //Serial.print("How many taps have happened?");
     //Serial.println(numtaps);
     //delay(500);
   
     if(numtaps>1){  //if its not the first tap
     
       difference = tapTime - lastTapTime;  //calculates the difference in milliseconds between taps
       average = (average+difference)/numtaps;  //calculates the average
       
       /*Serial.print("D");
       Serial.println(difference);
       
       Serial.print("M");
       Serial.println(average);*/

     }
     
     lastTapTime = tapTime;          //set current taptime to lasttaptime variable
   
   }
   
   if(FSRVal<THRESHOLD && (startedTapping)){  //if the person is not tapping and they have already tapped at least once
     delaytime++; //increment delaytime
     Serial.println("T");
     Serial.println(delaytime);
   }
   
     //Serial.println(average);   
   
   if(delaytime>FINISHEDTIME && (startedTapping)){  //if its been awhile with the person not tapping
        Serial.print("F");
        Serial.println(average);   //print seconds to the serialport
        //delay(1000);
   }
   
   lastFSRVal = FSRVal;            //set current FSRVal to lastFSR variable                        
 
   
}

 

Processing Code

import java.util.Random;

import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/ttyUSB0"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10

int board_width = 400;
int board_height = 800;
int num_jugs = 4;
int num_rows = 8;
Random rand;
boolean[][] board;
int jug_width;
int jug_height;
PFont font;
boolean pause;
int points;
int milliseconds;
float seconds;
float fps;
float BPM = 0;
char code = 'Z';

void setup()
{
  port = new Serial(this, portname, 9600);
 
  /*while (port.available()<1){
    System.out.println("There is nothing in the Serial Port");
  }
 
  while(code!='F'){
    while (port.available() > 0) {
   
      //System.out.println("HELLO");
       int c = port.read();
       if (c != lf && c != cr) {
         buf += char(c);
       }
       if (c == lf) {
          String val = buf;
          code = val.charAt(0);
          String s = val.substring(1);
          milliseconds = int(s);
          println("code="+code);
          println("number="+milliseconds);
          buf="";
       }
    }
  }*/
      /*int c = port.read();
      if (c != lf && c != cr) {
        buf += char(c);
      }
      if (c == lf) {
        String val = buf;
        int milliseconds = int(val);
        System.out.println("milliseconds= "+milliseconds);   
        buf = "";
      }
    }
  }
 
  milliseconds = port.read();*/
 
  milliseconds = 1000;
  System.out.println("milliseconds again = "+milliseconds);
  milliseconds = milliseconds+500; //ADJUSTING FOR ERROR, LAG TIME....
  float m = float(milliseconds);
  seconds = (m/1000);
  System.out.println("Seconds = " +seconds);
  BPM = seconds*60;
  size(board_width, board_height);
  fps = 1/seconds;
  System.out.println("fps = " +fps);
  frameRate(fps);  
  board = new boolean[num_rows][num_jugs];
  rand = new Random();
  jug_width = board_width / num_jugs;
  jug_height = board_height / num_rows;
  font = loadFont("Ziggurat.vlw");
  pause = false;
  points = 0;
  textFont(font);
  newRow();
}

void newRow()
{
  int next_jug = rand.nextInt(num_jugs+1);
  if(next_jug == num_jugs) return;
  board[0][next_jug] = true;
}

void draw()
{
  if(!pause)
  {
    background(0);
    drawBoard();
    updateBoard(); 
  }
}

void updateBoard()
{
  copyDown();
  clearTop();
  newRow();
}

void copyDown()
{
   for(int col=0; col < num_jugs; col++)
   {
      for(int row=num_rows-1; row > 0; row--)
      {
        board[row][col] = board[row-1][col];
      }
   }
}

void clearTop()
{
  for(int i=0; i < num_jugs; i++)
  {
    board[0][i]=false;
  }
}

void drawBoard()
{
   for(int col=0; col < num_jugs; col++)
   {
     color c = color(col, 255, 255);
     drawOutline(num_rows-1, col, c);
   }
   colorMode(HSB, num_jugs, 255 ,255);
   for(int col=0; col < num_jugs; col++)
   {
      color c = color(col, 255, 255);
      for(int row=0; row < num_rows; row++)
      {
        if(board[row][col]) drawJug(row, col, c);
      }
   }
  
}

void drawOutline(int row, int col, color c)
{
  noFill();
  stroke(c);
  int x = (jug_width * col) + (int) (jug_width * .5);
  int y = (jug_height * row) + (int) (jug_height * .5);
  ellipse(x,y, jug_width, jug_height);
 
}

void drawJug(int row, int col, color c)
{
  int x = (jug_width * col) + (int) (jug_width * .5);
  int y = (jug_height * row) + (int) (jug_height * .5);
  fill(c);
  ellipse(x,y, jug_width, jug_height);
 
  fill(0, 102, 153);
  text("" + row + "," + col, x-(jug_width/4), y);
}

void scorePoint()
{
   points++;
   System.out.println("Points: " + points);
}

void togglePause()
{
   pause = !pause;
}

void serialEvent(Serial p) {
  int c = port.read();
  if (c != lf && c != cr) {
    buf += char(c);
  }
  if (c == lf) {
    String val = buf;
    code = val.charAt(0);
    String s = val.substring(1);
    milliseconds = int(s);
    println("code="+code);
    println("number="+milliseconds);
    buf="";
  }

  int key_num = 0;

 switch(code){
 
    case 'W':
      println("W Jug has been blown"); 
      key_num = 1;
      break;
    case 'X':
      println("X Jug has been blown"); 
      key_num = 2;
      break;
    case 'Y':
      println("Y Jug has been blown"); 
      key_num = 3;
      break;
    case 'Z':
      println("Z Jug has been blown"); 
      key_num = 4;
      break;
 }
 
 if(key_num > 0 && key_num <= num_jugs) tryKey(key_num);
 
}

void tryKey(int key_num)
{
  if(board[num_rows-1][key_num-1])
  {
    scorePoint();
    //System.out.println("row " + (num_rows-1) + ", jug " + (key_num-1));
  }
}
 

Item


Powered by Drupal - Design by Artinet