FSR Sriracha

Posted by Cole

Cole's picture

I attached my FSR to a Sriracha bottle as shown in the attached photo. Since I use this spicy sauce on almost every meal and you can squeeze the bottle, I thought it would be a fun everyday object to attch the FSR to. 

I then programmed a ridiculous animation to that I wanted to play when you squeezed the bottle. I programmed the animation based on the "Sequential" example under the "Animation" category.

I took a quick video of what the screen looks like here:http://vimeo.com/20276309. However, after many attempts, I could not link the FSR to the animation for the life of me. I tried many different things, I wont bother posting the mangled code as it is long and does nothing... but I will post code for the animation at the bottom.

Instead I modified the code that we were given in lab, posted below. I know it's nothing exciting, but the deadline to post the HW is near and I have a quiz in the morning to study for. Sorry, I will try to get the animation code working with the FSR later.

-- Simple (but working) Code-- Flashes Sriracha colored balls incrementally on the screen...

 

import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "COM4"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
void setup() {
  size(300,300);
  frameRate(10);
  smooth();
  background(40,40,40);
  noStroke();
  port = new Serial(this, portname, 9600); 
}
void draw() {
}
void keyPressed() {
  if(key == ' ') {
    background(40,40,40);  // erase screen
  }
  else {
    for (int s=0; s < height; s--){
      for (int t=0; t < width; t=t+15){
    int x = t;
    int y = s;
    drawball(x,y, 50);
      }
    }
  }
}
// draw balls
void drawball(int x, int y, int r) {
  for (int i=0; i<100; i++ ) {
    fill(255,0,0);
    ellipse(x,y,r,r);
  }
 
}
// called whenever serial data arrives
void serialEvent(Serial p) {
  int c = port.read();
  if (c != lf && c != cr) {
    buf += char(c);
  }
  if (c == lf) {
    int val = int(buf);
    println("val="+val); 
    int x = int(random(0,width));
    int y = int(random(0,height));
    drawball(x,y,val);
    buf = "";
    background(40,40,40);  // erase screen
  }
}
------------------------------------------------------------------------------------------------------------
Animation
------------------------------------------------------------------------------------------------------------
/**
 * Sequential
 * by James Patterson.  
 * 
 * Displaying a sequence of images creates the illusion of motion. 
 * Twelve images are loaded and each is displayed individually in a loop. 
 */
  
int numFrames = 24;  // The number of frames in the animation
int frame = 0;
PImage[] images = new PImage[numFrames];
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "COM4"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10    
void setup()
{
  size(1200, 650);
  noLoop();
  frameRate(8);
  port = new Serial(this, portname, 9600); 
  
  images[0] = loadImage("sriracha1.gif");
  images[1] = loadImage("sriracha2.gif");
  images[2] = loadImage("sriracha3.gif");
  images[3] = loadImage("sriracha4.gif");
  images[4] = loadImage("sriracha5.gif");
  images[5] = loadImage("sriracha6.gif");
  images[6] = loadImage("sriracha7.gif");
  images[7] = loadImage("sriracha8.gif");
  images[8] = loadImage("sriracha9.gif");
  images[9] = loadImage("sriracha10.gif");
  images[10] = loadImage("srirachaS.gif");
  images[11] = loadImage("srirachaSR.gif"); 
  images[12] = loadImage("srirachaSRI.gif"); 
  images[13] = loadImage("srirachaSRIR.gif"); 
  images[14] = loadImage("srirachaSRIRA.gif"); 
  images[15] = loadImage("srirachaSRIRAC.gif"); 
  images[16] = loadImage("srirachaSRIRACH.gif"); 
  images[17] = loadImage("srirachaSRIRACHA.gif"); 
  images[18] = loadImage("srirachaSRIRACHA1.gif"); 
  images[19] = loadImage("srirachaSRIRACHA2.gif"); 
  images[20] = loadImage("srirachaSRIRACHA3.gif");
  images[21] = loadImage("srirachaSRIRACHA4.gif");
  images[22] = loadImage("srirachaSRIRACHA5.gif");
  images[23] = loadImage("srirachaSRIRACHA6.gif");
  // If you don't want to load each image separately
  // and you know how many frames you have, you
  // can create the filenames as the program runs.
  // The nf() command does number formatting, which will
  // ensure that the number is (in this case) 4 digits.
  //for(int i=0; i<numFrames; i++) {
  //  String imageName = "PT_anim" + nf(i, 4) + ".gif";
  //  images[i] = loadImage(imageName);
  //}
 
void draw() 
  frame = (frame+1) % numFrames;  // Use % to cycle through frames
  image(images[frame], 0, 0);
}
// called whenever serial data arrives
void serialEvent(Serial p) {
  int c = port.read();
  if (c != lf && c != cr) {
    buf += char(c);
  }
  if (c == lf) {
    int val = int(buf);
    println("val="+val); 
    redraw();
    buf = "";
  }
}
SrirarchaFSR.jpg
0
Your rating: None