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:
I used two pieces of Spenco gel from an old bike seat cover as a "diffuser" for my force-sensitive resistor. The gel pieces sit on top of the FSR and sandwich it between themselves and a small stack of note paper. There is also a photocell in the circuit, which slows down the data rate a tiny bit while a hand is over the gel pads. The final piece uses Processing to place the input from the FSR in the context of a miniature "test your strength" carnival game. The user pushes down on the gel as hard as they can and sees their score on a scale reminiscent of the carnival piece. If they press hard enough, they trigger a "bell" at the top to "ring".
LEDs
wire
breadboard
resistors
force-sensitive resistor
photocell
Arduino board
Spenco gel pieces
notepad
/*
* FSR provides input from user pressing down on gel pads.
* photocell provides change in delay time, going slower when light is blocked
* one LED is also controlled by the FSR, so the user knows when data has been taken
* by Annette Greiner 2008
*/
int potPin1 = 0; // select the input pin for the blink rate potentiometer
int potPin2 = 1; // select the input pin for the brightness potentiometer
int ledPin = 9; // select the pin for the LED
int brightval = 0; // variable to store the value coming from the brightness sensor
int blinkval = 0; //variable to store the value coming from the blink rate sensor
int blinkdelay = 0; // variable to store the blink delay time (so higher value gives faster blink)
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
}
void loop() {
blinkval = analogRead(potPin1); // read the value from the sensor
blinkdelay = 1024-blinkval+1000;
brightval = analogRead(potPin2); // read the value from the sensor, between 0 - 1024
analogWrite(ledPin, brightval/4); // analogWrite can be between 0-255
delay(blinkdelay); // stop the program for some time
analogWrite(ledPin, 0); // analogWrite can be between 0-255
delay(blinkdelay); // stop the program for some time
Serial.println(brightval);
}
Processing Code
/*
* Arduino Strength Test
* (modified from Arduino Ball Paint by Annette Greiner 2008)
* ----------------------
*
*
* 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.
* Shows a score on a display like a miniature carnival "test
* your strength" game
*/
import processing.serial.*;
// Change this to the portname your Arduino board
//String portname = "/dev/tty.usbserial-A4001nLJ"; // or "COM5"
String portname = "/dev/tty.usbserial-A70061zk"; // for usb port nearest screen
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
PImage b;
int score, top;
void setup() {
size(110,482);
frameRate(10);
smooth();
noStroke();
port = new Serial(this, portname, 9600);
b = loadImage("strengthtest.gif");
if(b!=null) background(b);
}
void draw() {
}
// draw balls
void drawball(int x, int y, int r) {
fill(40,40,40);
ellipse(x,y,r,r);
}
void drawBellRing(){
for(int i=0; i<4; i++){
noFill();
stroke(1);
strokeWeight(2);
arc(40,50,60,60, TWO_PI-PI/4, PI/4);
arc(40,50,80,80, TWO_PI-PI/4, PI/4);
}
}
// called whenever serial data arrives
void serialEvent(Serial p) {
int c = port.read();
if (c != lf && c != cr) {
buf += char(c);
}
if (c == lf) {
if(b!=null) background(b); // erase screen
int val = int(buf);
println("val="+val);
score = (int)(val/2.175);//scale the score to 470 pixel height
top = 470 - score;
println("score="+score);
for(int i=470; i>top; i--){
drawball(40,i,30);
}
if(score >= 400){
//high score!!
drawBellRing();
}
buf = "";
}
}