Assignment: Sensing PART II: Force Sensitive Resistors and Photocells
Collaborators:
Description
RBS (Rice Bowl Scale) is meant to read in the weight of a bowl of rice and give an estimate of how much rice is in the bowl. This estimate is displayed on screen with an image of one of six different bowls with varying amounts of rice.
The force resistor is meant to read the value of nothing in the bowl as higher than when it is activated and therefore display an empty bowl. As the force sensor is activated though it should input lower values and therefore display more rice in the bowl.
Components Used
- 1 Light Emitting Diode (LED)
- 3 Force Sensitive Resistor
- Small folded paper over resistor
- 1 Resistor
- Arduino Board
- Breadboard
- Wire
- Bowl
- Rice
Arduino Code
/*
* 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 sensorPin = 0; // select the input pin for the sensor
int ledPin = 7; // select the output 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(sensorPin); // read the value from the sensor, 0-1023
analogWrite(ledPin, val/4); // analogWrite (dimming the LED) can be between 0-255
Serial.println(val/4); // writing the value to the PC via serial connection
delay(1000); // rest a little...
}
Processing Code
import processing.serial.*;
// Change this to the portname your Arduino board
String portname = "/dev/tty.usbserial-A9007Lpu"; // serial port
Serial port;
String buf="";
int cr = 13; // ASCII return == 13
int lf = 10; // ASCII linefeed == 10
int xVal = 200;
int yVal = 420;
PImage R0;
PImage R1;
PImage R2;
PImage R3;
PImage R4;
PImage R5;
void setup() {
//initialize canvas:
size(800,533);
frameRate(10);
smooth();
background(40,40,40);
noStroke();
port = new Serial(this, portname, 9600);
//Initialize rice bowl images:
R0 = loadImage("rice1.gif");
R1 = loadImage("rice2.gif");
R2 = loadImage("rice3.gif");
R3 = loadImage("rice4.gif");
R4 = loadImage("rice5.gif");
R5 = loadImage("rice6.gif");
background(R0);
//image(piggyBank, 0, 0);
}
void draw() {
}
// 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);
buf = "";
//background(40,40,40); // erase screen
float n = 1.0; //sensitivity factor
if(val < 250/n)
background(R0);
else if(val < 200/n)
background(R1);
else if(val < 150/n)
background(R2);
else if(val < 100/n)
background(R3);
else if(val < 50/n)
background(R4);
else if(val < 00/n)
background(R5);
}
}
Images