Category Archives: Telecommunication

Hangman!

As a final revisit to our ICM class, I’ve created another game for the Telecommunications homework unlike the one-that-tried to-be-ambitious-with-rotation-but-failed. This game is a two-player Hangman (under 10 letters or less). The first player sets the word and then the second player actually guesses. And the actual communication part is to indicate loss or win by LEDS! There is also a RESET function if you lose or wish to choose another word. 

**Disclaimer: capital letters aren’t recognized and you can’t use backspace. Furthermore, when guessing a letter, “spaces” are counted as incorrect and somehow if you don’t enter anything (i.e. press “ENTER” without an actual input), it may break my game.

Choose Word

 

Loss

Win

lost win

 

 

And here is the code: 

import processing.serial.*;

Serial myPort;

int pos_X = 125;
int pos_Y = 125;
float size = 1.25;
int person_width = (int)(40*size);
int person_height = (int)((65*size) + (65*size) + (35*size));

//Typing for guessing: 
String typing = "";
char saved;
char [] temporary;
//Object:
Letters set;

//Game stages: 
//Set word:
boolean reset = false;
boolean start;
PFont font;
String set_word = "";
String final_word = "";
int word_length =0;

//Guess word:
boolean guess;
boolean background_on;
int lose_x;
int lose_y;
boolean increment_lose;
int total_lose = 10;
boolean word_right = false;
//Winner
boolean win;

void setup() {
  size(800, 600);
  start = true;
  guess = false;
  win = false;
  font = loadFont("Baskerville-48.vlw");
  lose_x = 400;
  lose_y=200;

  //Processing - ARDUINO
  printArray(Serial.list());
  String portName = Serial.list()[5];
  myPort = new Serial(this, portName, 9600);

}//End of setup

void draw() {
  if (start) {
    if (reset) {
      background(150, 150, 255);
      reset = false;
    }
    background(150, 150, 255);
    textFont(font, 32);
    fill(255);
    text("Type in the word (10 letters or less) that someone will guess.\n Press Enter to begin the game", 20, 50);
    text(set_word, 100, 400);
  }

  else if (guess) {
    if (background_on) {
      background(255);
      background_on = false;
    }
    textFont(font, 28);
    fill(184, 92, 245);
    text("Only the first character will be guessed.\n Press Enter to guess:  "+typing, 300, 550);
    //Hangman's stand
    fill(121, 63, 13);
    rect(40, 40, 150, 20); // top beam
    rect(40, 60, 30, person_height+60); // stand
    rect(40, person_height+120, 150, 30);//base
    fill(0);
    ellipse(53, 50, 7, 8);

    //ROPE
    stroke(245, 222, 179);
    strokeWeight(4);
    line(150, 60, 150, 100);

    set.draw_lines();
    if (set.check_letter(saved)) {
      set.draw_letter(saved);

    }
    else {
      if (increment_lose) {
        fill(184, 92, 245);
        text(saved, lose_x, lose_y);
        lose_x+=30;
        increment_lose = false;
        total_lose --;
        if (lose_x > 700) {
          lose_x = 400;
          lose_y+= 50;
        }
      }
    }
    losing();
    if (word_length == 0) {
      guess = false;
      textFont(font, 30);
      text("You've won! \n Press 1 to reset the game & play again.", 200, 350);
      myPort.write(1);

    }
  }//end of guess

  else if (win) {
  }
}//End of Draw

void keyPressed() {
  if (start) {
    if (key == '\n' ) {
      final_word = set_word;
      set = new Letters(final_word);
      word_length = final_word.length();
      System.out.println("start "+word_length);
      start = false;
      background_on = true;
      guess = true;
    } 
    else {
      // Otherwise, concatenate the String
      // Each character typed by the user is added to the end of the String variable.
      set_word = set_word + key;
    }
  }

  else if (guess) {
    if (key == '\n' ) {

      word_right = true;
      temporary = typing.toCharArray();
      saved = temporary[0];
      increment_lose = true;
      strokeWeight(0);
      stroke(255);
      typing = "";
      fill(255);
      rect(540, 560, 100, 30);
      // A String can be cleared by setting it equal to ""

    } 
    else {
      // Otherwise, concatenate the String
      // Each character typed by the user is added to the end of the String variable.
      typing = typing + key;
    }
    if (key == '1') {
      start = true;
      reset = true;
      guess = false;
      set_word = "";
      final_word = "";
      total_lose = 9;
      word_length =0;
      myPort.write(3);
      typing = "";
    }
  }

  else if(word_length == 0){
    if (key == '1') {
      start = true;
      reset = true;
      guess = false;
      set_word = "";
      final_word = "";
      total_lose = 9;
      myPort.write(3);
      typing = "";
    }
  }
  //end of guess
}//End of key pressed

void losing() {//DONT FORGET YOU'RE COUNTING DOWN!
  strokeWeight(2);
  stroke(0, 0, 0);
  switch(total_lose) {
  case 0: 
    textFont(font, 30);
    fill(255, 0, 0);
    text("You have lost!\n Press '1' to reset the game", 200, 350);
    myPort.write(2);
    break;
  case 1:
    strokeWeight(1);
    line(145, 130, 155, 135);
    line(155, 130, 145, 135);
    break;
  case 2:
    strokeWeight(1);
    line(155, 115, 160, 120);
    line(160, 115, 155, 120);
    break;
  case 3:
    strokeWeight(1);
    line(140, 115, 145, 120);
    line(145, 115, 140, 120);
    break;
  case 4:
    line( pos_X+20*size, pos_Y+70*size, pos_X+40*size, pos_Y+130*size) ; // right leg 
    break;
  case 5:
    line( pos_X+20*size, pos_Y+70*size, pos_X, pos_Y+130*size) ; //Left leg
    break;
  case 6:     
    line( pos_X+20*size, pos_Y+30*size, pos_X+40*size, pos_Y+70*size) ; // right arm 
    break;
  case 7:
    line( pos_X+20*size, pos_Y+30*size, pos_X, pos_Y+70*size) ; // left arm
    break;
  case 8: 
    line(pos_X+20*size, pos_Y+(16.4*size), pos_X+20*size, pos_Y+70*size); // Body
    break;
  case 9: 
    fill(255, 0, 100); // head fill
    ellipse(pos_X+20*size, pos_Y, 40*size, 35*size) ; // head
    break;
  }//end of switch :P
}

And here is the letter class:

class Letters{
  char [] to_char;
  PFont text;

  //Letter CONSTRUCTOR: Takes in the string
 Letters(String word){
   to_char = word.toCharArray();
   text = loadFont("Baskerville-48.vlw");
 }//End of constructor

void draw_lines(){
     int x = 40;
   for (int i=0;i<to_char.length;i++) {
    stroke(0);
    strokeWeight(2);
    line(x,500,x+50,500);
    x+=75;
   }
}
boolean check_letter(char example){
  for (int i = 0; i<to_char.length;i++){ //scans letters of the actual word
    if(example == to_char[i]){
      return true;
    }

  }
  return false;
}

void draw_letter(char example){
   int x = 50;
   if (word_right) {
   for (int i=0;i<to_char.length;i++) {
    // System.out.println(example);
    // System.out.println(to_char[i]);
     if(example == to_char[i]){
       //System.out.println("hahahahah");
        fill(184,92,245);
     textFont(text, 42);
     text(example, x, 490);
      word_length--;
      System.out.println(word_length);
   }
    x+=75;

   }
   word_right = false;
   }

 // if(example)

}//end 

}//letters class

 Here is Arduino code:

int input2=0;
void setup() {
  Serial.begin(9600);     
  pinMode(7, OUTPUT); //WIN
  pinMode(3, OUTPUT); //LOSE
}

void loop()
{

  if (Serial.available() > 0){
     Serial.println("input1");
     int input = Serial.read(); 
     Serial.println("input");
     //this waits for byte from Processing
     if(input == 1){
       input2=1;
       }
     else if(input == 2){
       input2=2;
     }
     else if(input == 3){
       input2=3;
     }
  }//Actual variable
  if(input2 == 1){
       digitalWrite(7, HIGH);
       Serial.println("i");
       }
     else if(input2 == 2){
       digitalWrite(3, HIGH);
     }
     else if(input2 == 3){
       digitalWrite(7, LOW);
       digitalWrite(3, LOW);
    }
}

Don’t Let Go of Nirvana

