Assignment: Sensing PART II: Force Sensitive Resistors and Photocells
Collaborators:
Assignment: Sensing PART II: Force Sensitive Resistors and Photocells
Collaborators:
Description
I made an animation program, based off the animation program within one of the Processing examples. The program tracks the cusor and creates an animation of the cursor's path when the mouse is clicked. When the force sensor is pressed, the program changes the radius of the ball. I use a towel to cover up my force sensor, so the user can squeeze the towel to change the radius.
I also connected a potentiometer to the Arduino board to change the color intensity of the randome colors generated. At the max, the color is all white. The lowest value makes the colors more saturated.
You can see the demo of the program here!
http://inst.eecs.berkeley.edu/~lita/video.swf
Materials
- Potentiometer
- Force sensor
- Towel
- 10K Ohm resistor
- 220 Ohm resistor
- Arduino board
Arduino Code
// These are the input pins
int forcePin = 0; // select the input pin for the sensor
int potPin = 5;
//Values
int forceVal = 0; // variable to store the value coming from the sensor
int potVal = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
forceVal = analogRead(forcePin); // read the value from the sensor, 0-1023
potVal = analogRead(potPin);
Serial.print("F");
Serial.println(forceVal); // writing the value to the PC via serial connection
Serial.print("P");
Serial.println(potVal);
delay(50); // rest a little...
}
Processing Code
/**
* Animator.
*
* Click and drag to draw and start the program.
*
* A simple animation tool that displays a continuous cycle of
* twenty-four images. Each image is displayed for 30 milliseconds
* to create animation. While each image is displayed, it’s possible
* to draw directly into it by pressing the mouse and moving the cursor.
*
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbserial-A9007LQT"; // or "COM5"
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
float seed = 0;
int currentFrame = 0;
PImage[] frames = new PImage[24];
PImage blank;
int lastTime = 0;
void setup()
{
size(640, 640);
smooth();
background(0,0,0);
strokeWeight(10);
port = new Serial(this, portname, 9600);
for (int i = 0; i < frames.length; i++) {
frames[i] = get(); // Create a blank frame
}
blank = get(); // get a blank frame.
}
void draw()
{
int currentTime = millis();
if (currentTime > lastTime+30) {
nextFrame();
lastTime = currentTime;
}
if (mousePressed == true) {
int r= 255-int(random(seed));
int g= 255-int(random(seed));
int b = 255-int(random(seed));
stroke(r,g,b);
line(pmouseX, pmouseY, mouseX, mouseY);
}
}
void keyPressed() {
if (key == ' ')
{
for (int i = 0; i < frames.length; i++) {
frames[i] = blank; // Create a blank frame
}
draw();
}
}
void nextFrame()
{
frames[currentFrame] = get(); // Get the display window
currentFrame++; // Increment to next frame
if (currentFrame >= frames.length) {
currentFrame = 0;
}
image(frames[currentFrame], 0, 0);
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
} else if (c == lf) {
//println(buf);
char flag = buf.charAt(0);
buf = buf.substring(1, buf.length() - 1);
if (flag == 'F') {
int val = int(buf) * 2;
println("val="+val);
strokeWeight(val+10);
} else if (flag == 'P') {
int val = int(buf);
seed = float(buf) * 4.0;
println("pot="+seed);
}
buf = "";
}
}