Sensing PART II: Force Sensitive Resistors and Photocells

Assignment: Sensing PART II: Force Sensitive Resistors and Photocells

Collaborators:

 

Description

The goal of this assignment was to learn about force sensitive resistors and photocells, as well as Processing.

 

 

Components Used

The following components were used:

  • Arduino
  • Breadboard
  • Rubber band (2)
  • USB cable
  • Jumper Cables (several)
  • 220 Ohm resistor (2)
  • 10k Ohm resistor (2)
  • LED light (2: green and blue)
  • Potentiometer
  • Light sensor
  • Force sensor

 

Arduino Code

The following code causes green and blue LEDs to blink alternatingly. Their brightness is controlled by a photovoltaic sensor and their blinking rate is controlled by a potentiometer. This provides a nice ambient-environment mood for the MP3 triggered by the Processing code. Also, values from a force sensor are read and fed into the serial port to be used in Processing.

/*
* 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 potPin = 0; // select the input pin for the sensor
int photoPin = 1;
int forcePin = 2;
int greenPin = 11; // select the output pin for the LED
int bluePin = 10;
int potVal = 0; // variable to store the value coming from the sensor
int photoVal = 0;
int forceVal = 0;
void setup() {
Serial.begin(9600);
}

void loop() {
potVal = analogRead(potPin); // read the value from the sensor, 0-1023
photoVal = analogRead(photoPin);
forceVal = analogRead(forcePin);
photoVal = 1.59375*photoVal - 66; // calibrate
analogWrite(greenPin, photoVal); // analogWrite (dimming the LED) can be between 0-255
analogWrite(bluePin, 0);
Serial.println(forceVal); // writing the value to the PC via serial connection
delay(potVal); // rest a little...
analogWrite(greenPin, 0);
analogWrite(bluePin, photoVal);
delay(potVal);
}

Processing Code

The following code is intended to detect when the force sensor reaches a certain threshold and play a nice Marvin Gaye tune when that occurrs, while displaying the waveform of the left and right audio channels. Unfortunately there is some small problem and it doesn't exactly work. When the Processing code is running the serial data gets sporadic and the "song.play();" command is never triggered (the audio plays fine when "song.play();" is in the setup() loop).

/*
* Tangible MP3 Player
* Plays an MP3 and displays waveform when a force sensor is squeezed
*
* By Eric Mai
* September 30, 2009
* For i262
*/

// This code makes use of Minim: http://code.compartmental.net/tools/minim/

// Minim is an audio library that uses the JavaSoundAPI, a bit of Tritonus, and
// Javazoom's MP3SPI to provide an easy to use audio library for Processing.
// Minim can be downloaded here: http://code.compartmental.net/tools/minim/

import ddf.minim.*;

import processing.serial.*;
String portname = "/dev/tty.usbserial-A9007LQO"; // Serial port name
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

Minim minim;
AudioPlayer song;
AudioInput input;

void setup()
{
size(500, 200);
port = new Serial(this, portname, 9600); // connects to serial port

minim = new Minim(this);
song = minim.loadFile("15 Mercy Mercy Me (The Ecology).mp3");
}

void draw()
{
background(0);
stroke(255);

for(int i=0; i
line(i, 50 + song.left.get(i)*50, i+1, 50 + song.left.get(i+1)*50);
line(i, 150 + song.right.get(i)*50, i+1, 150 + song.right.get(i+1)*50);
}

}

// called whenever serial data arrives
void serialEvent(Serial p){
int c = port.read();
if (c > 900) {
song.play();
}
}

void stop()
{
// the AudioPlayer you got from Minim.loadFile()
song.close();
// the AudioInput you got from Minim.getLineIn()
input.close();
minim.stop();

// this calls the stop method that
// you are overriding by defining your own
// it must be called so that your application
// can do all the cleanup it would normally do
super.stop();
}

Fixed Processing Code (revised 10am Oct 1)

The following code fixes a datatype error in the previous Processing code.

/*
* Tangible MP3 Player
* Plays an MP3 and displays waveform when a force sensor is squeezed
*
* By Eric Mai
* September 30, 2009
* Revised October 1, 2009
* For i262
*/

// This code makes use of Minim: http://code.compartmental.net/tools/minim/

import ddf.minim.*;

import processing.serial.*;
String portname = "/dev/tty.usbserial-A9007LQO"; // Serial port name
String buf="";
Serial port;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10

Minim minim;
AudioPlayer song;
AudioInput input;

void setup()
{
size(500, 200);
port = new Serial(this, portname, 9600); // connects to serial port
port.bufferUntil('\n');

minim = new Minim(this);
song = minim.loadFile("15 Mercy Mercy Me (The Ecology).mp3");
}

void draw()
{
background(0);
stroke(255);

for(int i=0; i
line(i, 50 + song.left.get(i)*50, i+1, 50 + song.left.get(i+1)*50);
line(i, 150 + song.right.get(i)*50, i+1, 150 + song.right.get(i+1)*50);
}

}

// called whenever serial data arrives
void serialEvent(Serial port){
String inString = port.readStringUntil('\n');
float inByte = float(inString);
if (inByte > 2000) {
song.play();
}
}

void stop()
{
// the AudioPlayer you got from Minim.loadFile()
song.close();
// the AudioInput you got from Minim.getLineIn()
//input.close();
minim.stop();

// this calls the stop method that
// you are overriding by defining your own
// it must be called so that your application
// can do all the cleanup it would normally do
super.stop();
}

Pictures


The experience is enhanced by allowing a lion finger puppet to be squeezed to trigger the playing of the song.

Screen shot of the waveform in the middle of the song.