This week, I used the force sensor to link the Arduino with my enlightenment scene in Processing. Basically, when the user holds onto the force sensor, Nirvana stays on the screen. However, if the user becomes distracted and let’s go, the screen reverts to the image of Buddha trying to meditate. I like this version better because it allows the user to physically engage with the quest for Nirvana.  Continue reading Don’t Let Go of Nirvana

Piggy Bubbles (Popped by hand, wow, so cool, I’m amazing!)

So I had a busy week so I just tried to incorporate telecommunications into last weeks project. So I just used a pressure sensor and when you press the sensor it’s supposed to pop one bubble. However, I cannot get it to pop only one bubble and I keep trying to fix it but it doesn’t end up working. So in reality it’s supposed to only pop one bubble and only play that bubbles sound. I also feel that my code could be made simpler and had I had the time I’m sure I would have figured it out but like I said I was busy but whatever.

Here's a screen cap of the program running.
Here’s a screen cap of the program running.
import processing.serial.*;
Serial myPort;
int shouldPop;
float tempNumNo;
int tempNum;
boolean letItPop = false;
import ddf.minim.*;
Minim m;
//AudioPlayer s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;
AudioPlayer[] sounds;

//background photo
PImage photo;

//creates bubble array
Bubble[] bubbles = new Bubble[10]; 

void setup() {

  printArray(Serial.list());
  String portName = Serial.list()[0];
  myPort = new Serial(this, "COM3", 9600);
  myPort.readStringUntil('\n');  

  size(510, 418);
  for (int i=0; i < bubbles.length; i++) {
    bubbles[i] = new Bubble();
  }

  m = new Minim(this);
  sounds = new AudioPlayer[10];
  sounds[0] = m.loadFile("pop1.wav");
  sounds[1] = m.loadFile("moo.wav");
  sounds[2] = m.loadFile("meow.wav");
  sounds[3] = m.loadFile("fart.wav");
  sounds[4] = m.loadFile("elephant.wav");
  sounds[5] = m.loadFile("pop2.mp3");
  sounds[6] = m.loadFile("splash.wav");
  sounds[7] = m.loadFile("pig.wav");
  sounds[8] = m.loadFile("rubberduck.wav");
  sounds[9] = m.loadFile("belch.wav");

  photo = loadImage("http://www.kimballstock.com/pix/PIG/01/PIG-01-RK0133-01P.JPG");
}

void draw() {
  smooth();
  background(photo);
  for (int i=0; i < bubbles.length; i++) {
    bubbles[i].everything();
  }

  tempNumNo = random(0, 10);
  int tempNumAlmost = int (tempNumNo);
  popPop(tempNumAlmost);
}  

void serialEvent(Serial _port) { 
  if (myPort == null) return;
  String input = myPort.readStringUntil('\n');
  if (input != null) {  
    input = input.trim();  
    shouldPop = int(input);   
    println(shouldPop);
  }
}

void popPop(int tempNum) {
  if (shouldPop > 0 && letItPop == false) {
    letItPop = true;

    if (letItPop == true) {
      float soundTemp = random(0, 9);
      int soundNum = int(soundTemp);
      sounds[tempNum].play();
      sounds[tempNum].rewind();
      bubbles[tempNum].bubbleChange();
      letItPop = false;
    }
  }
}

 

KILLING THE MALAYSIAN PRIME MINISTER != GOOD

You’re Derek Zoolander and you’ve got some decisions to make.

//the port stuff
import processing.serial.*;
Serial myPort;
//int new_x;

//audio
import ddf.minim.*;
AudioPlayer player;
Minim minim;

//declare variables
PImage bgdimg;
PImage mugatu;
PImage primeMini;
float circleA = 100;
float circleB = 50;
float speedA = 5;
float speedB = 2;
int new_x;
int xpos;
int points = 0;
int miss = 0;

void setup() {
  size(595, 325);
  bgdimg = loadImage("magnum.jpg");
  mugatu = loadImage("mugatu.png");
  primeMini = loadImage("prime_minister.jpg");
  smooth();

  //arduino stuff
 printArray(Serial.list());
 String portName = Serial.list()[13];
 myPort = new Serial(this, "/dev/tty.usbmodemfd131", 9600);
myPort.readStringUntil('\n');  //clean out anything already in the buffer
   new_x = 0;

//initialize minim
  minim = new Minim(this);
  player = minim.loadFile("Relax.mp3", 2048);
  player.play();
}

void draw() {
  float paddle = 1000 / (points + 10);
  background (bgdimg);
  if (circleA < 0 || circleA > width)
    speedA = -speedA;
  if (circleB > height) {
    speedB = -speedB;
    float distance = abs(mouseX - circleA);
    if (distance < paddle)
      points += 1;
    else miss += 1;
  }
  else
    speedB += 1; 

  circleA = circleA + speedA;
  circleB = circleB + speedB;

  rect(xpos-paddle, height-10, 100, 125);
  fill(225,10,50);
  image(mugatu, circleA, circleB);

   if(miss > 5 && miss < 15){
    textSize(18);
    text("KILL THE MALAYSIAN PRIME MINISTER!", width/4, height/2);
   }
   if (miss > 20){
     textSize(18);
     fill (0);
     text("YOU'VE LOST", width/2, height/2);
     background(primeMini);
   }
}

void serialEvent(Serial _port) { //this is a callback function
if(myPort == null)return;
    String input = myPort.readStringUntil('\n');
    if (input != null) {  //if a '\n' character has in fact now arrived
      input = input.trim();  //Take off the '\n' character
    new_x = (int)map(xpos,50,378,0,325);
      xpos = int(input);  //Turn it into number 
      println(xpos);
    }}

 

Ouch!

I think this week’s assignment adequately embodies the Of Mice and Men quote: “best laid plans o’mice and men” often go awry. I had a much better idea but it really fell apart. So now I’m saving that one for a rainy day presenting a lamer substitute for this week’s assignment.

Basically, the player of the game is a (supposed to be) constantly-moving, never-tiring person who runs around the screen. The potentiometer input is supposed to dictate the angle of movement and the goal of the game is to just run around the screen, avoiding the 5 rectangle obstacles  and the edges.

The potentiometer input manages to be processed by my Processing program, but there are some other problems that I wasn’t able to fix in time.

Here’s my main code: 

import processing.serial.*;
Serial myPort;

boolean main_game;

int reading;
float angle;

Person self;
PFont wound;
int wound_count;

PShape starry;

Obstacles [] rectangle = new Obstacles[5];
ArrayList<Star>counting_star;

void setup() {
  size(800, 750, P2D);
  self = new Person(400, 700, 1);
  wound = loadFont("AmericanTypewriter-CondensedBold-32.vlw");
  wound_count = 0;
  counting_star = new ArrayList<Star>();
  main_game = true;

  //Arduino --> Processing:
  println(Serial.list());
  String portName = Serial.list()[0];
  myPort = new Serial(this, "/dev/tty.usbmodem1411", 9600);
  myPort.readStringUntil('\n');

  //For creating a star: 
  PShape starry = createShape();
  starry.beginShape();
  // You can set fill and stroke
  starry.fill(255, 255, 0);
  starry.stroke(255, 255, 0);
  // Here, we are hardcoding a series of vertices
  starry.vertex(0, -50);
  starry.vertex(14, -20);
  starry.vertex(47, -15);
  starry.vertex(23, 7);
  starry.vertex(29, 40);
  starry.vertex(0, 25);
  starry.vertex(-29, 40);
  starry.vertex(-23, 7);
  starry.vertex(-47, -15);
  starry.vertex(-14, -20);
  starry.endShape(CLOSE);

  for (int i=0;i<rectangle.length;i++) {
    rectangle[i] = new Obstacles((int)random(0, width-100), (int)random(0, height-100));
  }
  for (int i = 0; i < 0; i++) {
    counting_star.add(new Star(starry));
  }
} // End of SETUP

void draw() {
  if (main_game) {
    background (0, 145, 145);

    //Obstacles: 
    for (int i=0;i<rectangle.length;i++) {
      rectangle[i].drawRect();
    }
    //Star:
    for (int i = 0; i < counting_star.size(); i++) {
      counting_star.get(i).drawStar();
    }

    //Person: 
    self.changeDirectionAngle();
    self.set_theta(angle);
    self.movePerson();
    //self.rotate_person();

    if (self.checkBoundaries()) {
      self.changeColor();
      //self.set_random_move();
      self.bounce();
      textFont(wound, 48);
      text("Ouch!", 350, 50);
      wound_count++;
      counting_star.add(new Star(starry));
    }
   // for (int i=0;i<rectangle.length; i++) {
      if (self.check_Collision(rectangle[i])) {
        wound_count++;
        counting_star.add(new Star(starry));
      }
   // }

  }
  if (wound_count==6) {
    main game = false;
    background(255, 0, 0);
    textFont(wound, 48);
    text("You've died!", 350, 50);
}

} // End of DRAW

