Description
I actually had a hard time trying to think of something interesting to create with processing and the FSR. (I wanted to make an etch-a-sketch, but I didn't feel well enough to complete that in time. Yeah yeah, excuses.) So I looked through the example code and decided to modify Bouncy Bubbles to look like bubblegum bubbles, with the FSR affecting their descent. They seem to be suspended when pressure is applied, but then they come crashing back down when force is lifted.
Components Used
1 arduino uno R3 (5V)
1 10K Ω resistor
1 FSR
6 wires
1 USB cable
Code
/**Bubblegum1.0 (2/26/2013) based on Bouncy Bubbles
* Bouncy Bubbles
* based on code from Keith Peters.
*
* Multiple-object collision.
*/
import processing.serial.*;
// Imported wholesale from Arduino Ball Paint
String portname = "COM6"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
float FSRval = 1;
int numBalls = 10;
float spring = 0.06; //I just played around with these values
float gravity = 0.03;
float friction = -0.9;
Ball[] balls = new Ball[numBalls];
void setup() {
size(640, 480);
port = new Serial(this, portname, 9600); //I had trouble figuring this out
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(30, 70), i, balls);
}
stroke(255, 113, 215); //a little outline to make it look more like bubbles
smooth(); //anti-aliased edges
fill(255, 36, 190, 191); //Changed color and transparency to look like bubblegum
}
void draw() {
background(0);
for (int i = 0; i < numBalls; i++) {
balls[i].collide();
balls[i].move();
balls[i].display();
}
}
class Ball {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;
Ball[] others;
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide() {
for (int i = id + 1; i < numBalls; i++) {
float dx = others[i].x - x;
float dy = others[i].y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring;
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
void move() {
vy += gravity/FSRval; //to use FSR to affect gravity
x += vx;
y += vy;
if (x + diameter/2 > width) {
x = width - diameter/2;
vx *= friction;
}
else if (x - diameter/2 < 0) {
x = diameter/2;
vx *= friction;
}
if (y + diameter/2 > height) {
y = height - diameter/2;
vy *= friction;
}
else if (y - diameter/2 < 0) {
y = diameter/2;
vy *= friction;
}
}
void display() {
stroke(255, 113, 215);
fill(255, 36, 190, 191);
smooth();
ellipse(x, y, diameter, diameter);
}
}
// needs serial data input, modded from Arduino Ball Paint code
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);
if(val==0){
FSRval = 1;
}
else{
FSRval = (float)(val*10)/1500;
//I had trouble figuring out a good value to divide by
}
buf = "";
}
}
Mechanical Construction
I wanted to create something that allowed for more nuanced pressure on the FSR. What I came up with looks rather bizarre, but it works. It is a sliver of styrofoam packing peanut glued to an index card with toothpicks sticking out of it. The packing peanut is the exact size of the FSR (which is placed under the index card), but the spread of the toothpicks allows for wider distribution and pinpointing of pressure. This way I can press different amounts of them to get a wider range of readings.