Assignment 4: Bear Hug with FSR

Assignment: Sensing PART II: Force Sensitive Resistors and Photocells

Collaborators:

This huggable teddy bear transmits an integer through the Arduino board/USB port to a Java applet created using Processing.

Arduino Code:

/*
* Quick little script to write input from the FSR to the serial port
* Jessica Voytek, Sept 2009
*/

int FSRPin = 0;
int value = 0;

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

void loop() {
value = analogRead(FSRPin);
Serial.println(value);
delay(50);
}

Processing Code:

/* Bear Hugs!!
*
* Adapted from:
* Load background image: http://processing.org/learning/basics/backgroundimage.html
* Arduino Ball Paint: http://courses.ischool.berkeley.edu/i290-13/f07/system/files/BouncingBallPaint+(Processing).txt
* How to draw a heart: http://processing.org/discourse/yabb2/YaBB.pl?num=1246205739
* And special thanks to Alison Meier for her Indiana Jones and the FSR Boulder as inspiration!
* http://courses.ischool.berkeley.edu/i262/f09/?q=node/499
*
* Modifications by Jessica Voytek, Sept, 2009
*
*/

import processing.serial.*;
String portname = "/dev/tty.usbserial-A9007LDj"; // Serial port name
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
PImage bg;

void setup() {
size(679,679); //size of window corresponds to size of image
frameRate(10);
bg = loadImage("Bear_Hug.jpg"); //sets image
smooth();
background(bg); // opens background image
port = new Serial(this, portname, 9600); //connects to Serial port
}

void draw() {

}

void changeHeart(int val) {
int heart_coords[] = {320,300,320,220,480,260,320,400,320,220,160,260,320,400};
int begin_heart_x = heart_coords[0];
int begin_heart_y = heart_coords[1];

int bezierV1_cx1 = heart_coords[2];
int bezierV1_cy1 = heart_coords[3];
int bezierV1_cx2 = heart_coords[4];
int bezierV1_cy2 = heart_coords[5];
int bezierV1_x = heart_coords[6];
int bezierV1_y = heart_coords[7];

int bezierV2_cx1 = heart_coords[8];
int bezierV2_cy1 = heart_coords[9];
int bezierV2_cx2 = heart_coords[10];
int bezierV2_cy2 = heart_coords[11];
int bezierV2_x = heart_coords[12];
int bezierV2_y = heart_coords[13];

smooth();
noStroke();

float theVal = float(val);  // make a float out of the pot2Value and assign it to i
float RedValue = 0.00;  // initiate a variable which will hold a float value for the percentage on

// This calculation seems a little wierd, but it's optimized for the range 
// of values I was gettinging with the FSR in this configuration
RedValue = ((theVal - 100)/850) * 255; 

// Draw the heart in the right color
fill(RedValue,20,0);
beginShape();
vertex(begin_heart_x, begin_heart_y);
bezierVertex(bezierV1_cx1, bezierV1_cy1, bezierV1_cx2, bezierV1_cy2, bezierV1_x, bezierV1_y);
vertex(begin_heart_x, begin_heart_y);
bezierVertex(bezierV2_cx1, bezierV2_cy1, bezierV2_cx2, bezierV2_cy2, bezierV2_x, bezierV2_y);
endShape();
}

// 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);
buf = "";
changeHeart(val);
}
}

Images and Videos