void serialEvent(Serial _port) { //this is a callback function
  if (myPort == null) return;  //this is a hack to cover a bug where the port does not get set up in time.  
  //this says if the port is not set up yet, bail (for now.) 
  String input = myPort.readStringUntil('\n');
  if (input != null) {  //if a '\n' character has in fact now arrived
    input = input.trim();  //Take off the '\n' character
    reading = int(input);  //Turn it into number
    System.out.println("Here is regular number"+reading);
    angle = map(reading, 0, 1000, 0, 360);
    println("Here is angle"+angle);
  }
}

Obstacles class: 

class Obstacles {
int x;
int y;

int r = (int)random(0,255);
int b =(int)random(0,255);
int g = (int)random(0,255);

int obstacle_width = (int)(random(50,150));
int obstacle_height = (int)(random(50,150));

//Constructor
Obstacles(int _x, int _y){
  x = _x;
  y = _y;
}

void drawRect(){
  stroke(r, b, g);
  fill(r, b, g);
  rect(x, y, obstacle_width, obstacle_height);
}

int get_X_Position(){
return x;
}

int get_Y_Position(){
return Y;
}

int get_Width(){
return obstacle_width;
}

int get_Height(){
return obstacle_height;
}

//void check_Collision(){

}

 

 

Person class:

class Person{
int pos_X;
int pos_Y;

int person_width = 0;
int person_height =0;

float size;

//Coloring head:
int r = 255;
int b = 255;
int g = 255;

//MOVEMENT: 
float velocity_X =3;
float velocity_Y =3;
float theta; //potentiometer input

//Constructor
Person(int x, int y, float _size){
  pos_X = x;
  pos_Y = y;
  size = _size;
}

void drawPerson() {
line( pos_X+20*size, pos_Y, pos_X+20*size, pos_Y+70*size) ; // Body
line( pos_X+20*size, pos_Y+70*size, pos_X, pos_Y+130*size) ; //Left leg
line( pos_X+20*size, pos_Y+70*size, pos_X+40*size, pos_Y+130*size) ; // right leg 
line( pos_X+20*size, pos_Y+30*size, pos_X, pos_Y+70*size) ; // left arm
line( pos_X+20*size, pos_Y+30*size, pos_X+40*size, pos_Y+70*size) ; // right arm 
fill(r,b,g) ; // grey for head fill
ellipse(pos_X+20*size, pos_Y, 40*size, 35*size) ; // head
person_width = (int)(40*size);
person_height = (int)((65*size) + (65*size) + (35*size));
}

void movePerson(){
  pos_X += velocity_X;
  pos_Y += velocity_Y;
  this.drawPerson();
}

void changeColor(){
  r = (int)random(0, 255);
  b = (int)random(0, 255);
  g = (int)random (0, 255);
}

boolean checkBoundaries() {
    boolean out_of_bounds = false;
    //Out of screen (horizontal)
    if (pos_X+(40*size) > width || pos_X < 0) {
      out_of_bounds = true;
    }
    //Out of screen (vertical)
    if (pos_Y + person_height >height ||pos_Y <0){
      out_of_bounds = true;
    }
    return out_of_bounds;
  }

void bounce(){
  if (pos_X + person_width >width || pos_X < 0) {
      velocity_X *= -1;
    }
    //Out of screen (vertical)
    if (pos_Y + person_height > height ||pos_Y <0 ) {
      velocity_Y *= -1;
    }
}

void set_random_move(){
  velocity_X = (int)random(1,5);
  velocity_Y = (int)random(1,5);
}

void set_theta(float _theta){
  theta = radians(_theta);
  System.out.println("here is theta"+theta);
}

void changeDirectionAngle() {
    //"Rotate" the face's velocities with given angle:
    velocity_X = cos(theta);
    velocity_Y = sin(theta);
  }

//COLLISION
  boolean check_Collision(Obstacles temporary) {
    boolean signal = false;
    int temp_x = temporary.get_X_Position();
    int temp_y = temporary.get_Y_Position();
    int temp_height = temporary.get_Height();
    int temp_width = temporary.get_Width();

    if (pos_X < (temp_x + temp_width) && (x+person_width) > (temp_x )) {   
      if (pos_Y < (temp_y + temp_height) && (y+person_height) > (temp_y )) {
        signal = true;
      }
      else {
        signal = false;
      }
    }
    return signal;
  }

/* 
void rotate_person(){
  pushMatrix();
  rotate(theta);
  this.movePerson();
  popMatrix();
}

*/  

}//end of person class

Star class:

class Star{
PShape star;
int x= (int)random(0,width);
int y = (int)random(0, height);

Star( PShape create_star){

  star = create_star;
}

void drawStar(){
  shape(star, x,y);
}

}

 

 

So the problems I’m having:

– The bounce function is ignored somehow

– The potentiometer angle isn’t processed completely as I hoped and the movement is jerky. Sometimes the person doesn’t move at all (which may be due to a mathematical computation but I’m very bewildered by this).

Back to Chasing 1D… (It works now!)

tumblr_mws0yzolrR1rx97q3o2_250

For this week’s assignment I wanted to have the arduino communicate with the crazy fan who is chasing One Direction. I changed the code to make it so that the crazy fan would not be moved by the mouse, instead she would move around randomly just like the members. My goal is to catch/freeze whichever member the fan is over by setting off the sensor. This gets into knowing when an object is on top of another which I am having trouble with. I am going to keep working with it though to try to figure something out.

 

 

UPDATE:

I fixed the code! Now they all freeze separately when the sensor is pressed!

 

import ddf.minim.*;
import processing.serial.*;

Minim minim;
AudioPlayer player;
Serial myPort;

Member [] oneDirection = new Member [5];
float xPos, yPos;
float xSpeed, ySpeed;
PImage harry, liam, louis, niall, zayn; 
PImage [] boy = {harry, liam, louis, niall, zayn };
PImage crazyFan;
boolean overBoy;
int catchCounter = 0;
float fanXpos;
float fanYpos;
float fanXspeed;
float fanYSpeed;
float sensor;

void setup(){

  //--Taken from Telecommunication page on class website--
  // List all the available serial ports:
  println(Serial.list());
  // Print out the list and look for port your Microcontroller is on
  // Finding the Arduino is not easy because they have weird names on the mac and on the pc they look like COM1.
  // Ardnino is almost always the first one so you can usually use Serial.list()[0].
  String portName = Serial.list()[0];
  myPort = new Serial(this, "/dev/cu.usbmodem1411", 9600);
  myPort.readStringUntil('\n');  //clean out anything already in the buffer
  //-----

  size(800,600); 

  crazyFan = loadImage("crazyFan.png");
  fanXpos = random(0,width-75);
  fanYpos = random(0,height-150);
  fanXspeed = random(3.0, 5.0);
  fanYSpeed = random(3.0, 5.0);

  harry = loadImage("Harry_Styles.png");
  liam = loadImage("Liam_Payne.png");
  louis = loadImage("Louis_Tomlinson.png");
  niall = loadImage("Niall_Horan.png");
  zayn = loadImage("Zayn_Malik.png");

  oneDirection[0] = new Member(harry, xPos, yPos);
  oneDirection[1] = new Member(liam, xPos, yPos);
  oneDirection[2] = new Member(louis, xPos, yPos);
  oneDirection[3] = new Member(niall, xPos, yPos);
  oneDirection[4] = new Member(zayn, xPos, yPos);
  minim = new Minim(this);
  player = minim.loadFile("WMYB.mp3");
  player.play();
}

void draw(){ 

  background(255);

  for (int i = 0; i < 5; i ++){
      oneDirection[i].positionChange();
      oneDirection[i].drawLad();

  }
  crazyFan();

}  

void crazyFan(){
  crazyFan.resize(0,150);

  if(fanXpos >= width - 75|| fanXpos <0){
    fanXspeed *= -1;
  }
  if(fanYpos >= height - 150 || fanYpos < 0){
    fanYSpeed *= -1;
  } 
    fanXpos += fanXspeed;
    fanYpos += fanYSpeed; 
  image(crazyFan, fanXpos, fanYpos);
}

