Description:
In this lab we had to consider how to use the FSR to vary the visual output using Processing. I decided to vary the color of the circles more differently with varying pressure: light pressure to harder pressure ranges from the colors pink to red to purple to blue.
For the mechanical aspect of the homework, we had to consider to use other items to use in conjuction with the FSR to vary the pressure input. I initially tried to use a sandwich bag, a rubber band and a straw to cover the FSR and try to apply pressure by blowing air into the bag with a straw, but this proved to be difficult as air often escaped from the bag and FSR did not read any pressure differences.
Components:
1x Arduino
1x Breadboard
1x Photocell
1x FSR
3x LEDs
1x 220 Ohm Resistor
1x 10k Ohm Resistor
Code: (in Processing)
/*
* 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
* http://todbot.com/
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbmodemfd121"; // 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(252,228,13); // erase screen - background is now yellow
}
else {
int x = int(random(0,width));
int y = int(random(0,height));
drawball(x,y, 50);
}
}
// draw balls
void drawball(int x, int y, int r) {
for(int i=0; i < 100; i++){
if (r <= 200) { // this will vary the output from pink to red
fill(222, 31,( 215 - r));
ellipse(x,y,r,r);
}
else { //this will vary the ouput from purple to blue
fill((-(min(r,255))+ 255), 13, 140);
ellipse(x,y,r,r);
}
}
}
// 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(252,228,13); // erase screen- background is now yellow
}
}
- Login to post comments