Assignment: Sensing PART II: Force Sensitive Resistors and Photocells
Collaborators:
For the first part of the lab, I created a visualization targeted at the photocell, although it works with all of them.
When there is a lot of light on the photocell, a yellow sphere draws and grows brighter against a cyan backround. As the amount of light decreases, the background darkens and stars appear and twinkle (sort of).
Processing Code for Visualization
import processing.serial.*;
String portname = "/dev/tty.usbserial-A9007Lpv";
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
void setup() {
frameRate(10);
background(100, 200, 255);
noStroke();
size(400,400);
port = new Serial(this, portname, 9600);
}
void draw() {
}
void serialEvent(Serial p) {
int c = port.read(); // read what's coming from the serial port
int val=0; // value that will be the value from your sensor
color bg; //change the color of the background based on the value of the potentiometer
if (c != lf && c != cr) { //check to see if the value from analong input is linefeed or return
buf += char(c); //if it's neither apend the character to the buffer?
}
if (c == lf) { //if it is equal to line feed
val = int(buf); //set the varaible for the sensor value to the value in the buffer
println("This is Val:" + val); // this is for testing
buf = ""; //clean out the buffer
}
// color bg; //change the color of the background based on the value of the potentiometer
if(val >= 50)
{
bg=(#99ccff);
// delay(100);
background(#99ccff); // erase screen
dEllipse(val);
}
else if(val >=25) {
bg=(#333399);
// delay(100);
background(#000033);
drawStar(bg);
}
else {
bg=(#000033);
// delay(100);
background(#000033);
drawStar(bg);
}
}
void dEllipse(int x) {
noStroke();
fill (255,255,0, x);
ellipse(200, 200, 50, 50);
}
void changeEllipse(int x) {
int y=0;
println("In Change Elipse");
println(x);
if(x<100){
y=20;
}
else {
y=100;
}
noStroke();
fill(255,255,0,y);
ellipse(100, 100, 12, 12);
}
void drawStar(/*float xpos, float ypos,*/ color erase){
int x = int(random(0,width));
int y = int(random(0,height));
float x2=x + 30;
float y2=y + 20;
float x3=x + 15;
float y3=y - 30;
noStroke();
fill(#99ccff);
triangle (x, y - 20, x2, y - 20, x3, y);
triangle (x+4, y+10, x2-4, y+10, x3, y3-5);
fill(erase);
triangle (x+4, y+10, x2-4, y+10, x3, y-5);
}
For the second half of the lab, I tried several materials(cork lined coasters, sponges, foam, etc., but found that many of them distributed the weight of objects too much. In the end I selected "magic eraser" foam (for removing scratches/dirt from paint) because it wasn't as dense as some of the others. It also cleans off easily, so I made a laundry detergent notifying visualization that indicates when it is time to replace the detergent.
Processing Code for Detergent visualzation
import processing.serial.*;
String portname = "/dev/tty.usbserial-A9007Lpv";
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
void setup() {
frameRate(10);
noStroke();
size(400,400);
port = new Serial(this, portname, 9600);
}
void draw() {
}
void serialEvent(Serial p) {
int c = port.read(); // read what's coming from the serial port
int val=0; // value that will be the value from your sensor
int val2 = 0; // value used to look for changes in the sensor
color bg; //change the color of the background based on the value of the potentiometer
if (c != lf && c != cr) { //check to see if the value from analong input is linefeed or return
buf += char(c); //if it's neither apend the character to the buffer?
}
if (c == lf) { //if it is equal to line feed
val = int(buf); //set the varaible for the sensor value to the value in the buffer
println("This is Val:" + val); // this is for testing
buf = ""; //clean out the buffer
if(val < 5) //if the value from the FSR is less that five - you are low on detergent
{
background(#ffffff);
fill(#ff0000);
rect(width-(width), height-.1*height, width, height-.1*height);
}
else if(val>=5) { //if the value is higher than five you are ok for at least a few more loads, if not more
background(#ffffff);
fill(#00CCFF);
rect(0, 75, width, 325);
}
}
}
Materials
- Breadboard
- Resistors
- Photocell, FSR, Pot
- Cleaning foam (magic eraser)
- Laundry detergent bottles (one nearly fully, one nearly empty)