//--Backbone of method taken from class website--
void serialEvent(Serial _port) { //this is a callback function
  if (myPort == null) return;  //this is a hack to cover a bug where the port does not get set up in time.  
  //this says if the port is not set up yet, bail (for now.) 
  String input = myPort.readStringUntil('\n');
  if (input != null) {  //if a '\n' character has in fact now arrived
    input = input.trim();  //Take off the '\n' character
    sensor = int(input);  //Turn it into number 
   // println(sensor);
    if (sensor>=400 && sensor<=600){  
      for (int i = 0; i < 5; i ++){
        if (dist(oneDirection[i].xPos, oneDirection[i].yPos, fanXpos, fanYpos)<= 75){
          overBoy=true;
          catchCounter++;
          oneDirection[i].freeze();
        }//dist
      }

  }
  }
}

class Member{
    float xPos = random(0,width-75);
    float yPos = random(0,height-150);
    float xSpeed = random (3.0, 5.0);
    float ySpeed = random (3.0, 5.0);
    PImage [] boy = {harry, liam, louis, niall, zayn };
    PImage lad, _lad;

    //Constructor
    Member(PImage lad, float xPos, float yPos){
       _lad = lad;
    }//constructor  

    void positionChange(){
      if(xPos >= width - 75|| xPos <0){
        xSpeed *= -1;
      }
      if(yPos >= height - 150 || yPos<0){
        ySpeed *= -1;
      } 

      xPos += xSpeed;
      yPos += ySpeed;
    }

    void drawLad(){
      xPos += xSpeed;
      yPos += ySpeed; 
      _lad.resize(0,150);
      image(_lad, xPos, yPos);
    }//draw the boys

    void freeze(){
     if (overBoy){
       if (catchCounter%2==0){
          xSpeed = 0;
          ySpeed = 0; 
          System.out.println(catchCounter);
       }
      }//if over boy

    }//method

    }//member

 

Almost there

I’m close to something happening. Unfortunately my photo-sensor broke, so as a proxy I’ve been using a button as an input.

Every time the button is pushed, I want the expanding circle to revert back to its original size. Check da code, help me!

This Arduino code prints “1” to the serial when the button is pressed and “0” otherwise:

void setup(){
  Serial.begin(9600);
}

void loop(){
  //int brightness = analogRead(A0);
  int pressed = digitalRead(11);
  //Serial.print(brightness);
  //Serial.print(", ");
  Serial.print(pressed);
  Serial.print('\n');
}

Processing main file:

import processing.serial.*;
Ripple a;

Serial myPort;

void setup() {
  // List all the available serial ports:
  println(Serial.list());
  // Print out the list and look for port your Microcontroller is on
  // Finding the Arduino is not easy because they have weird names like "/dev/tty.usbmodem1421" or COM1
  // Ardnino is almost always the first one so you can usually use Serial.list()[0].
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  //myPort.readStringUntil('\n');  //clean out anything already in the buffer
  size(600,600);
  a = new Ripple();
}

void draw() {
  background(255);
  a.ripple();
  a.colorshift();
  a.buttonReset();
}

Processing class file:

class Ripple {
float col;
float radius;
//float brightness;
float button;

Ripple () {
 radius = 50; 
 col = 80;
}

  void ripple () {
    noStroke();
    fill(col);
    ellipse(.5*width, .5*height, radius, radius);
    radius = radius + 10; 
  }

  void colorshift () {
    if (col == 40) {
      col = 80;
    }
    else if (col == 80) {
      col = 40;
  }
  }

  void buttonReset () {
    if (myPort == null) return;
    //brightness = myPort[0];
    String input = myPort.readStringUntil('\n');
    if (input != null) {
      input = input.trim();
      button = int(input); 
    } 
    //button = myPort.read();
    print(button);
    if (button == 1) {
      radius = 50;
}
}
}

Final Maze Game with some Arduino connection

youcandoit copy

…maybe

Missing the telecommunications class was not smart on my part. Although I was in Boston, so it was a bit out of my control- I really wish I had been able to learn how to fully bridge the Arduino/Processing gap. I tried to teach myself how to exactly to do that this weekend- but it just wasn’t the same. I got each side working on their own…but the actual connection part (mainly the downloading part) I just don’t understand. Along with that, the Arduino lingo is a bit confusing too.

So…I decided to just write out the code (on both ends) of how I *think* it would work. I do understand what is going on. And I do wish I could make use of the understanding- because this section was kind of the culmination of the everything we learned this semester. For the time being, what I have figured out here will do.

Basically, I took my final maze game and replaced the keyPressed function (“q” as the restart button) and replaced it with a force sensor. Pressing the force sensor would clear the map of the “YOU LOSE” message.

Here is the code from both sides:

Processing:

import processing.serial.*;
Serial myPort;
int force;

PImage  starthere;
PImage finishhere;
PImage title;
PImage rules;
PImage youlose;
PImage youlose27;
PImage youwin;
PImage pressq;

PImage almostthere;
PImage youcandoit;
PImage greatstart;
PImage keepitup;
PImage woo;

PImage greatstartcover;
PImage youcandoitcover;
PImage keepitupcover;
PImage woocover;
PImage almosttherecover;

int gap = 20;

void setup() {
  size (800, 400);
  background (204, 204, 204);
  printArray(Serial.list());
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  myPort.readStringUntil('\n');
  starthere = loadImage("starhere.png");
  finishhere = loadImage("finishhere.png");
  title = loadImage("cursorcraze.png");
  rules = loadImage("mazerules2.0.png");
  youlose = loadImage("youlose2.png");
  youlose27 = loadImage("youlose27.png");
  youwin = loadImage("youwin.png");
  pressq = loadImage("pressQ.png");
  almostthere = loadImage("almostthere.png");
  youcandoit = loadImage("youcandoit.png");
  greatstart = loadImage("greatstart.png");
  keepitup = loadImage("keepitup.png");
  woo = loadImage("woo.png");
  greatstartcover = loadImage("greatstartcover.png");
  youcandoitcover= loadImage("youcandoitcover.png");
  keepitupcover = loadImage("keepitupcover.png");
  woocover = loadImage("woocover.png");
  almosttherecover = loadImage("almosttherecover.png");

  stroke (0);
  fill (0);
  //top and bottom
  //1
  rect (0, 0, gap, 160); 
  rect (0, 200, gap, 200); 
  //2
  rect (20, 0, gap, 160); 
  rect (20, 200, gap, 220); 
  //3
  rect (40, 0, gap, 140); 
  rect (40, 180, gap, 240); 
  //4
  rect (60, 0, gap, 120); 
  rect (60, 160, gap, 260); 
  //5
  rect (80, 0, gap, 100); 
  rect (80, 140, gap, 280); 
  //6
  rect (100, 0, gap, 100); 
  rect (100, 140, gap, 280); 
  //7
  rect (120, 0, gap, 120); 
  rect (120, 160, gap, 260); 
  //8
  rect (140, 0, gap, 140); 
  rect (140, 180, gap, 280); 
  //9
  rect (160, 0, gap, 160); 
  rect (160, 200, gap, 200); 
  //10
  rect (180, 0, gap, 180); 
  rect (180, 220, gap, 200); 
  //11
  rect (200, 0, gap, 200); 
  rect (200, 240, gap, 200); 
  //12
  rect (220, 0, gap, 220); 
  rect (220, 260, gap, 200);
  //13
  rect (240, 0, gap, 240); 
  rect (240, 280, gap, 200); 
  //14
  rect (260, 0, gap, 260); 
  rect (260, 300, gap, 200);
  //15
  rect (280, 0, gap, 280); 
  rect (280, 320, gap, 200); 
  //16
  rect (300, 0, gap, 300);
  rect (300, 340, gap, 200); 
  //17
  rect (320, 0, gap, 320); 
  rect (320, 360, gap, 200);
  //18
  rect (340, 0, gap, 320);
  rect (340, 360, gap, 200); 
  //19
  rect (360, 0, gap, 300);
  rect (360, 340, gap, 200);
  //20
  rect (380, 0, gap, 280);
  rect (380, 320, gap, 200); 
  //21
  rect (400, 0, gap, 260);
  rect (400, 300, gap, 200); 
  //22 
  rect (420, 0, gap, 240); 
  rect (420, 280, gap, 200);
  //23
  rect (440, 0, gap, 220); 
  rect (440, 260, gap, 200); 
  //24
  rect (460, 0, gap, 220); 
  rect (460, 260, gap, 200);
  //25
  rect (480, 0, gap, 240);
  rect (480, 280, gap, 200); 
  //26
  rect (500, 0, gap, 260);
  rect (500, 300, gap, 200); 
  //27
  rect (520, 0, gap, 280);
  rect (520, 320, gap, 200); 
  //28
  rect (540, 0, gap, 280);
  rect (540, 320, gap, 200);  
  //29
  rect (560, 0, gap, 260);
  rect (560, 300, gap, 200); 
  //30 
  rect (580, 0, gap, 240); 
  rect (580, 280, gap, 200); 
  //31
  rect (600, 0, gap, 220); 
  rect (600, 260, gap, 200); 
  //32
  rect (620, 0, gap, 200);
  rect (620, 240, gap, 220); 
  //33
  rect (640, 0, gap, 180);
  rect (640, 220, gap, 240); 
  //34
  rect (660, 0, gap, 160); 
  rect (660, 200, gap, 260);
  //35
  rect (680, 0, gap, 140);
  rect (680, 180, gap, 280); 
  //36
  rect (700, 0, gap, 120);
  rect (700, 160, gap, 300); 
  //37
  rect (720, 0, gap, 120);
  rect (720, 160, gap, 320);  
  //38
  rect (740, 0, gap, 140); 
  rect (740, 180, gap, 300);
  //39
  rect (760, 0, gap, 160);
  rect (760, 200, gap, 320); 
  //40
  rect (780, 0, gap, 160); 
  rect (780, 200, gap, 320); 

  image(starthere, 0, 200);
  image(finishhere, 700, 200);
  image(title, 210, 0);
  image(rules, 220, 70);
  image(pressq, 680, 5);
}

