FSR and Visualization

Submitted by kmpark on Wed, 02/27/2013 - 07:23

 

Description

Using Arduino Uno with FSR and Processing, I was able to build an pressure visualization. However, the process was very painful because the serial communication was very foreign to me, and I still don't think mine is working properly. I initially designed a wooden block building indicator based on FSR input. However, it turns out that the FSR was not a weight sensor to speak but a force sensor. I had trouble with mapping the output of sensors to the size of the ball in visualization too. My serial monitor was kind of speeding up crazy, so I am not sure processing is getting the input as it supposed to be. I tried with refreshrate on processing and adding delay on Arduino code, but nothing really worked. 

Also, I was trying to make a visualization showing overlapped pictures in response to the FSR value, but the processing yielded a lot of JAVA warnings and runtime error, so I dropped it.  I will try this personally later.

 

Components Used

1- Arduino Uno

1- LED

2- Resistors

1- Breadboard

1- FSR

 

Code

- Processing code:

/*
 * Arduino Ball Paint
 * (Arduino Ball, modified 2008)
 * ---------------------- 
 *
 * Draw balls randomly on the screen, size controlled by a device
 * on a serial port.  Press space bar to clear screen, or any 
 * other key to generate fixed-size random balls.
 *
 * Receives an ASCII number over the serial port, 
 * terminated with a carriage return (ascii 13) then newline (10).
 * 
 * This matches what Arduino's " Serial.println(val)" function
 * puts out.
 *
 * Created 25 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com
 * http://todbot.com/ 
 */
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbmodem1421"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
 
// Declaring a variable of type PImage
//PImage daniel;  
//PImage daniel2;  
 
void setup() {
//  size(800,600);
  frameRate(50);
  smooth();
  background(40,40,40);
  noStroke();
 
  port = new Serial(this, portname, 9600); 
  port.bufferUntil('\n');
 
  size(400,400);
 
}
 
 
 
void draw() {
//  background(0);
//  // Draw the image to the screen at coordinate (0,0)
//    // Make a new instance of a PImage by loading an image file
//  daniel = loadImage("1.jpg");
//  daniel2 = loadImage("2.jpg");
//  background(daniel2);
   
//  tint(255, val/100);
//  image(daniel,0,0);
}
 
 
void keyPressed() {
  if(key == ' ') {
    background(40,40,40);  // erase screen
  }
  else {
//    int x = int(random(0,width));
//    int y = int(random(0,height));
    int x = int(random(100,width-100));
    int y = int(random(100,height-100));
    drawball(x,y,20);
  }
}
 
// draw balls
void drawball(int x, int y, int r) {
  for (int i=0; i<100; i++ ) {
    fill(255-r,r,255-i);
    ellipse(x,y,r,r);
  }
 
}
// called whenever serial data arrives
void serialEvent(Serial port) {
  String c = port.readStringUntil(lf);
//  if (c != lf && c != cr) {
//    buf += char(c);
//  }
  if (c != null) {
    int val = int(trim(c));
    println("val="+val); 
    if (val>1000) {
      val = val/2;
    }
//    int x = int(random(0,width));
//    int y = int(random(0,height));
    int x = int(random(100,width-100));
    int y = int(random(100,height-100));
    drawball(x,y,val/2);
    buf = "";
    background(40,40,40);  // erase screen
  }
}
 
- Arduino Code:
 
int FSRPin = 0;    // select the input pin for the potentiometer
int ledPin = 13;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor
 
void setup() {
  pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
  Serial.begin(9600);
}
 
void loop() {
  val = analogRead(FSRPin)/4;    // read the value from the sensor    
  analogWrite(ledPin, val);  // turn the ledPin on
//  delay(val);                  // stop the program for some time
//  digitalWrite(ledPin, LOW);   // turn the ledPin off 
//  delay(val);                  // stop the program for some time
//  map(val, 1, 10000, 0, 100);
  Serial.println(val);
  delay(FSRPin);
  Serial.println('\n');
 
  }
 

Video

http://youtu.be/Go_9GWe4HhI

 

0
Your rating: None
Drupal theme by Kiwi Themes.