This week our assignment was to write an application to interface with an iput and/or and output device. I chose to interface with my input device from a few weeks ago. My goal was to write a program that showed an animated sheep running across the screen and that would jump to different heights depending on how hard you squeezed the pompom sensor.
sheep and pompom
sheep gif
RESULTS: After some struggles with Processing and a lot of help from my friend Moe, I got the code to half work, but I ran into issues with serial and couldn't figure out how to properly control the values. MOUSE CLICK v1:
MOUSE CLICK v2:
POMPOM SENSOR:
PROCESSING CODE: Of all the options we had this week, I felt most comfortable tackling code in Processing. I started with an example of how to control an the frames of an animated GIF. However, I soon ran into a wall because I wanted to be able to dictate where in the stream of frames of the running GIF, it switched to the jumping GIF and I couldn't figure out how to do that other that the coordinates of the mouse. So, after consulting with my personal brogrammer, I decided to switch to a code in which I controlled the movement instead of just the frames. In this version I got the sheep to run across the screen and jump on command(mouse click) using linear translation and a few conditionals (I still used a GIF to animate the leg movement). -------------------------------------------------------------------------------------
//SHEEP RUN AND JUMP
//FRAME BASED CONTROL

Animation animation1, animation2;

float xpos;

void setup() {
  size(1200, 416);
  background(255, 255, 255);
  frameRate(8); //rate of display for the frames of the animation
  
  animation1 = new Animation("PT_sheepjump_", 9); //calls image frames
  animation2 = new Animation("PT_sheeprun_", 14); //calls image frames
}

void draw() { 
  if (mousePressed) {
    background(255, 255, 255);
    animation1.display(mouseX, 0); //display jumping sheep at the x coordinate of mouse
  } else {
    background(255, 255, 255);
    animation2.display(0, 0); //display running sheep
  }
}

class Animation {  //class for animating a sequence of images
  PImage[] images;
  int imageCount;
  int frame;
  
  Animation(String imagePrefix, int count) {
    imageCount = count;
    images = new PImage[imageCount];

    for (int i = 0; i < imageCount; i++) {
      // Use nf() to number format 'i' into four digits
      String filename = imagePrefix + nf(i, 4) + ".jpg";
      images[i] = loadImage(filename);
    }
  }

  void display(float xpos, float ypos) {
    frame = (frame+1) % imageCount;
    image(images[frame], xpos, ypos);
  }
  
  int getWidth() {
    return images[0].width;
  }
}

-------------------------------------------------------------------------------------
//SHEEP RUN AND JUMP
//TRANSLATION BASED

Animation animation;
float x,y;
float dim = 416.0; //width of image
float pct = 1.0;   //percent traveled (0.0 to 1.0)

void setup() {
  size(1200, 416);
  background(255, 255, 255);
  frameRate(8); //rate of display for the frames of the animation

  animation = new Animation("PT_sheep_", 3); //calls image frames
}

void draw() {
  background(255, 255, 255);
  
  x = x + 32; //horizontal translation
  if (x > width + dim) { //brings image back to the start
    x = -dim;
  } 
  
  if (pct < 1.0){ 
    pct += .1;
    if (pct < 0.6){
      y = y - 10; //jump up
    }else{
      y = y + 10; //jump down
    }
  }else{
    y = 0; //back to running horizontally
  }
  
  translate(x, y); //drawing the translation
  animation.display(0, 0); //starting the frames at the lower left corner of the window
}

void mousePressed() { //mouse pressed allows jumping loop
pct = 0.0;
 
}

class Animation { //class for animating a sequence of images
  PImage[] images;
  int imageCount;
  int frame;
  
  Animation(String imagePrefix, int count) {
    imageCount = count;
    images = new PImage[imageCount];

    for (int i = 0; i < imageCount; i++) {
      // Use nf() to number format 'i' into four digits
      String filename = imagePrefix + nf(i, 4) + ".jpg";
      images[i] = loadImage(filename);
    }
  }

  void display(float xpos, float ypos) {
    frame = (frame+1) % imageCount;
    image(images[frame], xpos, ypos);
  }
  
  int getWidth() {
    return images[0].width;
  }
}

-------------------------------------------------------------------------------------
//SHEEP RUN AND JUMP
//ADDING SERIAL INPUT
//DOESN'T WORK YET

import processing.serial.*;

Serial myPort; //create object from serial class
int val; //data received from the serial port
int lowval;

Animation animation;
float x,y;
float dim = 416.0; //width of image
float pct = 1.0; //percent traveled (0.0 to 1.0)

void setup() {
  size(1200, 416);
  background(255, 255, 255);
  frameRate(8); //rate of display for the frames of the animation
  
  String portName = Serial.list()[0]; //opening the serial port
  print(portName);
  myPort = new Serial(this, portName, 9600);
  
  animation = new Animation("PT_sheep_", 3); //calls image frames
}

void draw() {
  background(255, 255, 255);
 
  while (myPort.available() == 0){ //if data is available, read it and store it in lowval
    lowval = myPort.read();
  }
  
  x = x + 32; //horizontal translation
  if (x > width + dim) { //brings image back to the start
    x = -dim;
  } 

  if (lowval > 20 && lowval < 100){ //using sensor values to trigger jump loop
    pct = 0.0;
    println(lowval);
  }
  
  if (pct < 1.0){
    pct += 0.1;
    if (pct < 0.6){
      y = y - 10; //jump up
    }else{
      y = y + 10; //jump down
    }
  }else{
    y = 0; //back to running horizontally
  }
  
  translate(x, y); //drawing the translation
  animation.display(0, 0); //starting the frames at the lower left corner of the window
}


class Animation {  //class for animating a sequence of images
  PImage[] images;
  int imageCount;
  int frame;
  
  Animation(String imagePrefix, int count) {
    imageCount = count;
    images = new PImage[imageCount];

    for (int i = 0; i < imageCount; i++) {
      // Use nf() to number format 'i' into four digits
      String filename = imagePrefix + nf(i, 4) + ".jpg";
      images[i] = loadImage(filename);
    }
  }

  void display(float xpos, float ypos) {
    frame = (frame+1) % imageCount;
    image(images[frame], xpos, ypos);
  }
  
  int getWidth() {
    return images[0].width;
  }
}

In adding the serial input from my pompom sensor, I wanted to replace the mouse control with the serial values, but for some reason, the values it was printing didn't seem to match up with the code I was writing. I verified that the sensor was working properly in a different code (one that just changed fill value based on the sensor value). So, I'm not sure what's going on or why the values don't seem to match up in this particular code. I am happy that I got the code to work well with just the mouse, but the pompom squeezing would be so satisfying!