void draw() {

  if ((mouseX > 0 && mouseX < 20) && (mouseY < 160  || mouseY > 200)) {
    background (youlose);
  }

  else if ((mouseX > 20 && mouseX < 40 ) && (mouseY < 160 || mouseY > 200)) {
    background (youlose);
  }

  else if ((mouseX > 40 && mouseX < 60) && (mouseY < 140 || mouseY > 180)) {
    background (youlose);
  }

  else if ((mouseX > 60 && mouseX < 80) && (mouseY < 120 || mouseY > 160)) {
    background (youlose);
  }

  else if ((mouseX > 80 && mouseX < 100) && (mouseY < 100 || mouseY > 140)) {
    background (youlose);
  }

  else if ((mouseX > 80 && mouseX < 100) && (mouseY > 100 || mouseY < 140)) { 
    image(greatstart, 50, 75);
  }

  else if ((mouseX > 100 && mouseX < 120) && (mouseY < 100 || mouseY > 140)) {
    background (youlose);
  }

  else if ((mouseX > 120 && mouseX < 140) && (mouseY < 120 || mouseY > 160)) {
    background (youlose);
  }

  else if ((mouseX > 120 && mouseX < 140) && (mouseY > 120 || mouseY < 160)) {
    image (greatstartcover, 50, 75);
  }

  else if ((mouseX > 140 && mouseX < 160) && (mouseY < 140 || mouseY > 180)) {
    background (youlose);
  }

  else if ((mouseX > 160 && mouseX < 180) && (mouseY < 160 || mouseY > 200)) {
    background (youlose);
  }
  //10
  else if ((mouseX > 180 && mouseX < 200) && (mouseY < 180 || mouseY > 220)) {
    background (youlose);
  }

  else if ((mouseX > 200 && mouseX < 220) && (mouseY < 200 || mouseY > 240)) {
    background (youlose);
  }

  else if ((mouseX > 220 && mouseX < 240) && (mouseY < 220 || mouseY > 260)) {
    background (youlose);
  }

  else if ((mouseX > 240 && mouseX < 260) && (mouseY < 240 || mouseY > 280)) {
    background (youlose);
  }

  else if ((mouseX > 260 && mouseX < 280) && (mouseY < 260 || mouseY > 300)) {
    background (youlose);
  }

  else if ((mouseX > 280 && mouseX < 300) && (mouseY < 280 || mouseY > 320)) {
    background (youlose);
  }

  else if ((mouseX > 300 && mouseX < 320) && (mouseY < 300 || mouseY > 340)) {
    background (youlose);
  }

  else if ((mouseX > 320 && mouseX < 340) && (mouseY < 320 || mouseY > 360)) {
    background (youlose);
  }

  else if ((mouseX > 320 && mouseX < 340) && (mouseY > 320 || mouseY < 360)) {
    image(youcandoit, 310, 360) ;
  }

  else if ((mouseX > 340 && mouseX < 360) && (mouseY < 320 || mouseY > 360)) {
    background (youlose);
  }

  else if ((mouseX > 360 && mouseX < 380) && (mouseY < 300 || mouseY > 340)) {
    background (youlose);
  }

  else if ((mouseX > 360 && mouseX < 380) && (mouseY > 300 || mouseY < 340)) {
    image (youcandoitcover, 310, 360);
  }

  //20
  else if ((mouseX > 380 && mouseX < 400) && (mouseY < 280 || mouseY > 320)) {
    background (youlose);
  }

  else if ((mouseX > 400 && mouseX < 420) && (mouseY < 260 || mouseY > 300)) {
    background (youlose);
  }

  else if ((mouseX > 420 && mouseX < 440) && (mouseY < 240 || mouseY > 280)) {
    background (youlose);
  }

  else if ((mouseX > 440 && mouseX < 460) && (mouseY < 220 || mouseY > 260)) {
    background (youlose);
  }

  else if ((mouseX > 440 && mouseX < 460) && (mouseY > 220 || mouseY < 260)) {
    image (keepitup, 430, 180);
  }

  else if ((mouseX > 460 && mouseX < 480) && (mouseY < 220 || mouseY > 260)) {
    background (youlose);
  }

  else if ((mouseX > 480 && mouseX < 500) && (mouseY < 240 || mouseY > 280)) {
    background (youlose);
  }

  else if ((mouseX > 480 && mouseX < 500) && (mouseY > 240 || mouseY < 280)) {
    image (keepitupcover, 430, 180);
  }

  else if ((mouseX > 500 && mouseX < 520) && (mouseY < 260 || mouseY > 300)) {
    background (youlose);
  }
  //27
  else if ((mouseX > 520 && mouseX < 540) && (mouseY < 280 || mouseY > 320)) {
    background (youlose27);
  }

  else if ((mouseX > 520 && mouseX < 540) && (mouseY > 280 || mouseY < 320)) {
    image (woo, 520, 320);
  }

  else if ((mouseX > 540 && mouseX < 560) && (mouseY < 280 || mouseY > 320)) {
    background (youlose);
  }

  else if ((mouseX > 560 && mouseX < 580) && (mouseY < 260 || mouseY > 300)) {
    background (youlose);
  }

  else if ((mouseX > 560 && mouseX < 580) && (mouseY > 260 || mouseY < 300)) {
    image (woocover, 520, 320);
  }

  //30
  else if ((mouseX > 580 && mouseX < 600) && (mouseY < 240 || mouseY > 280)) {
    background (youlose);
  }

  else if ((mouseX > 600 && mouseX < 620) && (mouseY < 220 || mouseY > 260)) {
    background (youlose);
  }

  else if ((mouseX > 640 && mouseX < 660) && (mouseY < 180 || mouseY > 220)) {
    background (youlose);
  }

  else if ((mouseX > 660 && mouseX < 680) && (mouseY < 160 || mouseY > 200)) {
    background (youlose);
  }

  else if ((mouseX > 680 && mouseX < 700) && (mouseY < 140 || mouseY > 180)) {
    background (youlose);
  }

  else if ((mouseX > 700 && mouseX < 720) && (mouseY < 120 || mouseY > 160)) {
    background (youlose);
  }

  else if ((mouseX > 700 && mouseX < 720) && (mouseY > 120 || mouseY < 160)) {
    image(almostthere, 690, 85);
  }

  else if ((mouseX > 720 && mouseX < 740) && (mouseY < 120 || mouseY > 160)) {
    background (youlose);
  }

  else if ((mouseX > 740 && mouseX < 760) && (mouseY < 140 || mouseY > 180)) {
    background (youlose);
  }

  else if ((mouseX > 740 && mouseX < 760) && (mouseY > 140 || mouseY < 180)) {
    image (almosttherecover, 690, 85);
  }

  //final 2 boxes
  else if ((mouseX > 760 && mouseX < 780) && (mouseY < 160 || mouseY > 200)) {
    background (youlose);
  }
  //40
  else if ((mouseX > 780 && mouseX < 800) && (mouseY < 160 || mouseY > 200)) {
    background (youlose);
  }

  else if ((mouseX > 760 && mouseX < 780) && (mouseY > 160 || mouseY < 200)) {
    background (youwin);
  }

  else if ((mouseX > 780 && mouseX < 800) && (mouseY > 160 || mouseY < 200)) {
    background (youwin);
  }

  if (force>0){
      background (204, 204, 204);
      stroke (0);
      fill (0);
      //top and bottom
      //1
      rect (0, 0, gap, 160); 
      rect (0, 200, gap, 200); 
      //2
      rect (20, 0, gap, 160); 
      rect (20, 200, gap, 220); 
      //3
      rect (40, 0, gap, 140); 
      rect (40, 180, gap, 240); 
      //4
      rect (60, 0, gap, 120); 
      rect (60, 160, gap, 260); 
      //5
      rect (80, 0, gap, 100); 
      rect (80, 140, gap, 280); 
      //6
      rect (100, 0, gap, 100); 
      rect (100, 140, gap, 280); 
      //7
      rect (120, 0, gap, 120); 
      rect (120, 160, gap, 260); 
      //8
      rect (140, 0, gap, 140); 
      rect (140, 180, gap, 280); 
      //9
      rect (160, 0, gap, 160); 
      rect (160, 200, gap, 200); 
      //10
      rect (180, 0, gap, 180); 
      rect (180, 220, gap, 200); 
      //11
      rect (200, 0, gap, 200); 
      rect (200, 240, gap, 200); 
      //12
      rect (220, 0, gap, 220); 
      rect (220, 260, gap, 200);
      //13
      rect (240, 0, gap, 240); 
      rect (240, 280, gap, 200); 
      //14
      rect (260, 0, gap, 260); 
      rect (260, 300, gap, 200);
      //15
      rect (280, 0, gap, 280); 
      rect (280, 320, gap, 200); 
      //16
      rect (300, 0, gap, 300);
      rect (300, 340, gap, 200); 
      //17
      rect (320, 0, gap, 320); 
      rect (320, 360, gap, 200);
      //18
      rect (340, 0, gap, 320);
      rect (340, 360, gap, 200); 
      //19
      rect (360, 0, gap, 300);
      rect (360, 340, gap, 200);
      //20
      rect (380, 0, gap, 280);
      rect (380, 320, gap, 200); 
      //21
      rect (400, 0, gap, 260);
      rect (400, 300, gap, 200); 
      //22 
      rect (420, 0, gap, 240); 
      rect (420, 280, gap, 200);
      //23
      rect (440, 0, gap, 220); 
      rect (440, 260, gap, 200); 
      //24
      rect (460, 0, gap, 220); 
      rect (460, 260, gap, 200);
      //25
      rect (480, 0, gap, 240);
      rect (480, 280, gap, 200); 
      //26
      rect (500, 0, gap, 260);
      rect (500, 300, gap, 200); 
      //27
      rect (520, 0, gap, 280);
      rect (520, 320, gap, 200); 
      //28
      rect (540, 0, gap, 280);
      rect (540, 320, gap, 200);  
      //29
      rect (560, 0, gap, 260);
      rect (560, 300, gap, 200); 
      //30 
      rect (580, 0, gap, 240); 
      rect (580, 280, gap, 200); 
      //31
      rect (600, 0, gap, 220); 
      rect (600, 260, gap, 200); 
      //32
      rect (620, 0, gap, 200);
      rect (620, 240, gap, 220); 
      //33
      rect (640, 0, gap, 180);
      rect (640, 220, gap, 240); 
      //34
      rect (660, 0, gap, 160); 
      rect (660, 200, gap, 260);
      //35
      rect (680, 0, gap, 140);
      rect (680, 180, gap, 280); 
      //36
      rect (700, 0, gap, 120);
      rect (700, 160, gap, 300); 
      //37
      rect (720, 0, gap, 120);
      rect (720, 160, gap, 320);  
      //38
      rect (740, 0, gap, 140); 
      rect (740, 180, gap, 300);
      //39
      rect (760, 0, gap, 160);
      rect (760, 200, gap, 320); 
      //40
      rect (780, 0, gap, 160); 
      rect (780, 200, gap, 320); 

      image(starthere, 0, 200);
      image(finishhere, 700, 200);
      image(title, 210, 0);
      image(rules, 220, 70);
      image(pressq, 680, 5);
    }

}

