Lab 4-Working Env

Submitted by seanchen on Tue, 02/26/2013 - 23:53

Description

I decided to monitor the pressure placed on my wrist when using the mouse. And then visualize it on Processing in the form of a line graph. The setting is simply done by attaching a post-it beneath my mouse, and then tape the force sensing resistor on the post-it where my wrist usually rest on. Then the value read by Arduino is sent through serial port to Processing to draw the graph.

I used various colors representing different levels of pressure. The serial port is not very stable so I had to do some filters to take out values that might have been a bad transmission. When I tried to add a second value to the serial port (the photo cell) separated by a comma, Arduino started to lag whenever the Processing app is initiated. I spent quite some time debugging but couldn't get it to work.

 

Components

1 x Arduino UNO Board

1 x FSR

 

Code

Arduino

const int forcePin = 2;               
 
int forceVal = 0;
 
void setup() {
  Serial.begin(9600);
}
 
void loop() {
  forceVal = analogRead(forcePin);
  Serial.println(forceVal);
 
}
 
 

Processing

// Reference line graph example at 
// http://www.learningprocessing.com
 
import processing.serial.*;
String portname = "/dev/tty.usbmodem1411";
Serial port;
String inputStr = "";
int pHeight = 400;
 
float[] vals;
int pressure, light;
 
void setup() {
  println(3/4*pHeight);
  size(600,400);
  smooth();
  port = new Serial(this, portname, 9600);
 
  frameRate(30); 
  vals = new float[width];
  for (int i = 0; i < vals.length; i++) {
    vals[i] = pHeight;
  }
}
 
 
void draw() {
 
  background(255);
  // Draw lines connecting all points
  for (int i = 0; i < vals.length-1; i++) {
    setStroke(vals[i+1]);
    strokeWeight(2);
    line(i,vals[i],i+1,vals[i+1]);
  }
  
  // Slide everything down in the array
  for (int i = 0; i < vals.length-1; i++) {
    vals[i] = vals[i+1]; 
  }
 
}
 
void setStroke(float val){
  //println(val);
  float p = float(pHeight);
  
  if(val > 3/4.0*p){
    stroke(0,204,0);
  } else if(val > p/2.0) {
    stroke(225,225,0);
  } else if(val > p/4.0) {
    stroke(255,128,0);
  } else {
    stroke(255,0,0);
  } 
  
}
 
 
void serialEvent(Serial myPort) {
  if (myPort.available() > 0) {
    inputStr = myPort.readStringUntil('\n');
      
      if(inputStr != null) {
        //print(inputStr);
        inputStr = trim(inputStr);    
        
        if(int(inputStr) < 1025) {
          println(inputStr);
          float val = pHeight - map(int(inputStr), 0, 800, 0, pHeight);
          if(val - vals[width-2] < pHeight/2){
            vals[width-1] = val;
          }
        } else {
          println("N/A"); 
        }
 
      }
     
  }
}
 
Processing result
sensor setup
0
Your rating: None
Drupal theme by Kiwi Themes.