Assignment 4: FSR, Photocell, and Etch-a-Sketch

Assignment: Sensing PART II: Force Sensitive Resistors and Photocells

Collaborators:

Description

I combined both of the assignments into one. I created a version of the childhood toy Etch A Sketch using two pots, an FSR, and a photocell. The two pots act as the two dials in Etch A Sketch: one controls horizontal movement and one controls vertical movement. I also made it a little fancier by coding in random colors as determined by the photocell. I used a watermelon seed taped over the FSR and covered it with a folded tissue as a weight diffuser, and I lightly taped a plastic cup on top.

The idea was to put brushes of different sizes and weights into the cup to determine how heavy the stroke of the line would be in the output.

Components Used

  • 2 pots
  • 1 FSR
  • 1 photocell
  • 2 10k resistors
  • 1 tissue
  • 1 plastic cup
  • 1 watermelon seed
  • Tape
  • A few pens to simulate brushes

Processing Code

/*
 * Etch-a-Sketch
 * ----------------------
 *
 * Use two pots to control a line drawing. Input from a photocell
 * randomly determines the color of the line, and the thickness of
 * the line is determined by weight detected by an FSR.
 */
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbserial-A7006TaC";
Serial port;
String buf = "";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
int x1;
int y1;
int x2 = 0;
int y2 = 1024;
int weight = 0;
float light = 0;

void setup() {
  size(1024,1024);
  frameRate(10);
  smooth();
  background(#ffffff);
  noStroke();
  port = new Serial(this, portname, 9600);
}
void draw() {
}
void keyPressed() {
  if(key == ' ') {
    background(#ffffff);  // erase screen
  }
}

// called whenever serial data arrives
void serialEvent(Serial p) {
  int c = port.read();
  if (c != lf) {
    buf += char(c);
  } else if (c == lf) {
    println(buf);
    char indicator = buf.charAt(0);
    buf = buf.substring(1, buf.length() - 1);
    if (indicator == 'h') {
      x1 = x2;
      x2 = int(buf);
    } else if (indicator == 'v') {
      y1 = y2;
      y2 = 1024 - int(buf);
    } else if (indicator == 'r') {
      weight = int(buf) / 10;
    } else if (indicator == 'c') {
      light = float(buf) / 4;
      line(x1, y1, x2, y2);
      int rand1 = int(random(light));
      int rand2 = int(random(light));
      int rand3 = int(random(light));
      stroke(rand1, rand2, rand3);
      strokeWeight(weight);     
    }
    buf = "";
  }
}

Arduino Code

// Output
int hPotPin = 0;
int vPotPin = 1;
int fsrPin = 5;
int photPin = 4;
int hPotVal = 0;
int vPotVal = 0;
int fsrVal = 0;
int photVal = 0;

void setup()
{
Serial.begin(9600); // ...set up the serial ouput on 0004 style
}

// Main program
void loop()
{
hPotVal = analogRead(hPotPin);
vPotVal = analogRead(vPotPin);
fsrVal = analogRead(fsrPin);
photVal = analogRead(photPin);
Serial.print("h");
Serial.println(hPotVal);
Serial.print("v");
Serial.println(vPotVal);
Serial.print("r");
Serial.println(fsrVal); // to discount the weight of the glass
Serial.print("c");
Serial.println(photVal);
}

Item

This is a picture of my breadboard wiring.

 

And this is the entire contraption. The pen in the cup is supposed to be a paintbrush.

And here's a video (swf) demo-ing the application.