void serialEvent(Serial _port) { 
  if (myPort == null)return;
  String input = myPort.readStringUntil('\n');
  if (input != null) {  
    input = input.trim();  
    force = int(input);  
  } 
}

and Arduino:

//copied from class notes
void setup(){
  Serial.begin(9600);
}

void loop(){
  int input = analogRead(0);
  Serial.print(input);  //notice I did not say println
  Serial.print('\n');  //instead I more explcitly send the linefeed character
}

I wish somehow that I could control the cursor with the force sensor- though first I’ll just have to figure out how exactly to fully connect the two.

 

 

minim(al) visualization

minim(al) visual

So I was playing around with the minim library, recording voices and wondering If that’s how I really sound. My original plan was to save a recording, and play it back using the FSR, but I could only get it to work sometimes with the FilePlayer.

Instead, here’s a pretty/cool visualization of any song that plays. Personally, the visualization reminds me of Mega Man/Transformers.

To add your own song, just put the song file into the same Processing folder, and replace “Friend.mp3” with the name of the song file. To play the song, the force applied to the FSR has to be higher than 150.

//importing minim and serial libraries
import ddf.minim.*;
import processing.serial.*;

//for to play music
Minim minim; 
AudioPlayer player; 

//for the arduino connection
Serial myPort;

//force applied to fsr
int force;

float y = 0;
float x = 0;

void setup() {
  size(600, 600);
  smooth();
  colorMode(RGB);

  //code from class
  printArray(Serial.list());
  String portName = Serial.list()[5];
  myPort = new Serial(this, portName, 9600);
  myPort.readStringUntil('\n'); 

  //start minim with this
  minim = new Minim (this);

  if (force > 150) //force applied to pressure sensor has to be higher than 150 
  {
    player = minim.loadFile ("Friend.mp3"); //replace this .mp3 file with the name of your .mp3 file
    player.play ( );
  }
}

//draws rectangles using matrix
void draw() {
  x+=0.02;
  y+=0.005;
  float n = noise(x);
  float t = noise(y);

  pushMatrix();
  stroke(70, 3, 11, 20);
  translate(310, -140);
  rotate(.8);
  fill (60, 170, 205, t*4);
  rect(t*(width)-50, n*height+10, 50*n, height/2*n);
  rect( n*height+10, t*(width)-50, height/2*n, 50*n);
  popMatrix();
}

//code from class for serial communication
void serialEvent(Serial _port) { //this is a callback function
  if (myPort == null) return;  //this is a hack to cover a bug where the port does not get set up in time.  
  //this says if the port is not set up yet, bail (for now.) 
  String input = myPort.readStringUntil('\n');
  if (input != null) {  //if a '\n' character has in fact now arrived
    input = input.trim();  //Take off the '\n' character
    force = int(input);  //Turn it into number 
    println(force);
  }
}

 

Helicopter

import processing.serial.*;

Serial myPort;
PImage bg;
PImage copter;
int xpos;
int fire;

void setup() {
  size(1200, 900);
  printArray(Serial.list());
  String portName = Serial.list()[5];
  myPort = new Serial(this, portName, 9600);
  myPort.write("A"); //send any old character
  myPort.readStringUntil('\n');  
  copter = loadImage("copter.png");
  bg = loadImage("sunny-sky.jpg");
}

void draw() {
  background(bg);
  image(copter, mouseX, mouseY);
}

void mouseDragged() {
  int  mpos = int(map(mouseX, 0, width, 0, 255)); 
  println(mpos);
  myPort.write(mpos);
}
#include <Servo.h>      // include the servo library

Servo servoMotor;       // creates an instance of the servo object to control a servo

void setup() {
  Serial.begin(9600);       // initialize serial communications
  servoMotor.attach(6);  // attaches the servo on pin 2 to the servo object
  pinMode(7,OUTPUT);
}

