Sensing FSR and Photocells

Posted by jlzych

jlzych's picture

My code displays a bezier curve of the amount of force detected. I read the force of a tennis ball hitting a tennis racquet. I wanted to be able to do an actual stroke but the FSR isn't wireless so that was not possible. Unfortunately I don't have video capabilities so you'll have to make do with pictures!

Code

/*
 * Draws a bezier curve of the force detected.
 */

import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbmodem1a21"; // or 1d11
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
int force = 0;

void setup() {
  size(450,300);
  frameRate(30);
  smooth();
  background(40,40,40);
  noStroke();
  port = new Serial(this, portname, 9600); 
}

void draw() {
  background(40, 40, 40);
  drawline();
  //delay(250);
}

// draw line
void drawline() {
  noFill();
  stroke(255);
  // Scale amplifier to between 0 and half the screen height
  float amplifier = map(force, 0, 1024, 0, height/2);
  int x = width/2;
  int y = int(height/2.0 - amplifier);
  bezier(0, height/2, width/4, height/2, width/4, y, x, y);
  bezier(x, y, width*(.75), y, width*(.75), height/2, width, height/2);
}

// 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);
    if (val != 0) {
      println("val="+val);
      force = val;
    }
    buf = "";
  }
}

 

Tennis racquet and ball
Graph of force
0
Your rating: None