A4: FSR Lab
Description
Use a FSR to control the size of "Heavier" or "Lighter" text based on object weight.
Materials
- 1x Light Emitting Diode (LED)
- 1x Resistor
- 1x Force Sensing Receptor
- plastic plate and paper clamp
Audrino Code
/*
* one pot fades one led
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int potPin = 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() {
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the value from the sensor, between 0 - 1024
Serial.println(val);
analogWrite(ledPin, val/4); // analogWrite can be between 0-255
}
Processing Code
/* Modified from A. Haney Kapow so that the word drawn was either Heavy or Light
* dependent on object weight.
* Receives an ASCII number over the serial port,
* terminated with a carriage return (ascii 13) then newline (10).
*
*
* 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.usbmodem641"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
void setup() {
size(300,300);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, portname, 9600);
}
void draw() {
}
void keyPressed() {
if(key == ' ') {
background(40,40,40); // erase screen
}
else {
int x = int(random(0,width));
int y = int(random(0,height));
}
}
// draw weight
void weight(int val){
float i =random(100);
float j =random(50);
stroke(255);
fill(255,i,j);
if (val < 100){
val = int (val/10);
beginShape();
vertex(20*val,10*val);
vertex(60*val,30*val); //1
vertex(80*val,20*val); //2
vertex(60*val,40*val); //3
vertex(90*val,50*val); //4
vertex(70*val,60*val); //5
vertex(90*val,90*val); //6
vertex(60*val,60*val); //7
vertex(50*val,80*val); //8
vertex(40*val,60*val); //9
vertex(20*val,50*val); //11
vertex(40*val,50*val); //12
vertex(10*val,40*val); //13
vertex(40*val,35*val); //14
vertex(20*val,10*val); //14
endShape(CLOSE) ;
fill(255);
// int textsize = int(14*val);
// textSize(textsize);
text("Lighter",45*val,45*val);
if (val > 100){
val = int (val/10);
beginShape();
vertex(20*val,10*val);
vertex(60*val,30*val); //1
vertex(80*val,20*val); //2
vertex(60*val,40*val); //3
vertex(90*val,50*val); //4
vertex(70*val,60*val); //5
vertex(90*val,90*val); //6
vertex(60*val,60*val); //7
vertex(50*val,80*val); //8
vertex(40*val,60*val); //9
vertex(20*val,50*val); //11
vertex(40*val,50*val); //12
vertex(10*val,40*val); //13
vertex(40*val,35*val); //14
vertex(20*val,10*val); //14
endShape(CLOSE) ;
fill(255);
// int textsize = int(14*val);
// textSize(textsize);
text("Heavier",45*val,45*val);
}
}
// 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);
}
int x = int((width/2));
int y = int((height/2));
// drawball(x,y,val);
weight(val);
buf = "";
}
}