void loop()
{
  digitalWrite(7,LOW);
  if (Serial.available() > 0) {
    int input = Serial.read();  //this waits for byte from Processing
    int servoAngle = map(input,0, 255, 0, 179);
    // move the servo using the angle from the sensor:
    digitalWrite(7,HIGH);
    servoMotor.write(servoAngle);
  }
}

Screen Shot 2014-03-29 at 7.31.15 PM

 

Here be a video

Spider Crush

heaven

For the telecommunication between the Arduino and Processing I made a simple program about killing a spider, cause there happened to be one on the wall as I began this project.

bea arthur pizza
This has nothing to do with my project, but I found a huge gallery of Bea Arthur + pizza landscapes online last night…. many potential games guys

I used the photocell sensor to represent the spider, the more you crush it, the closer the spider gets to heaven. I guess I could have used a pressure sensor as well, but the photocell was already installed and we can just make a metaphorical leap to us draining the light out of the spider’s life before he gets to go to heaven.

SORRY about the music, but I honestly couldn’t help myself.

http://youtu.be/YTQ28v2wcF0

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

import processing.serial.*;

PImage heaven;
PImage spider;

Minim minim;
AudioPlayer sou; //variable name;

Serial myPort;
float ypos; //change int to float

void setup() {
  size(640, 480);
   minim = new Minim(this);
  sou = minim.loadFile("I Believe I Can Fly.mp3");
  sou.loop();
  heaven = loadImage ("heaven.jpg");
  spider = loadImage ("spider.png");
  // List all the available serial ports:
  printArray(Serial.list());
  // Print out the list and look for port your Microcontroller is on
  // Finding the Arduino is not easy because they have weird names on the mac and on the pc they look like COM1.
  // Ardnino is almost always the first one so you can usually use Serial.list()[0].
  String portName = Serial.list()[7];
  myPort = new Serial(this, portName, 9600);
  myPort.readStringUntil('\n');  //clean out anything already in the buffer
   size(640, 480);
}

void draw() {
  //background(255);
  image(heaven, 0, 0);
  image(spider, 220, ypos, 200, 200);//draw ellipse at x, variable y @
}

void serialEvent(Serial _port) { //this is a callback function
    if(myPort == null) return;
    String input = myPort.readStringUntil('\n');
    if (input != null) {  //if a '\n' character has in fact now arrived
      input = input.trim();  //Take off the '\n' character
      ypos = float(input);  //Turn it into number, changed int to float, why? cause nothing else worked
      println(ypos);
    }
}

 

Eat the Shapes Game

hello fellow earthlings

This week, I decided to continue working on the game I made during spring break. I added a force sensor through Arduino to replace the mousePressed() function, as well as some other small details for a more cohesive game.

