Assignment: Sensing PART II: Force Sensitive Resistors and Photocells
Collaborators:
Much thanks to Mark R. who helped me figure out the basics for this (without whom I wouldn't have been able to do the little I was able!)
For the visualization, I couldn't figure out the code enough to get it to run. I amended it well enough so that Processing would run it without any error notices, & the box would appear, but without the visualization I wanted. After hours of tinkering, I have accepted that this is the best I, alone, can do! I took the new visualization from OpenProcessing.org: http://www.openprocessing.org/visuals/?visualID=3330 . It's very beautiful (see attachment)! I spliced it into the paintball code, & ideally that visual would've restarted with pressure from the FSR.
For the mechanical aspect, Mark helped me flip the Arduino code so that the LED starts at full brightness & dims as pressure is applied to the FSR. I thought about the refilling of coffee cups in restaurants, & how it could be cute if the cup or saucer were sensored so that a light dimmed as pressure (liquid) diminished, & was almost off when a refill was needed. In the photos, I taped a dime on the FSR to even out pressure distribution, & used marbles rather than liquid (I am FAR too clumsy to have tried this with an actual beverage).
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
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbserial-A9007LRl"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
void setup() {
size(600,600);
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, y, deg;
int mode = 1;
int nbr =60;
Dot[] d = new Dot[nbr];
float maxRadius = 200;
float angle=0;
void setup(){
background(233, 233, 220);
size(440, 440);
smooth();
x = width/2 ;
y = height/2;
float deg = (360/nbr);
for (int i=0; i<d.length; i++){
angle += radians(deg);
maxRadius += random(100);
d[i] = new Dot(x + cos(angle)*maxRadius , y+ sin(angle)*maxRadius , random(-20, 20), i);
}
}
void mouseReleased(){
background(233, 233, 220);
float deg = (360/nbr);
for (int i=0; i<d.length; i++){
angle += radians(deg);
maxRadius =200+random(20);
d[i] = new Dot(x + cos(angle)*maxRadius , y+ sin(angle)*maxRadius, random(-20, 20), i);
}
}
void draw(){
beginShape();
for (int i=0; i<d.length; i++){
d[i].maison();
stroke(20);
noFill();
vertex(d[i].x , d[i].y);
}
endShape(CLOSE);
}
class Dot{
float x, y;
float vx, vy;
float ar = random(-20, 20);
int id;
Dot(float xpos, float ypos, float angledeRotation, int moi){
x = xpos;
y = ypos;
ar = angledeRotation;
id = moi ;
}
void maison(){
ar+=random(0.2);
x += cos(ar)*(random(1))+((width/2 - x) * 0.025);
y += sin(ar)*(random(1))+((height/2 - y) * 0.025);
}
}
}
}
// 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);
int x = int(random(0,width));
int y = int(random(0,height));
drawball(x,y,val);
buf = "";
background(40,40,40); // erase screen
}
}
Arduino 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 = 2; // select the input pin for the potentiometer
int ledPin = 9; // 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, 255-val/4); // analogWrite can be between 0-255
}