Lab04

fanwaipio's picture

 

Description
Visualization
* Use the Arduino to read FSR input and visualized as circle wave.
 
Mechanical Construction
* Measure the force which writing on a card
 
Components Used
Resistor
FSR
Card
 

Processing part

/* 
 * Circle visualization of FSR
 * ---------------
 *
 * Created 27 September 2011
 * copyleft 2011 Fan, Wai Pio
 *
 * one pot dims, the other pot changes the blinking rate
 * modification of the following
 * Arduino Ball Paint
 * 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.usbmodem411"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
 
int width = 600;
int height = 600;
 
float[] radiuses = new float[10];
float[] dRadiuses = new float[10];
int oldVal = 0;
 
void setup() {
  size(width,height);
  frameRate(10);
  smooth();
  background(255,255,255);
  noFill();
  port = new Serial(this, portname, 9600); 
}
 
void draw() {
}
 
void keyPressed() {
  if(key == ' ') {
    background(255,255,255);  // erase screen
  }
}
 
// draw circles
void drawCircle() {
  for(int i=0; i<radiuses.length; i++) {
    radiuses[i] += dRadiuses[i];
    ellipse(width/2,height/2,radiuses[i],radiuses[i]);
    if(radiuses[i] > width*1.2) {
      radiuses[i] = 0;
      dRadiuses[i] = 0;
    }
  }
}
 
// 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>0) {//(val - oldVal) * (val - oldVal) > 400) {
      oldVal = val;
      int i=0;
      for(; i < dRadiuses.length; i++)
        if(dRadiuses[i] == 0) {
          break;
        }
        
      if(i < radiuses.length)
        dRadiuses[i] = val/1024.0*radiuses.length;
        
        
    }
    
    buf = "";
    background(255,255,255);  // erase screen
    drawCircle();
  }
}
 
Arduiro Part
/*
  AnalogReadSerial
 Reads an analog input on pin A0, prints the result to the serial monitor 
 
 This example code is in the public domain.
 */
void setup() {
  Serial.begin(9600);
}
 
void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue, DEC);
}
27092011306.jpg
0
Your rating: None