The goal of this week’s game is to eat as many shapes as possible. To ‘eat’ an object, push the force sensor when your character (the black-outlined circle) is near a shape. A laser beam sound plays every time the force sensor is pressed.  The red number in the background shows how many shapes are currently on screen, and the large  blue number displays the number of shapes eaten. The smaller blue number in the center shows the number of shapes eaten in the previous round (kind of like a ‘high score’ record). If the red number reaches more than 25, the game is over. When a shape is eaten, one of three random sound bits plays (can you guess where I sampled from? http://www.youtube.com/watch?v=JtH68PJIQLE). Each shape comes on screen based on an independent timer. Every 10 seconds, a large black circle comes on screen. If you eat this shape, the game will automatically be over. Pressing ‘q’ resets the game.

I think I’m finally ready to label this game as ‘finished’. It’s pretty simple in gameplay, but I definitely used this project to explore how to make classes/objects, use the sound library, and connect Processing with Arduino. If I decide to come back and work on this game, I would definitely try to clean up the code so that there aren’t so many repetitive elements/the program will run faster.

goodbye fellow earthlings!

 

a better looking version of the game

 

Force Meter Set Up

//Main Body of Code
import processing.serial.*;
import ddf.minim.*;
Minim m;
AudioPlayer[] player;
Serial myPort;
int force;
Mouse theMouse;
ArrayList theSquares;
ArrayList theCircles;
ArrayList theTriangles;
ArrayList theDeath;
Timer theSquareTimer;
Timer theCircleTimer;
Timer theTriangleTimer;
Timer theDeathTimer;
int eatenShapes = 0;
int shapesOnScreen = 0;
int lastRound;
boolean gameOver = false;

void setup() {
  size (500, 500, P2D);
  printArray(Serial.list());
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
  myPort.readStringUntil('\n'); 
  m = new Minim(this);
  player = new AudioPlayer[4];
  player[0] = m.loadFile("Beep.wav");
  player[1] = m.loadFile("grimes_1.wav");
  player[2] = m.loadFile("grimes_2.wav");
  player[3] = m.loadFile("grimes_3.wav");
  theMouse = new Mouse();
  theSquares = new ArrayList();
  theCircles = new ArrayList();
  theTriangles = new ArrayList();
  theDeath = new ArrayList();
  theSquareTimer = new Timer (2000);
  theCircleTimer = new Timer (1000);
  theTriangleTimer = new Timer (1500);
  theDeathTimer = new Timer (10000);
  theSquareTimer.start();
  theCircleTimer.start();
  theTriangleTimer.start();
  theDeathTimer.start();
}

void draw() {
  background(255);
  grid();
  textAlign(CENTER);
  textSize(height/4);
  fill(0, 0, 255, 180);
  text(eatenShapes, 2*width/3, height/2);
  fill(255, 0, 0, 180);
  text(shapesOnScreen, width/3, height/2);
  textSize(height/7);
  fill (0, 0, 255, 180);
  text(lastRound, width/2, 2*height/3);

  if (theSquareTimer.timeOver()) {
    theSquares.add(new Square(40));
    shapesOnScreen ++;
    theSquareTimer.start();
  }
  if (theCircleTimer.timeOver()) {
    theCircles.add(new Circle(50));
    shapesOnScreen ++;
    theCircleTimer.start();
  }
  if (theTriangleTimer.timeOver()) {
    theTriangles.add(new Triangle(30));
    shapesOnScreen ++;
    theTriangleTimer.start();
  }
  if (theDeathTimer.timeOver()) {
    theDeath.add(new Death());
    theDeathTimer.start();
  }
  if (shapesOnScreen > 25 || gameOver == true) {
    background (255);
    textSize(50);
    textAlign(CENTER);
    text ("game over", width/2, height/2);
    theSquares.clear();
    theCircles.clear();
    theTriangles.clear();
    theDeath.clear();
    theMouse.disappear();
  }

if (force > 30) {
    for (int i = 0; i < theDeath.size(); i++) {
      Death d = (Death) theDeath.get(i);
      if (theMouse.intersectD(d)) {
        gameOver = true;
      }
    }
    player[0].play();
    player[0] = m.loadFile("Beep.wav");
    int sx = int((random(1, 4)));
    for (int j = 1; j < 4; j++) {
      for (int i = 0; i < theSquares.size(); i++) {
        Square q = (Square) theSquares.get(i);
        if (theMouse.intersectS(q)) {
          q.disappear();
          eatenShapes++;
          shapesOnScreen--;
          player[sx].play();
          player[0] = m.loadFile("grimes_1.wav");
          player[1] = m.loadFile("grimes_2.wav");
          player[2] = m.loadFile("grimes_3.wav");
        }
      }

      for (int i = 0; i < theCircles.size(); i++) {
        Circle c = (Circle) theCircles.get(i);
        if (theMouse.intersectC(c)) {
          c.disappear();
          eatenShapes++;
          shapesOnScreen--;
          player[sx].play();
          player[0] = m.loadFile("grimes_1.wav");
          player[1] = m.loadFile("grimes_2.wav");
          player[2] = m.loadFile("grimes_3.wav");
        }
      }
      for (int i = 0; i < theTriangles.size(); i++) {
        Triangle t = (Triangle) theTriangles.get(i);
        if (theMouse.intersectT(t)) {
          t.disappear();
          eatenShapes++;
          shapesOnScreen--;
          player[sx].play();
          player[0] = m.loadFile("grimes_1.wav");
          player[1] = m.loadFile("grimes_2.wav");
          player[2] = m.loadFile("grimes_3.wav");
        }
      }
    }
  }

  for (int i = 0; i < theSquares.size(); i++) {
    Square q = (Square) theSquares.get(i);
    q.move();
    q.display();
  }
  for (int i = 0; i < theCircles.size(); i++) {
    Circle c = (Circle) theCircles.get(i);
    c.move();
    c.display();
  }
  for (int i = 0; i < theTriangles.size(); i++) {
    Triangle t = (Triangle) theTriangles.get(i);
    t.move();
    t.display();
  }
  for (int i = 0; i < theDeath.size(); i++) {
    Death d = (Death) theDeath.get(i);
    d.move();
    d.display();
  }

  theMouse.bounce();
  theMouse.display();

  }

void serialEvent(Serial _port) { //this is a callback function
  if (myPort == null)return;
  String input = myPort.readStringUntil('\n');
  if (input != null) {  //if a '\n' character has in fact now arrived
    input = input.trim();  //Take off the '\n' character
    force = int(input);  //Turn it into number
  }
}

void keyPressed() {
  if (key == 'q') {
    gameOver = false;
    lastRound = eatenShapes;
    eatenShapes = 0;
    shapesOnScreen = 0;
    theSquareTimer.start();
    theCircleTimer.start();
    theTriangleTimer.start();
    theDeathTimer.start();
    theSquares.clear();
    theCircles.clear();
    theTriangles.clear();
    theDeath.clear();
  }
}

 //Circle Class
class Circle {
  float w; //radius of circle
  float circleX, circleY; //circle location
  float circleXspeed, circleYspeed;
  color cc = color(255,0,0, 180);

  Circle(float tempW) {
    w = tempW;
    circleX = random(w, 5*w);
    circleY = random(w, 5*w);
    circleXspeed = random(2, 5);
    circleYspeed = random(2, 5);
  }

  void move() {
    circleX = circleX + circleXspeed;
    circleY = circleY + circleYspeed;
    //boundary conditions
    if (circleX>width-w/2||circleX<w/2) {
      circleXspeed = -circleXspeed;
    }
    if (circleY>=height-w/2||circleY<w/2) {
      circleYspeed = -circleYspeed;
    }
  }

  void display() {
    fill (cc);
    noStroke();
    shapeMode(CENTER);
    ellipse(circleX, circleY, w, w);
  }

  void disappear() {
    circleXspeed = 0;
    circleYspeed = 0;
    circleX = width*2;
    circleY = height*2;
  }
}

//Death Ball Class
class Death {
  float d; //radius of circle
  float deathX, deathY; //circle location
  float deathXspeed, deathYspeed;
  color dc = color(0, 230);

  Death() {
    d = random(50, 100);
    deathX = random(d, 2*d);
    deathY = random(d, 2*d);
    deathXspeed = random(2, 4);
    deathYspeed = random(2, 4);
  }

  void move() {
    deathX = deathX + deathXspeed;
    deathY = deathY + deathYspeed;
    //boundary conditions
    if (deathX > width-d/2||deathX < d/2) {
      deathXspeed = -deathXspeed;
    }
    if (deathY > height - d/2 || deathY < d/2) {
      deathYspeed = -deathYspeed;
    }
  }

  void display() {
    fill (dc);
    noStroke();
    shapeMode(CENTER);
    ellipse(deathX, deathY, d, d);
  }
}

//Background Grid Class
float k=0;
float j=0;//gridlines
float l = 100; //length of square

void grid() {
  for (k=0; k<height/l; k++) {
    for (j=0;j<width/l;j++) {
      stroke(103, 227, 215);
      strokeWeight(1.2);
      line(j*l, 0, j*l, height); //use j*s if width>height
      line(0, j*l, width, j*l); //use k*s if height>width
    }
  }
}

//Mouse Class
class Mouse {
  float r; //radius
  float mousex, mousey; //position
  float mouseXspeed, mouseYspeed; //speed of mouse

  Mouse() {
    r = 18;
    mousex = random(r, width - 1.5*r);
    mousey = random(r, height - 1.5*r);
    mouseXspeed = random(3, 4);
    mouseYspeed = random(3, 4);
  }

  void grow() {
    r = r + 1;
  }

  void bounce() {
    mousex = mousex + mouseXspeed;
    mousey = mousey + mouseYspeed;

    //boundary conditions
    if (mousex>width-r||mousex< r) {
      mouseXspeed = -mouseXspeed;
    }
    if (mousey>=height-r||mousey <r) {
      mouseYspeed = -mouseYspeed;
    }
  }

  void display() {
    stroke(0);
    strokeWeight(3.5);
    noFill();
    shapeMode(CENTER);
    ellipse(mousex, mousey, r*2, r*2);
  }

  boolean intersectS(Square q) {
    float distanceS = dist(mousex, mousey, q.squareX, q.squareY);
    if (distanceS < r + q.s) {
      return true;
    }
    else {
      return false;
    }
  }

  boolean intersectC(Circle c) {
    float distanceC = dist(mousex, mousey, c.circleX, c.circleY);
    if (distanceC < r + c.w) {
      return true;
    }
    else {
      return false;
    }
  }
  boolean intersectT(Triangle t) {
    float distanceT = dist(mousex, mousey, t.triangleX, t.triangleY);
    if (distanceT < r + t.h) {
      return true;
    }
    else {
      return false;
    }
  }

  boolean intersectD(Death d) {
    float distanceD = dist(mousex, mousey, d.deathX, d.deathY);
    if (distanceD < r + d.d) {
      return true;
    } 
    else {
      return false;
    }
  } 
}

//Squares Class
class Square {
  float s; //square side
  float squareX, squareY; //square location
  float squareXspeed, squareYspeed;
  color qc = color(0,0,255, 200);
  float rotation;

  Square(float tempS) {
    s = tempS;
    squareX = random(2*s, width - 4*s);
    squareY = random(2*s, width - 4*s);
    squareXspeed = random(2, 4);
    squareYspeed = random(2, 4);
  }

  void move() {
    squareX = squareX + squareXspeed;
    squareY = squareY + squareYspeed;
    //boundary conditions
    if (squareX>width-s/2||squareX<s/2) {
      squareXspeed = -squareXspeed;
    }
    if (squareY>=height-s/2||squareY<s/2) {
      squareYspeed = -squareYspeed;
    }
  }

  void disappear() {
    squareXspeed = 0;
    squareYspeed = 0;
    squareX = width*2;
    squareY = height*2;
  }

  void display() {
    noStroke();
    fill(qc);
    rotation = rotation+0.08;
    pushMatrix();
    translate(squareX, squareY);
    rotate(rotation);
    rectMode(CENTER);
    rect(0, 0, s, s);
    popMatrix();
  }
}

//Timer Class
class Timer {
  int savedTime;
  int totalTime;

  Timer(int temp_totalTime) {
    totalTime = temp_totalTime;
  }

  void start() {
    savedTime = millis();
  }

  boolean timeOver() {
    int passedTime = millis() - savedTime;
    if (passedTime > totalTime) {
      return true;
    }    
    else {
      return false;
    }
  }
}

//Triangle Class
class Triangle {
  float h ; //half-side of tiangle
  float triangleX, triangleY; //location
  float triangleXspeed, triangleYspeed;
  color tc = color(0, 255, 0, 200);
  float rotation;

  Triangle(float tempH) {
    h = tempH;
    triangleX = random(4*h, width-4*h);
    triangleY = random(4*h, height-4*h);
    triangleXspeed = random(3, 5);
    triangleYspeed = random(2, 4);
  }

  void move() {
    triangleX = triangleX + triangleXspeed;
    triangleY = triangleY + triangleYspeed;

    if (triangleX > width -h || triangleX < h) {
      triangleXspeed *= -1;
    }

    if (triangleY > height -h || triangleY < h) {
      triangleYspeed *=-1;
    }
  }

  void disappear() {
    triangleXspeed = 0;
    triangleYspeed = 0;
    triangleX = width*2;
    triangleY = height*2;
  } 

  void display() {
    stroke(tc);
    strokeWeight(8);
    noFill();
    rotation = rotation+0.08;
    pushMatrix();
    translate(triangleX, triangleY);
    rotate(rotation);
    shapeMode(CENTER);
    triangle(-h, h, h, h, 0, -h/1.5);
    popMatrix();
  }
}

 

//The Arduino end of the code (also copy and pasted from class)
void setup(){
  Serial.begin(9600);
}

void loop(){
  int input = analogRead(0);
  Serial.print(input);  //notice I did not say println
  Serial.print('\n');  //instead I more explcitly send the linefeed character
}