Sensing Part 2: FSR and Photocells

Posted by melissa

melissa's picture

Description: 
Used photocell and force sensitive resistor to control brightness of an LED. Then used them to with Processing to control shapes on screen. Also created a mechanical system to distribute force on FSR using a pen. 

Materials used:

  • LEDs
  • Force sensitive resistor
  • Photocell
  • 220 ohm resistor for led
  • 10k ohm resistor for voltage divider for FSR and photocell
  • Processing
  • Pen

Code:
I modified the example ballpaint code. Now it draws smaller balls of various sizes and different colors that stay on the screen until cleared with the space bar, instead of giant circles flickering really fast.

 

/*
 * 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 = "COM11"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
void setup() {
  size(500,500);
  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));
    drawball(x,y, 50);
  }
}
// draw balls
void drawball(int x, int y, int r) {
  for (int i=0; i<100; i++ ) {
    //fill(255-i,i,240);
    int rval = int(random(0,r%255));
    int g = int(random(r%255,255));
    int b = int(random(0,255));
    fill(rval,g,b);
    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);
    val = val/5;
    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
  }
}
IMG_1264.JPG
0
Your rating: None