Assignment: Sensing PART II: Force Sensitive Resistors and Photocells
Collaborators:
Assignment: Sensing PART II: Force Sensitive Resistors and Photocells
Collaborators:
Fountain Drink Level
Description
For the FSR, I taped a folded piece of paper around it to help sense the weight. I then placed a CD case on top of it.
With less pressure on the FSR, the LED will get brighter. So with a 20oz fountain drink on the CD case, it will light up brighter and brighter as it gets sipped, until it is empty.
WIth Processing, I just made a visual representation of drink level. Depending on how full the cup is, the screen is completely filled with blue. Less drink leads to lower blue 'water level'on the screen.
Components Used
- Light Emitting Diode (LED)
- 220Ohm, 10K Resistor
- FSR
- Paper
- Scotch Tape
- CD case
Arduino Code
/*
* less force increases brightness of one led
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/
int potPin = 2; // select the input pin for the fsr
int ledPin = 9; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
int ledVal =0;
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the value from the sensor, between 0 - 1024
ledVal= 255-(val/1.3);
if(ledVal<0)ledVal=0;
Serial.println(ledVal);
analogWrite(ledPin, ledVal); // analogWrite can be between 0-255
}
Processing Code
/**
* Adapted from "Load and Display" Example
*
* Images can be loaded and displayed to the screen at their actual size
* or any other size.
*/
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "COM4"; // or "COM5"
Serial port;
String buf="";
int val = 250;
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
int weightfactor = 1; //this is to adjust factor depending on weight range, increase for lighter objects
PImage a; // Declare variable "a" of type PImage
String imageName = "fill.JPG";
void setup() {
size(500, 500);
// The file imageName must be in data folder
a = loadImage(imageName); // Load the image into the program
noStroke();
frameRate(2);
smooth();
port = new Serial(this, portname, 9600);
}
void draw() {
background(120);
image(a,0,500-(screenfactor*val));
}
// 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 newval = int(buf);
println("val="+val);
if (newval!=val){ //to minimize activity, only change image when weight changes
background(120);
image(a,0,(500-(screenfactor*val)));
val=newval;
}
buf = "";
}
}