User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Lab 4: Using Photocells and FSRs to Change LED and Graphical Output

Submitted by Becky on Tue, 09/30/2008 - 17:45

Assignment: Sensing PART II: Force sensors and photocells

Collaborators:

Assignment: Sensing PART II: Force sensors and photocells
Collaborators:

Assignment: Sensing PART II: Force sensors and photocells
Collaborators:

Description: Part I

In Part I of this lab, we experimented with using Photocells and FSRs to effect LED behavior.  By increasing the resistance in the circuit with light (for the photocell) or force (for the FSR), I made the LED blink at a different rate.

Materials Used:

  • Arduino board
  • wires
  • breadboard
  • computer
  • 1 LED
  • 1 220k resistor
  • 1 10k resistor
  • photocell
  • FSR

Code: Part Ia

/*
* FSR fades one led
* modified version of AnalogInput
* by DojoDave
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int potPin = 0; // select the input pin for the potentiometer
int ledPin = 13; // 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, val/4); // analogWrite can be between 0-255
}

Description: Part Ib

In this part of the lab, we used the variable resistor sensors to control graphical output using Processing.  The first experiment I did was to use the FSR to control the rate at which balls would appear in the graphical display window of processing.

In the second part (code below), I modified code for drawing a bullseye so that the bullseye would redraw at a different size depending on the pressure sensed through the FSR.

Materials Used:

  • same as above
  • additionally - the Processing software

Code Part Ib:First Exploration Using FSR

import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbserial-A7007bg6"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

void setup()
{
size(500, 500);
background(51);
noStroke();
smooth();
//noLoop();
port = new Serial(this, portname, 9600);
}

void draw()
{
//drawTarget(100, 100, 200, 10);
}

//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);
//drawTarget(100, 100, 200, 10);
drawTarget(250, 250, val, 10);
buf = "";
}
}

void drawTarget(int xloc, int yloc, int size, int num)
{
float grayvalues = 255/num;
float steps = size/num;
for(int i=0; i<num; i++) {
fill(i*grayvalues);
ellipse(xloc, yloc, size-i*steps, size-i*steps);
}
}

Images:

IMG_1407ed
Arduino board with FSR connected to Analog inputs.

IMG_1406_ed
Using one finger to exert pressure on the FSR.

IMG_1408
Diffusing force on sensor across a flat sheet of bubble wrap.

IMG_1424
Squeezing the FSR through a shape made from bubble wrap.

Code Part Ib:Second Exploration Using FSR and Potentiometer

Test Code for Arduino:

/*

* Resistive Sensor Input

* Takes the input from a resistive sensor, e.g., FSR or photocell

* Dims the LED accordingly, and sends the value (0-255) to the serial port

*/

int aIn = 0; // select the input pin for the sensor
int aOut = 13; // select the output pin for the LED

int bIn = 2;
int bOut = 11;

int aVal = 0; // variable to store the value coming from the sensor
int bVal = 0;

void setup() {
Serial.begin(9600);

}

void loop() {

aVal = analogRead(aIn); // read the value from the sensor, 0-1023
analogWrite(aOut, aVal/4); // analogWrite (dimming the LED) can be between 0-255
Serial.println(aVal/4); // writing the value to the PC via serial connection

bVal = analogRead(bIn) / 4;
analogWrite(bOut, bVal);


delay(50); // rest a little...

}

 

Code for Processing:
See notes below.

Part II: Mechanical

In this part, we create a physical object across which we can apply force to the FSR.  I used bubble wrap to create a surface that a person can squeeze in various ways to create more force on the FSR.

 

Conclusions/Observations:

The FSR was more sensitive than the photocell -- it sensed a greater range of variation in force than the photocell sensed in light, and so its variable resistance was greater than that of the photocell.  Therefore, the LED and graphical output varied more with the FSR than with the photocell.

I ran the Arduino software while also running the Processing software so that I could use the serial monitor in the Arduino software to observe changes in force on the FSR.

In future explorations I will use both a potentiometer and the FSR to cause change in the graphics drawn on the computer.  One input will change the size of the bullseye and one input will change the location.  The code with which I'm working to try to read in multiple inputs from the serial connection is attached bullseye_multi.rtf.  I am still trying to separate the input from the FSR and from the potentiometer.

Because it is easy to grasp the bubble wrap shape in your hand, it is natural to interact with the FSR by squeezing the shape.  I think this is more pleasant than applying pressure just with a finger or with a flat hand on a sheet of bubble wrap. Both the ability to change the shape to fit in your hand and the elasticity of the bubble wrap make this shape pleasing to squeeze.