Making Things Interactive

March 28, 2008

Simon Says: Macbook Tilt Edition

Filed under: 8: State Machine, Joshua Smith — jssmith44 @ 2:08 pm

Below is the processing code for the Simon Says game I made involving tilting your macbook along different axis. Im going to build a better GUI for it, and update the code with it soon.

import sms.*;

int gameSize = 100;                               //max sequence number
int[] gameSeq = new int[gameSize];                // generated sequence (0,1)
int level = 0;
int threshold = 100;                            // threshold  value for tilt sense
int currentX = 0;                              // current state of tilt (0, 1, -1)
int lastX = 0;                                  // last state of tilt
int userIndex = 0;                              // where player is in sequence
int displayState = 0;                          // should display sequence?

void setup() {
  size(600,600);
  stroke(255);
  generateSequence();
   }

void draw() {
  background(0);
  int[] vals = Unimotion.getSMSArray();
  //println(vals[0] + " " + vals[1] + " " + vals[2]);
  lastX = currentX;
    if (vals[0] > threshold)
      currentX = 1;
    else if (vals[0] < (threshold *-1))
      currentX = -1;
    else
      currentX = 0;

  if(displayState == 0)
  displaySequence();   // show values up to index, then say go!

 if(detectChangeEvent() && currentX != 0)
      evaluate();

}

void evaluate() // determine if move matches sequence
  {
    if(currentX == 1 && gameSeq[userIndex] == 1){
      flashCorrect();
        if(userIndex < level)
          userIndex++;
       else
         {displayState = 0;
          level++;
          userIndex=0;
          currentX = 0;
          println();
          println("Round: " + (level+1));
          delay(2000);}
                                                 }
    else if(currentX == -1 && gameSeq[userIndex] == 0){
      flashCorrect();
        if(userIndex < level)
          userIndex++;
       else
         {displayState = 0;
          level++;
          userIndex = 0;
          currentX = 0;
          println();
          println("Round: " + (level+1));
          delay(2000);}
    }
    else
      reset();
  }

void displaySequence() // show sequence up to userIndex
{
  for(int i =0; i<level+1; i++){
   background(0);

   if(gameSeq[i] == 0){
     //line(400, 500, 200, 100);
     println("negative");
     delay(1500);}
   else
    { println("positive");
      delay(1500);
     //line(400, 100, 200, 500);
  }
}
  println();
  println("Now, Your Turn!!");
  displayState = 1;
}

boolean detectChangeEvent() //detect change in state

{

  if(currentX == lastX)
  return false;
  else
  return true;
}

void generateSequence() //genetare random sequence of size = gameSize
{

  for(int i = 0; i< gameSize; i++){
      gameSeq[i] = int(random(0,2));}

}

void flashCorrect(){
println("correct!");

}

void reset()
{
  println("WRONG!!");
}

Notice this game has to keep track of many states: The Level, The State of the game(Simon Says Part, or the I Show You Back Part) , The state of the position the player is in sequence, and the current state of each move(is it a move to the left, right, or no move).

International Dance Party

Filed under: Will Not Count Towards Your Grade — Jesse @ 12:45 am

This the Int’l Dance Party (IDP) system I describe in class. 

Update: Boomboxes State Machine

Filed under: 8: State Machine, Assignments, Jesse Chorng — Jesse @ 12:10 am
Tags:

I had some trouble with getting my state machine to work in class. I got home, went straight to Radio Shack and got headphone cable Y-adapter so I could hook up speakers to hear what’s actually being played. I don’t know what exactly went wrong in class but I suspect that the Shuffle’s output might have been different and wasn’t enough to cross the threshold from ‘Listening’ to ‘Audio Detected’

I got it to work simply by rechecking connections and being able to judge what state it should be in with the help of speakers. It goes from ‘listening’ (red LED), to ‘audio detected’ (blue LED), then to ‘party’ mode (dancing LED bar) via potentiometer,  back to ‘audio detected’, then to ‘party’, and once the music is paused back to ‘listening’  

Blog at WordPress.com.