4. FSR on Soccer Ball with Bouncy Mushroom Visualization

Posted by Erich

Erich's picture

Mushroom Visualization with FSR on Soccer Ball

Attaching a force sensitive resistor to a soccer ball, a graphical visualization of a mushroom responds to the force on the ball. 

Movie: http://www.youtube.com/watch?v=x70qSX6HOkc

Processing Code: (based on spring example)

/**
 * Mushroom 
 * 
 * Squeeze force sensor start the spring. 
 */
 
// Spring drawing constants for top bar
int s_height = 80;     // Height
int left = 50;         // Left position
int right = 250;       // Right position
int max = 450;         // Maximum Y value
int min = 50;          // Minimum Y value
boolean over = true;   // If mouse over
boolean move = false;  // If mouse down and over
 
// Spring simulation constants
float M = 1.9;   // Mass
float K = 0.1;   // Spring constant
float D = .97;  // Damping
float R = 100;    // Rest position
 
// Spring simulation variables
float ps = 200.0; // Position
float vs = 0.0;  // Velocity
float as = 0;    // Acceleration
float f = 0;     // Force
float FSR = 0;   // Force Sensor Position
 
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "COM6";
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
 
void setup() 
{
  size(300, 500);
  rectMode(CORNERS);
  smooth();
  port = new Serial(this, portname, 9600);
  frameRate(60);
}
 
void draw() 
{
  background(100, 150, 0);
  updateSpring();
  drawSpring();
}
 
void drawSpring() 
{
  // Draw base
  fill(255);
  float b_width = 0.25 * ps;
  if (b_width > 75){ b_width=75;
  } else if (b_width < 15){ b_width=15;}
  rect(width/2 - b_width, ps + s_height, width/2 + b_width, 700);
 
  // Set color and draw top arc
  if(over || move){ 
    fill(255,0,0);
  } else { 
    fill(204,0,0);
  }
  arc(left/2+right/2,ps+s_height,right,(max-ps)/4+s_height,PI,TWO_PI);
  //rect(left, ps, right, ps + s_height);
}
 
void updateSpring()
{
  // Update the spring position
  if(!move) {
    f = -K * (ps - R);    // f=-ky
    as = f / M;           // Set the acceleration, f=ma == a=f/m
    vs = D * (vs + as);   // Set the velocity
    ps = ps + vs;         // Updated position
  }
  if(abs(vs) < 0.1) {
    vs = 0.0;
  }
 
  // Set and constrain the position of top bar
  if(move) {
    ps = FSR - s_height/2;
    if (ps < min) { ps = min; } 
    if (ps > max) { ps = max; }
  }
}
 
// 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); 
    if (val > 800){ 
      move=true; over=true;
      FSR = map(val,800,1024,0,400);
       
    }else if (val < 800){
      move=false; over=false; FSR=0;
    }
    print("FSR="+FSR+"   ");
    buf = "";
  }
}

 

Mushroom Visualization
FSR Circuit
0
Your rating: None